【问题标题】:Unable to Start a PowerShell ForEach Loop with a Variable无法使用变量启动 PowerShell ForEach 循环
【发布时间】:2016-12-22 20:41:26
【问题描述】:

从下面的 sn-p 可以看出,我是 PowerShell 新手。我正在尝试使用运行工具的驱动器号的变量来运行命令。我收到一个错误:

表达式语句 'tools\perl_5.20.2.1\bin\perl.exe' 中有意外标记

如果我对驱动器号进行硬编码,此脚本可以正常工作。有人可以建议另一种方式吗?

$OutDrive = Read-Host -Prompt 'Please select a drive letter to create output     folders (e.g. c:)'

$ntuser = Get-ChildItem -Force $outdrive\Folder\$ComputerName-$Date\users |
      Where-Object {$_.PSIsContainer} | 
      foreach {$_.Name}

foreach ($Name in $ntuser){
    **$OutDrive\tools\perl_5.20.2.1\bin\perl.exe c:\tools\somescript**     $Outdrive\Folder\$ComputerName-$Date\users\$Name\ntuser.dat -f ntuser Out-File -   FilePath $Outdrive\FOlder\$ComputerName-$Date\users\$Name\$Name-blah.txt
}

【问题讨论】:

  • Get-ChildItem的参数加上引号
  • 虽然您应该在Get-ChildItem 参数周围加上引号,但这似乎不是导致错误的问题。 foreach 是否应该全部在一条线上而没有这样的管道?发布的代码至少需要一个 | 在 Out-File 之前。
  • 一直在使用>>而不是|当我对驱动器号进行硬编码时,Out-File 似乎工作正常

标签: powershell powershell-4.0


【解决方案1】:
& "$OutDrive\tools\perl_5.20.2.1\bin\perl.exe" ......

【讨论】:

  • 试过 & "$OutDrive\tools\perl_5.20.2.1\bin\perl.exe" ......现在得到错误 - & : The term 'd:\tools\ perl_5.20.2.1\bin\perl.exe 未被识别为 cmdlet、函数、脚本文件或可运行程序的名称。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-07-11
  • 2011-12-09
  • 1970-01-01
  • 2022-07-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多