您应该使用脚本块。每次调用它时它都会扩展变量。我自己安装了 Hyper-V,所以我为你做了一个例子:
PS > $computer = "DC", "SQL"
$mywherestatement = { $_.Name -like $Computer[0] -or $_.Name -like $Computer[1] }
Get-VM | Where $mywherestatement
Name State CPUUsage(%) MemoryAssigned(M) Uptime Status
---- ----- ----------- ----------------- ------ ------
DC Saved 0 0 00:00:00 Operating normally
SQL Saved 0 0 00:00:00 Operating normally
PS > $computer = "CLIENT", "WebDev"
Get-VM | Where $mywherestatement
Name State CPUUsage(%) MemoryAssigned(M) Uptime Status
---- ----- ----------- ----------------- ------ ------
CLIENT Saved 0 0 00:00:00 Operating normally
WebDev Saved 0 0 00:00:00 Operating normally
如果您的$computer
数组仅包含您要检查的名称,我会改用-contains
它,因为它适用于任何大小的数组。前任:
PS > $computer = "DC", "SQL", "CLIENT", "WebDev"
Get-VM | Where { $Computer -contains $_.Name }
Name State CPUUsage(%) MemoryAssigned(M) Uptime Status
---- ----- ----------- ----------------- ------ ------
CLIENT Saved 0 0 00:00:00 Operating normally
DC Saved 0 0 00:00:00 Operating normally
SQL Saved 0 0 00:00:00 Operating normally
WebDev Saved 0 0 00:00:00 Operating normally