您應該使用一個腳本塊。它會在你每次調用它時擴展變量。我的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
你不能以這種方式使用字符串,你需要一個腳本塊。 – 2013-03-19 14:59:28