PowerShell WMI/WQL to get information from SCCM - optimisation - powershell

This is a case where what I have works fine but I can't help but feel there must be a faster way to do this. I'm doubting my structure here. Do you see a faster way this could be done? The foreach takes a while since it has to query many times. The goal is to have an array that shows the computer's ($poste) collections (name and objectpath).
$poste = "p1234"
$SiteCode = "PRX"
$SiteServer = "SSX"
$ResourceID = Get-WmiObject -Namespace root\sms\site_$SiteCode -Computer $SiteServer -Class SMS_R_SYSTEM -Filter "Name='$poste'" |
 Select-Object -ExpandProperty ResourceID
$CollectionIDs = Get-WmiObject -Namespace root\sms\site_$SiteCode -ComputerName $SiteServer -Class sms_fullcollectionmembership -Filter "ResourceID ='$resourceID'" |
 Select-Object -expand CollectionID
foreach ($CollectionID in $CollectionIDs) {
$query = #"
SELECT * FROM SMS_Collection
WHERE SMS_Collection.CollectionID='$CollectionID'
"#
[array]$CollectionNamesPath += Get-WmiObject -Namespace root\sms\site_$SiteCode -Computer $SiteServer -Query $query |
Select-Object name,ObjectPath |
Sort-Object -Property Name
}
$CollectionNamesPath | Out-GridView

This is likely the cleanest version of your existing code:
$poste = "p1234"
$SiteCode = "PRX"
$SiteServer = "SSX"
$ResourceID = Get-WmiObject -Namespace root\sms\site_$SiteCode -ComputerName $SiteServer -Class SMS_R_SYSTEM -Filter "Name='$poste'" -Property ResourceID | Select-Object -ExpandProperty ResourceID
$CollectionIDs = Get-WmiObject -Namespace root\sms\site_$SiteCode -ComputerName $SiteServer -Class sms_fullcollectionmembership -Filter "ResourceID='$resourceID'" -Property CollectionID | Select-Object -ExpandProperty CollectionID
$CollectionNamesPath = foreach ($CollectionID in $CollectionIDs) {
$Query = "Select Name, ObjectPath From SMS_Collection Where SMS_Collection.CollectionID='$CollectionID'"
Get-WmiObject -Namespace root\sms\site_$SiteCode -ComputerName $SiteServer -Query $Query | Select-Object Name, ObjectPath | Sort-Object -Property Name
}
$CollectionNamesPath | Out-GridView
You could also try this, though I'm not sure how well WQL supports it:
$poste = "p1234"
$SiteCode = "PRX"
$SiteServer = "SSX"
$ResourceID = Get-WmiObject -Namespace root\sms\site_$SiteCode -ComputerName $SiteServer -Class SMS_R_SYSTEM -Filter "Name='$poste'" -Property ResourceID | Select-Object -ExpandProperty ResourceID
$CollectionIDs = Get-WmiObject -Namespace root\sms\site_$SiteCode -ComputerName $SiteServer -Class sms_fullcollectionmembership -Filter "ResourceID='$resourceID'" -Property CollectionID | Select-Object -ExpandProperty CollectionID
$Query = "Select CollectionID, Name, ObjectPath From SMS_Collection Where" + $($($CollectionIDs | ForEach-Object { " SMS_Collection.CollectionID='$_' " }) -join 'or')
$CollectionNamesPath = Get-WmiObject -Namespace root\sms\site_$SiteCode -ComputerName $SiteServer -Query $Query |
Sort-Object -Property CollectionID, Name |
Select-Object Name, ObjectPath
$CollectionNamesPath | Out-GridView
You may be able to do something like this:
Get-CimInstance -Namespace root\sms\site_$SiteCode -ComputerName $SiteServer -Class SMS_R_SYSTEM -Filter "Name='$poste'" -KeyOnly |
Get-CimAssociatedInstance -ComputerName $SiteServer -ResultClassName sms_fullcollectionmembership -KeyOnly |
Get-CimAssociatedInstance -ComputerName $SiteServer -ResultClassName SMS_Collection |
Sort-Object -Property CollectionID, Name |
Select-Object Name, ObjectPath
When it works it's fantastic. However, in my experience working with Get-CimAssociatedInstance is an uneven experience at best, doubly so when working on remote computers.

