Grab powershell variable from .bat file in same directory
Posted: 15 Jul 2017 11:15
Hey guys, I am a complete newb with bash and powershell scripting and have been trying to figure out how to define a variable in powershell with a variable defined in a batch script stored within the same directory as the powershell script.
Basically, I want to get the Dropbox Path from config.bat and set variable $dropbox for use in other variables within the powershell script. This is what I have so far....
config.bat:
Extract of test.ps1:
Everything works fine if I specify the absolute path for the config.bat file (as shown) but if I try using relative paths $PSScriptRoot\config.bat or .\config.bat, I get errors and powerhsell cannot find the config.bat file showing error...
"Get-Content : Cannot find path 'C:\config.bat' because it does not exist."
I have also tried an alternative way and used write-host to see if the variables show the correct directories....
With the above code, I get this PS error....
"Import-Module : The specified module 'C:\Users\User\config.bat' was not loaded because no valid module file was found in any module directory."
The output shows:
The path needs to be relative for my purposes and its probably something embarrassingly super simple that the coding gurus here will make me look like a complete numpty
Please help!
Basically, I want to get the Dropbox Path from config.bat and set variable $dropbox for use in other variables within the powershell script. This is what I have so far....
config.bat:
Code: Select all
@Echo OFF
set Dropbox_Path=C:\Users\User\Documents\Dropbox
Extract of test.ps1:
Code: Select all
#Grab Dropbox Path from config.bat and set $dropbox variable
$dropbox = Get-Content C:\Users\User\Documents\Dropbox\test\Batch-Files\config.bat `
| ? { $_ -match "Dropbox_Path=\s*"} `
| select -First 1 `
| % { ($_ -split "=", 2)[1] }
# Set other Variables using $dropbox
$input_csv = "$dropbox\test\Test.csv"
$master_csv = "$dropbox\test\Test-MASTER.csv"
$output_file = "$dropbox\test\Test.txt"
Everything works fine if I specify the absolute path for the config.bat file (as shown) but if I try using relative paths $PSScriptRoot\config.bat or .\config.bat, I get errors and powerhsell cannot find the config.bat file showing error...
"Get-Content : Cannot find path 'C:\config.bat' because it does not exist."
I have also tried an alternative way and used write-host to see if the variables show the correct directories....
Code: Select all
$configFullPath = "$($pwd)\config.bat"
Write-Host "Config file full path = $configFullPath"
Import-Module -Force $configFullPath
$dropbox = Get-Content _config.bat `
| ? { $_ -match "Dropbox_Path=\s*"} `
| select -First 1 `
| % { ($_ -split "=", 2)[1] }
Write-Host "Config file full path = $configFullPath"
Write-Host "Dropbox Path = $dropbox"
PAUSE
With the above code, I get this PS error....
"Import-Module : The specified module 'C:\Users\User\config.bat' was not loaded because no valid module file was found in any module directory."
The output shows:
Code: Select all
Config file full path = C:\Users\User\config.bat # WRONG
Dropbox Path =
The path needs to be relative for my purposes and its probably something embarrassingly super simple that the coding gurus here will make me look like a complete numpty
Please help!