2013-04-24 46 views
0

如果虚拟机名称存在于vm.txt文件中,Powershell脚本应该启动。 例子:如果虚拟机名称在vm.txt中,则执行

get-vm | foreach-object -process { 
If ($_.Name --matches one of the entries in the .txt File) { 
execute this part } 
else { execute this part } 

vm.txt文件: SERVER01 Server02上 server03

非常感谢!

回答

0

使用-contains运营商检查名称的列表中包含特定名称:

$vmlist = Get-Content vm.txt 

Get-VM | % { 
    if ($vmlist -contains $_.Name) { 
    # execute this part 
    } else { 
    # execute that part 
    } 
} 
+0

完美,感谢您的快速响应!有​​时候,解决方法很简单! – Daniel4711 2013-04-24 07:27:23

0

目前还不清楚vm文件是否在一行上有所有名称,或者它自己的行上是否有每个名称。 对于前者使用-match,对于后面的替换-match与-contains。

$content = Get-Content vm.txt 

get-vm | 
where-object { $_.Name -match $content } | 
foreach-object { 'execute this part'}