I have the following line:
"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} `
That produces: OSType : Windows Operating System - Windows(R) 7, OEM_SLP channel
I am doing this in a ForEach statement and it works fine but I want the output to be just: OSType: OEM_SLP
I can do this to break it apart but then it takes the value from the last computer and puts it in for every entry:
$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
It fails if I try to use the Invoke command on the bottom 3 lines. Any Ideas?
Here is my entire script:
$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
This should work:
$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'
However, I would highly recommend you to invoke Get-WmiObject –class Win32_processor
only once before you creating the object. Just store the result in a variable and access / filter it.
Edit:
To get rid of all your Get-WmiObject
calls, you could do something like this:
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
#........