2016-12-01 2 views
1

을 내가 가지고 독립 실행 형 쿼리로 작동하는 다음 코드를CASE는 IFElse, 또는 스위치 PowerShell은 출력을 변경하세요-WmiObject를

$Type = (Invoke-Command -ComputerName $Computer -ScriptBlock { Get-WmiObject -Class Win32_ComputerSystem | Select-Object -ExpandProperty Manufacturer }) 

    switch -regex ($Type) 
    { 
     "VMw.+" {"VM"} 
     default {"Physical"} 
    } 

내가 대신 호출 명령을 내 스위치 명령을 추가 할 여러 컴퓨터에 대해 실행할 수 있도록 $ Type 변수를 삭제하는 변수를 어떻게 완성 할 수 있습니까? 최종 결과를 얻으려면 Switch를 사용하지 않겠습니까? 간단한 foreach 루프에 싸서 Invoke-Command

switch -regex (Get-WmiObject -ComputerName $Computer -Class Win32_ComputerSystem | Select-Object -ExpandProperty Manufacturer) 
{ 
    "VMw.+" {"VM"} 
    default {"Physical"} 
} 

그리고를 사용할 필요가 없습니다 당신은 쉽게 여러 대의 컴퓨터에 대해 실행할 수 있도록

답변

1

Get-WmiObjectComputerName 속성이 있습니다 :

$Computers = "computer1","computer3","computer3" 

foreach ($Computer in $Computers) { 
    switch -regex (Get-WmiObject -ComputerName $Computer -Class Win32_ComputerSystem | Select-Object -ExpandProperty Manufacturer) 
    { 
     "VMw.+" {Write-Output "$Computer is a VM Computer"} 
     default {Write-Output "$Computer is a Physical Computer"} 
    } 
} 
+0

그 나를 위해 작동합니다 :'code' "Type"= switch -regex (Get-WmiObject -ComputerName $ Computer -Class Win32_ComputerSystem | Select-Object -ExpandProperty Manufacturer) { "VMw. +"{Write-Output "VM"} 기본값 {Write-Output "Physical"} } – pinchepooch