在PowerShell中将evalute表达式输出到变量中

时间:2018-04-17 12:11:06

标签: powershell active-directory

我希望能够在从类似字符串构建它之后构建Get-ADComputer命令:

$FilterOperatingSystems = "*Windows 7*" -or OperatingSystem -like "*Windows 8*" -or OperatingSystem -like "*Windows 10*"

$FilterOperatingSystems是脚本的参数,因此在由任务计划程序执行时它将来会进行校对)

$command='Get-ADComputer -properties OperatingSystem -Filter {(OperatingSystem -like '+$FilterOperatingSystems+' )} |'
$command+= 'Where-Object {$_.name -like "*-*"} | '
$command+= 'Where-Object {$_.name -NotLike "V7-*"} | '
$command+= 'Where-Object {$_.name -NotLike "*-NONE"} | '
$command+= 'Where-Object {$_.name -NotLike "*-ONCALL"} | '
$command+= 'Where-Object {$_.name -NotLike "*-BLACKBAUD"} | '
$command+= 'Where-Object {$|_.name -NotLike "SC-WIN7-1"} | '
$command+= 'Where-Object {$_.name -NotLike "UT-SWCLIENT-01"} | '
$command+= 'Select-Object -Expand Name'

Write-Host $command

$computer =iex $command 

$command就像这样:

Get-ADComputer -properties OperatingSystem -Filter {(OperatingSystem -like "*Windows 7*" -or OperatingSystem -like "*Windows 8*"  -or OperatingSystem
 -like "*Windows 10*"  )} |Where-Object {$_.name -like "*-*"} | Where-Object {$_.name -NotLike "V7-*"} | Where-Object {$_.name -NotLike "*-NONE"} | W
here-Object {$_.name -NotLike "*-ONCALL"} | Where-Object {$_.name -NotLike "*-BLACKBAUD"} | Where-Object {$|_.name -NotLike "SC-WIN7-1"} | Where-Obje
ct {$_.name -NotLike "UT-SWCLIENT-01"} | Select-Object -Expand Name

但是我收到了错误:

  

$:术语' $'不被识别为cmdlet的名称,函数,   脚本文件或可操作程序。检查名称的拼写,或   如果包含路径,请验证路径是否正确,然后重试。

那么,有可能做那样的事吗?如果是这样,那么正确的做法是什么?

1 个答案:

答案 0 :(得分:2)

我认为你有一个错字:

$|_.name -NotLike "SC-WIN7-1"

那不应该是$_.name吗?