在 Invoke Command 中删除部分 Powershell 结果
Remove part of Powershell Result within Invoke Command
我有以下行:
"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 : Windows 操作系统 - Windows(R) 7, OEM_SLP channel
我在 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
这应该有效:
$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+).*', ''
但是,我强烈建议您在创建对象之前只调用 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
#........
我有以下行:
"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 : Windows 操作系统 - Windows(R) 7, OEM_SLP channel
我在 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
这应该有效:
$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+).*', ''
但是,我强烈建议您在创建对象之前只调用 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
#........