Related

Format output in Powershell

I have a small code in my script that is working well. I'm just annoyed with the output..
My output looks like this:
11.11.111.123
Model
-----
HP ZBook Studio G5
csname : XXXXXXX
LastBootUpTime : 22/Apr/2022 08:10:57
But I want it like this:
IP Address: 11.11.111.123
Model: HP ZBook Studio G5
csname: xxxxx
LastBootUpTime: 22/Apr/2022 08:10:57
This is the script:
Get-WmiObject Win32_NetworkAdapterConfiguration -Computername $pcName |
Where { $_.IPAddress } |
Select -Expand IPAddress |
Where { $_ -like '10.11*' -or $_ -like '10.12*'}
Get-WmiObject -Class Win32_ComputerSystem -Computername $pcName | Select Model
Get-WmiObject win32_operatingsystem -Computername $pcName -ea stop | select csname, #{LABEL='LastBootUpTime';EXPRESSION={$_.ConverttoDateTime($_.lastbootuptime)}} | format-list
Since the output is produced by 3 different classes the way around it is create a new object to merge them:
$IPs = Get-CimInstance Win32_NetworkAdapterConfiguration -ComputerName $pcName |
Where-Object { $_.IPAddress -like '10.11*' -or $_.IPAddress -like '10.12*' }
$Model = (Get-CimInstance -Class Win32_ComputerSystem -ComputerName $pcName).Model
$OS = Get-CimInstance win32_operatingsystem -EA Stop -ComputerName $pcName
[pscustomobject]#{
'IP Address' = $IPs.IpAddress -join ', '
Model = $Model
csname = $OS.CSName
LastBootUpTime = $OS.LastBootUpTime.ToString()
}

Not able to format output as CSV [duplicate]

This question already has an answer here:
Get WMI Data From Multiple Computers and Export to CSV
(1 answer)
Closed 3 years ago.
Am not able to convert PS output to CSV format using echo function. I need to collect hardware information about multiple servers and got this script from internet. I modified it to collect only the necessary information such as Computername,HDD space, CPU details and RAM.
Below is my code:
$ArrComputers = "PC17"
Clear-Host
foreach ($Computer in $ArrComputers) {
$computerSystemRam = Get-WmiObject Win32_ComputerSystem -Computer $Computer |
select #{n="Ram";e={[math]::Round($_.TotalPhysicalMemory/1GB,2)}} |
FT -HideTableHeaders -AutoSize
$computerCPU = Get-WmiObject Win32_Processor -Computer $Computer |
select Name |
FT -HideTableHeaders
$computerCPUCores = Get-WmiObject Win32_Processor -Computer $Computer |
select NumberOfLogicalProcessors |
FT -HideTableHeaders -AutoSize
$computerC = Get-WmiObject -Class Win32_LogicalDisk -Filter "DeviceID= 'C:'" -ComputerName $Computer |
select #{n="Size";e={[math]::Round($_.Size/1GB,2)}} |
FT -HideTableHeaders -AutoSize
$computerD = Get-WmiObject -Class Win32_LogicalDisk -Filter "DeviceID= 'D:'" -ComputerName $Computer |
select #{n="Size";e={[math]::Round($_.Size/1GB,2)}} |
FT -HideTableHeaders -AutoSize
$computerE = Get-WmiObject -Class Win32_LogicalDisk -Filter "DeviceID= 'E:'" -ComputerName $Computer |
select #{n="Size";e={[math]::Round($_.Size/1GB,2)}} |
FT -HideTableHeaders -AutoSize
echo $computer,$computerC,$computerD,$computerE,$computerSystemRam,$computerCPU,$computerCPUCores
}
and my output is coming as
PC17
99.9
12
537.11
15.98
Intel(R) Xeon(R) CPU E5-2630 0 # 2.30GHz
12
What I need is to get this outputs as a comma separated value like below
PC17,99.9,12,537.11,15.98,Intel(R) Xeon(R) CPU E5-2630 0 # 2.30GHz,12
so that I can open it in Excel. Please let me know what the problem here is? Or any other alternative solution to so as to get the output as .csv.
Remove the Format-Table, use ExpandProperty and choose the right property from the array,
Also, I used -f to format the csv, see the differences:
foreach ($Computer in $ArrComputers)
{
$computerSystemRam = get-wmiobject Win32_ComputerSystem -Computer $Computer | select #{n="Ram";e={[math]::Round($_.TotalPhysicalMemory/1GB,2)}}
$computerCPU = get-wmiobject Win32_Processor -Computer $Computer | select -ExpandProperty Name
$computerCPUCores = get-wmiobject Win32_Processor -Computer $Computer | select -ExpandProperty NumberOfLogicalProcessors
$computerC = Get-WmiObject -Class Win32_LogicalDisk -Filter "DeviceID= 'C:'" -ComputerName $Computer | select #{n="Size";e={[math]::Round($_.Size/1GB,2)}}
$computerD = Get-WmiObject -Class Win32_LogicalDisk -Filter "DeviceID= 'D:'" -ComputerName $Computer | select #{n="Size";e={[math]::Round($_.Size/1GB,2)}}
$computerE = Get-WmiObject -Class Win32_LogicalDisk -Filter "DeviceID= 'E:'" -ComputerName $Computer | select #{n="Size";e={[math]::Round($_.Size/1GB,2)}}
"{0},{1},{2},{3},{4},{5},{6}" -f $computer,$computerC.Size,$computerD.Size,$computerE.Size,$computerSystemRam.Ram,$computerCPU,$computerCPUCores
}

