2016-11-30 33 views
1

我有以下行:调用命令内删除Powershell的结果的一部分

"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:视窗操作系统 - 视窗(R)7,OEM_SLP通道

我在做这个一个ForEach语句,它工作正常,但我想输出只是:OSType: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命令,则失败。有任何想法吗?

这里是我的整个脚本:

$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

好,它会更容易使用'.split(”“) -2]'而不是你正在使用的构造。 ''OS1 = $ OSType.Split(',')[1] $ OS2 = $ OS1.Split('')[1] “OSType”= $ OS2' – 4c74356b41

回答

1

这应该工作:

$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一次在创建对象之前。只需将结果存储在一个变量中并访问/过滤它即可。


编辑: 摆脱你的所有Get-WmiObject的电话,你可以做这样的事情:

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这取决于。如果您熟悉正则表达式,则不难阅读,然后使用负指数进行拆分。此外,我更喜欢这里的正则表达式,因为如果必须的话可以使它更可靠。 –