2017-07-15 81 views
1

我是一个完整的bb和powershell脚本编写的newb,并且一直在试图弄清楚如何在powershell中定义一个变量,并在同一个目录中存储的批处理脚本中定义一个变量作为PowerShell脚本。从同一个目录中的.bat文件抓取powershell变量

基本上,我想要从config.bat中获取Dropbox Path并将变量$ dropbox设置为在powershell脚本中的其他变量中使用。这是我到目前为止....

config.bat:

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

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" 

一切正常config.bat文件的绝对路径(如图所示),但如果我尝试使用相对路径$PSScriptRoot\config.bat.\config.bat,则出现错误,powerhsell找不到显示错误的config.bat文件...

“Get-Content:无法找到路径'C:\ config.bat',因为它不存在 。”

我也曾尝试的另一种方式,并使用写入主机,看看是否变量显示正确的目录....

$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 

与上面的代码,我得到这个PS错误.. ..

“导入模块:指定的模块‘C:\用户\用户\ config.bat’。是 未加载,因为没有有效的模块文件是在任何模块 目录中找到”

输出显示:

Config file full path = C:\Users\User\config.bat # WRONG 
Dropbox Path = 

的路径必须是相对的,我的目的和它可能是一些令人尴尬的超级简单,这里的编码大师都会让我看起来像一个完整的numpty :)

请帮忙!

回答

0

转到MyInvocation对象以查找脚本的路径。

$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 

更新:基于后续的答案,特别是mklement0,这里是一个修改后的答案。

对于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 

对于PowerShell的3。0+版本:

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

我现在用MyInvocation得到这个错误......“异常调用 “GetDirectoryName” 与 “1” 的说法(S): “路径不是合法的形式” 在线:1 char:1 + $ dropbox = Get-Content -Path“$([io.path] :: GetDirectoryName($ MyInvocation.MyCom ... – Bearwires

+0

” Get-Content:无法找到路径'C:\ config.bat',因为它不存在。“它似乎没有为配置文件位置定义正确的目录,即使它位于相同的目录中 – Bearwires

+0

什么版本PowerShell你在运行吗?这适用于PowerShell 5和6测试版3. – lit

0

此方法适用于像预期一样:

#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" 
1

要引用运行脚本的位置(目录)

  • PSv3 +:使用$PSScriptRoot$PSCommandPath指脚本的全部f ilename)。
  • PSv2-,除了在模块:使用Split-Path $MyInvocation.MyCommand.Path$MyInvocation.MyCommand.Path指的是脚本的完整文件名)。

的惯用PSv3 +代码的重新配制的问题:

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