How to fix this Powershell script using Get-WmiObject?

I have this piece of code that gets the information from the computers in the domain and outputs to a csv file. I tried to add a new line of code to also grab t he Disk information for the computers but I can't get it working as expected.
# Get the list of all computer names and export to CSV file
Get-ADComputer -Filter * | select Name | Export-Csv -Path 'C:\temp\computers.csv' -NoTypeInformation
# Import the computer names from CSV file and get the system information
$computers = Import-Csv “C:\Temp\computers.csv” | ForEach {
$computerSystem = Get-WmiObject Win32_ComputerSystem -ComputerName $_.Name
$computerOS = Get-WmiObject Win32_OperatingSystem -ComputerName $_.Name
$computerCPU = Get-WmiObject Win32_Processor -ComputerName $_.Name
$computerSN = Get-WmiObject Win32_bios -ComputerName $_.Name | Select-Object SerialNumber
$computerDisk = Get-WmiObject win32_logicaldisk -ComputerName $_.Name | Select-Object DeviceId
[PSCustomObject]#{
'PCName' = $computerSystem.Name
'Model' = $computerSystem.Model
'RAM' = "{0:N2}" -f ($computerSystem.TotalPhysicalMemory/1GB)
'CPU' = $computerCPU.Name
'OS' = $computerOS.caption
'SN' = $computerSN.SerialNumber
'User' = $computerSystem.UserName
'Disk' = $computerDisk.DeviceId | Format-Table DeviceId, MediaType, #{n="Size";e={[math]::Round($_.Size/1GB,2)}},#{n="FreeSpace";e={[math]::Round($_.FreeSpace/1GB,2)}}
}
} | Export-Csv 'C:\Temp\system-info.csv' -NoTypeInformation
This is the line of codes for disk.
$computerDisk = Get-WmiObject win32_logicaldisk -ComputerName $_.Name | Select-Object DeviceId
And...
'Disk' = $computerDisk.DeviceId | Format-Table DeviceId, MediaType, #{n="Size";e={[math]::Round($_.Size/1GB,2)}},#{n="FreeSpace";e={[math]::Round($_.FreeSpace/1GB,2)}}
The other parameters work but only the disk info section isn't working. the output is: System.Object[] instead of displaying the info .
This has worked or me somewhat but it only grabs the info for the first drive. Also, the free space is larger than the Disk size which is weird.
$Computers = Import-Csv 'C:\Temp\computers.csv'
$Computers | ForEach {
$computerSystem = Get-WmiObject Win32_ComputerSystem -ComputerName $_.Name
$computerOS = Get-WmiObject Win32_OperatingSystem -ComputerName $_.Name
$computerCPU = Get-WmiObject Win32_Processor -ComputerName $_.Name
$computerSN = Get-WmiObject Win32_bios -ComputerName $_.Name | Select-Object SerialNumber
$computerDisk = Get-WmiObject win32_logicaldisk -ComputerName $_.Name | Select-Object DeviceId, Size, FreeSpace
[PSCustomObject]#{
'PCName' = $computerSystem.Name
'Model' = $computerSystem.Model
'RAM' = "{0:N2}" -f ($computerSystem.TotalPhysicalMemory/1GB)
'CPU' = $computerCPU.Name
'OS' = $computerOS.caption
'SN' = $computerSN.SerialNumber
'User' = $computerSystem.UserName
'Disk' = $computerDisk.DeviceId | Format-Table | Out-String
'Size' = $computerDisk.Size | Format-Table | Out-String
'Free Space' = $computerDisk.FreeSpace | Format-Table | Out-String
}
} | Export-Csv 'C:\Temp\system-info.csv' -NoTypeInformation
You can do this to get the result I think you're after:
$Computers = Import-Csv 'C:\Temp\computers.csv'
$Computers | ForEach {
$computerSystem = Get-WmiObject Win32_ComputerSystem -ComputerName $_.Name
$computerOS = Get-WmiObject Win32_OperatingSystem -ComputerName $_.Name
$computerCPU = Get-WmiObject Win32_Processor -ComputerName $_.Name
$computerSN = Get-WmiObject Win32_bios -ComputerName $_.Name | Select-Object SerialNumber
$computerDisk = Get-WmiObject win32_logicaldisk -ComputerName $_.Name | Select-Object DeviceId, MediaType, #{n="Size";e={[math]::Round($_.Size/1GB,2)}},#{n="FreeSpace";e={[math]::Round($_.FreeSpace/1GB,2)}}
[PSCustomObject]#{
'PCName' = $computerSystem.Name
'Model' = $computerSystem.Model
'RAM' = "{0:N2}" -f ($computerSystem.TotalPhysicalMemory/1GB)
'CPU' = $computerCPU.Name
'OS' = $computerOS.caption
'SN' = $computerSN.SerialNumber
'User' = $computerSystem.UserName
'Disk' = $computerDisk | Format-Table | Out-String
}
} | Export-Csv 'C:\Temp\system-info.csv' -NoTypeInformation
The first problem was that in the $computerDisk = line you were using Select-Object to return only the DeviceID property, but then were later trying to use the other properties.
The second problem was that you need to pipe Format-Table to Out-String when you output it to convert it to string format so that Export-CSV doesn't treat it as an object.

