2016-11-30 2 views
1

내가 다음 줄을 호출 명령 내에서 파워 쉘 결과의 일부를 제거 ForEach 문을 잘 작동하지만 출력 그냥 : OS30 : OEM_SLP

나는 그것을 분해 할 수 있지만 다음 마지막 컴퓨터에서 값을 걸리고 모든 항목에 대해 넣습니다 :

$OSType = Invoke-Command -ComputerName $Computer -ScriptBlock { Get-CimInstance SoftwareLicensingProduct | Where-Object -FilterScript { ($_.Description -like "W*" -and $_.licensestatus -eq 1) } | select-object -first 1 -ExpandProperty Description} ` 

     $OS1 = $OSType.Split(',')[1] 

     $OS2 = $OS1.Split(' ')[1] 

     "OSType" = $OS2 

아래 3 줄에 Invoke 명령을 사용하려고하면 오류가 발생합니다. 어떤 아이디어?

$OSType = (Invoke-Command -ComputerName $Computer -ScriptBlock { 
    Get-CimInstance SoftwareLicensingProduct | 
     Where-Object -FilterScript { ($_.Description -like "W*" -and $_.licensestatus -eq 1) } | 
     select-object -first 1 -ExpandProperty Description 
    }) -replace '.*,\s?(\S+).*', '$1' 

그러나, 나는 개체를 만들기 전에 번 Get-WmiObject –class Win32_processor를 호출 할 것을 권장 높은 것 :

$computers = Get-Content -Path "C:\Computers.txt" 

$Results = @() 

ForEach ($Computer in $Computers) { 
    $Results += New-Object PSObject -Property @{ 

     "Computer" = Invoke-Command -ComputerName $Computer -ScriptBlock { Get-WmiObject –class Win32_processor | select-object -first 1 -ExpandProperty SystemName } ` 

     "CPU1" = Invoke-Command -ComputerName $Computer -ScriptBlock { Get-WmiObject –class Win32_processor | Where-Object -FilterScript { ($_.SocketDesignation -like "*1*") -or ($_.SocketDesignation -like "*0*") } | select-object -first 1 -ExpandProperty Name } ` 

     "Cores1" = Invoke-Command -ComputerName $Computer -ScriptBlock { Get-WmiObject –class Win32_processor | Where-Object -FilterScript { ($_.SocketDesignation -like "*1*") -or ($_.SocketDesignation -like "*0*") } | select-object -first 1 -ExpandProperty NumberOfCores } ` 

     "LogProc1" = Invoke-Command -ComputerName $Computer -ScriptBlock { Get-WmiObject –class Win32_processor | Where-Object -FilterScript { ($_.SocketDesignation -like "*1*") -or ($_.SocketDesignation -like "*0*") } | select-object -first 1 -ExpandProperty NumberOfLogicalProcessors } 

     "CPU2" = Invoke-Command -ComputerName $Computer -ScriptBlock { Get-WmiObject –class Win32_processor | Where-Object -FilterScript { ($_.SocketDesignation -like "*2*") -or ($_.SocketDesignation -like "*#1*") } | select-object -first 1 -ExpandProperty Name } ` 

     "Cores2" = Invoke-Command -ComputerName $Computer -ScriptBlock { Get-WmiObject –class Win32_processor | Where-Object -FilterScript { ($_.SocketDesignation -like "*2*") -or ($_.SocketDesignation -like "*#1*") } | select-object -first 1 -ExpandProperty NumberOfCores } ` 

     "LogProc2" = Invoke-Command -ComputerName $Computer -ScriptBlock { Get-WmiObject –class Win32_processor | Where-Object -FilterScript { ($_.SocketDesignation -like "*2*") -or ($_.SocketDesignation -like "*#1*") } | select-object -first 1 -ExpandProperty NumberOfLogicalProcessors } ` 

     "OS" = Invoke-Command -ComputerName $Computer -ScriptBlock { Get-CimInstance Win32_OperatingSystem | Select-Object -ExpandProperty Caption } ` 

     #"OSType" = Invoke-Command -ComputerName $Computer -ScriptBlock { Get-CimInstance SoftwareLicensingProduct | Where-Object -FilterScript { ($_.Description -like "W*" -and $_.licensestatus -eq 1) } | select-object -first 1 -ExpandProperty Description} ` 

     $OSType = Invoke-Command -ComputerName $Computer -ScriptBlock { Get-CimInstance SoftwareLicensingProduct | Where-Object -FilterScript { ($_.Description -like "W*" -and $_.licensestatus -eq 1) } | select-object -first 1 -ExpandProperty Description} ` 

     $OS1 = $OSType.Split(',')[1] 

     $OS2 = $OS1.Split(' ')[1] 

     "OSType" = $OS2 

     "SP" = Invoke-Command -ComputerName $Computer -ScriptBlock { Get-CimInstance Win32_OperatingSystem | Select-Object -ExpandProperty ServicePackMajorVersion } ` 

     "OSArch" = Invoke-Command -ComputerName $Computer -ScriptBlock { Get-CimInstance Win32_OperatingSystem | Select-Object -ExpandProperty OSArchitecture } ` 

     "Office" = Invoke-Command -ComputerName $Computer -ScriptBlock { Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | Where-Object -FilterScript { (($_.Publisher -like "Microsoft*") -and ($_.DisplayName -like "*Office 64*")) } | Select-Object -first 1 -ExpandProperty DisplayName } 

     "FullSQL" = Invoke-Command -ComputerName $Computer -ScriptBlock {If (Test-Path "HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names") { Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | Where-Object -FilterScript { (($_.Publisher -like "Microsoft*") -and ($_.DisplayName -like "Microsoft SQL Server*(*-bit)")) } | Select-Object -first 1 -ExpandProperty DisplayName }} 

    } 
} 

$Results | Select-Object Computer, CPU1, Cores1, LogProc1, CPU2, Cores2, LogProc2, OS, OSType, SP, OSArch, Office, FullSQL | Sort-Object Computer 
+0

을 [ -2]'를 사용하십시오. $ OS1 = $ OSType.Split (',') [1] $ OS2 = $ OS1.Split ('') [1] "OSType"= $ OS2' – 4c74356b41

답변

1

이 작동합니다 :

은 여기 내 전체 스크립트입니다. 결과를 변수에 저장하고 액세스/필터링하십시오.


편집 : 당신은 다음과 같이 할 수있는, 모든 Get-WmiObject 호출을 제거하려면 다음이`.split를 ('')를 사용하는 것이 더 쉽습니다, 잘

ForEach ($Computer in $Computers) { 
    $win32processor = Invoke-Command -ComputerName $Computer -ScriptBlock { Get-WmiObject –class Win32_processor } 

    $Results += New-Object PSObject -Property @{ 
     "Computer" = $win32processor | select-object -first 1 -ExpandProperty SystemName 
     "CPU1" = $win32processor | Where-Object -FilterScript { ($_.SocketDesignation -like "*1*") -or ($_.SocketDesignation -like "*0*") } | select-object -first 1 -ExpandProperty Name 
    #........ 
+0

'.split ('') -2]'이 훨씬 더 읽기 쉽습니다. – 4c74356b41

+0

지금 사용해보기. 윌 당신이 알려 드리겠습니다. – pinchepooch

+0

@ 4c74356b41에 따라 다릅니다. 정규식에 익숙하다면, 음수 인덱스가있는 분할을 읽는 것이 더 어렵지 않습니다. 또한 정규식을 선호하는 이유는 여기에 정규식을 사용하는 것이 더 신뢰할 수 있기 때문입니다. –