Bearwires
Bearwires

Reputation: 23

Grab powershell variable from .bat file in same directory

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:

@Echo OFF
set Dropbox_Path=C:\Users\User\Documents\Dropbox

Extract of test.ps1:

#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....

$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:

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!

Upvotes: 1

Views: 664

Answers (3)

lit
lit

Reputation: 16236

Turn to the MyInvocation object to find the path to the script.

$this_dir = [io.path]::GetDirectoryName($MyInvocation.MyCommand.Path)
$this_dir

$dropbox = Get-Content -Path "$([io.path]::GetDirectoryName($MyInvocation.MyCommand.Path))\config.bat" `
          | ? { $_ -match "Dropbox_Path=\s*"} `
          | select -First 1 `
          | % { ($_ -split "=", 2)[1] }

$dropbox

Update: Based on subsequent answers, particularly mklement0, here is a revised answer.

For all versions of PowerShell:

$scriptdir = Split-Path -Path $MyInvocation.MyCommand.Path -Parent
$dropbox = (Select-String -List `
    -Pattern ' Dropbox_Path=(.*)' `
    -Path "$scriptdir/config.bat").Matches[0].Groups[1].Value

For PowerShell 3.0+ versions:

$dropbox = (Select-String -List `
    -Pattern ' Dropbox_Path=(.*)' `
    -Path $PSScriptRoot/config.bat).Matches[0].Groups[1].Value

Upvotes: 0

mklement0
mklement0

Reputation: 437998

To refer to the running script's location (directory):

  • PSv3+: Use $PSScriptRoot ($PSCommandPath refers to the script's full filename).
  • PSv2-, except in modules: Use Split-Path $MyInvocation.MyCommand.Path ($MyInvocation.MyCommand.Path refers to the script's full filename).

An idiomatic PSv3+ reformulation of the code in the question:

$dropbox = (Select-String -List ' Dropbox_Path=(.*)' $PSScriptRoot/config.bat).
             Matches[0].Groups[1].Value

Upvotes: 1

Bearwires
Bearwires

Reputation: 23

This solution works as intended:

#Grab Dropbox Path from config.bat and set $dropbox variable

$dropbox = Get-Content $PSScriptRoot\config.bat `
      | ? { $_ -match "Dropbox_Path="} `
      | 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"

Upvotes: 0

Related Questions