Getting System.Object[] output in powershell

I wrote this quick script to retrieve information about a bunch of servers. When I run on my windows 7 (ps v2) host I get all the correct results. However, When I run on Server 2008 r2 (ps v2) I get System.Object[] for all the queries below. I have a bunch of other queries as well but they all work fine, just these ones I am getting this problem. Whats going on?
$ArrComputers = "localhost"
$OutputLog = ".\output.csv"
$NotRespondingLog = ".\notresponding.txt"
$ErrorActionPreference = "Stop"
Clear-Host
$data = ForEach ($Computer in $ArrComputers) {
try{
$ipAdd = (Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter IPEnabled=TRUE -ComputerName .)| select ipaddress
$MacAdd = (Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter IPEnabled=TRUE -ComputerName .)| Select MacAddress
$DefGateway = (Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter IPEnabled=TRUE -ComputerName .)| Select DefaultIPGateway
$DNSServ = (Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter IPEnabled=TRUE -ComputerName .)| Select DNSServerSearchOrder
$CPUname = (Get-WmiObject –class Win32_processor -ComputerName .)| Select name
$processorinfo = (Get-WmiObject –class Win32_processor -ComputerName .)| Select NumberOfCores
$processorinfo2 = (Get-WmiObject –class Win32_processor -ComputerName .)| Select NumberOfLogicalProcessors
$memory = Get-WMIObject -class Win32_PhysicalMemory -ComputerName $Computer |
Measure-Object -Property capacity -Sum |
select #{N="r"; E={[math]::round(($_.Sum / 1GB),2)}}
}catch{
$Computer | Out-File -FilePath $NotRespondingLog -Append -Encoding UTF8
continue
}
$props = #{
'IPAddress' = $ipAdd
'MacAddress' = $MacAdd
'DefaultIPGateway'= $DefGateway
'DNSServerSearchOrder' = $DNSServ
'cpuName' = $CPUname
'Cores' = $processorinfo
'logicalcores' = $processorinfo2
' Memory' = $memory
}
New-object -type PSCustomObject -Property $Props
}
$Data | export-csv -notypeinformation $outputlog
So the issue what you are facing: Powershell is returning the $data as Key=Value or hashtable format but as an object. So when you are inserting the same as CSV , then it is returning it as Object. So what you can do is you can convert the data to JSON format and you can insert the same. Else you can use Arraylist and insert all the values there. In that case it will accept the key-value pair mapping.
Hope it helps
I have removed the headers from the select query and created an array list with the custom object created in the loop and it will add each details from server to server and will append in the array list separately. I hope this helps you.
$ArrComputers = "localhost"
$OutputLog = ".\output.csv"
$NotRespondingLog = ".\notresponding.txt"
$ErrorActionPreference = "Stop"
Clear-Host
$Global:arraylist= New-Object System.Collections.ArrayList
$data = ForEach ($Computer in $ArrComputers) {
try{
$ipAdd = (Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter IPEnabled=TRUE -ComputerName $Computer)| select ipaddress
$MacAdd = (Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter IPEnabled=TRUE -ComputerName $Computer)| Select MacAddress
$DefGateway = (Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter IPEnabled=TRUE -ComputerName $Computer)| Select DefaultIPGateway
$DNSServ = (Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter IPEnabled=TRUE -ComputerName $Computer)| Select DNSServerSearchOrder
$CPUname = (Get-WmiObject –class Win32_processor -ComputerName $Computer)| Select name
$processorinfo = (Get-WmiObject –class Win32_processor -ComputerName $Computer)| Select NumberOfCores
$processorinfo2 = (Get-WmiObject –class Win32_processor -ComputerName $Computer)| Select NumberOfLogicalProcessors
$memory = Get-WMIObject -class Win32_PhysicalMemory -ComputerName $Computer |
Measure-Object -Property capacity -Sum |
select #{N="r"; E={[math]::round(($_.Sum / 1GB),2)}}
$props =[PSCustomObject]#{
'IPAddress' = $ipAdd.ipaddress[0]
'MacAddress' = $MacAdd.MacAddress
'DefaultIPGateway'= $DefGateway.DefaultIPGateway[0]
'DNSServerSearchOrder' = $DNSServ.DNSServerSearchOrder[0]
'cpuName' = $CPUname.name
'Cores' = $processorinfo.NumberOfCores
'logicalcores' = $processorinfo2.NumberOfLogicalProcessors
' Memory' = $memory.r
}
$arraylist.Add($props)
}catch{
$Computer | Out-File -FilePath $NotRespondingLog -Append -Encoding UTF8
continue
}
}
$arraylist | Export-Csv -NoTypeInformation $OutputLog -Force

Querying partitions/drives on a remote server with WMI

I do the following to check for local drives/partitions on a remote computer:
Get-WmiObject -Class Win32_Share -ComputerName SERVERNAME -Filter "Description='Default share'"
but the command also returns CD-roms etc.
Is there a command to only return disk/partitions?
Get-WmiObject -Class Win32_LogicalDisk -Filter "DriveType=3" |
Foreach-Object {$_.DeviceID}
Try this:
Get-WMIObject Win32_DiskPartition -computername remotecomp |
ForEach-Object {
$info = #{}
$info.Disk = $_.DiskIndex
$info.Partition = $_.Index
$info.DriveLetter = $_.psbase.GetRelated('Win32_LogicalDisk') |
Select-Object -ExpandProperty DeviceID
New-Object PSObject -Property $info
}
$info # contains partions number and unit letter as hashtable
Get-WmiObject -query "Select * from Win32_DiskPartition" ... maybe?