PS newbe here...
How do I get the remote computer name to appear in the output?
$computer = "PC3090-121","APCD02"
Get-WmiObject Win32_Printer -ComputerName $computer |
Select-Object SystemName,Name,Local |
Format-Table -AutoSize
I've tried including -computername, computername, %computername% in the Select and format-table -properties - no joy...
My searches have come up empty, or I couldn't understand them.
------------------------------ answer:
$computer = "PC3090-121","APCD02"
Get-WmiObject Win32_Printer -ComputerName $computer |
Select-Object __Server, Name, Local |
Format-Table -AutoSize
How about simply
Get-WmiObject Win32_Printer -ComputerName $computer |
Select-Object SystemName,Name,Local |
Format-Table -AutoSize
There is no computername property on the resulting object, nor is there a %computername% property. What exists is SystemName.
Related
Get-WmiObject -Class Win32_OperatingSystem -ComputerName (Get-Content "C:\Temp\Servers.txt") | SELECT-Object PSComputerName, #{Name="Memory (RAM in GB)";Expression={[Math]::Round($_.TotalVisibleMemorySize/1024/1024)}} | Format-Table
Get-WmiObject -Class Win32_logicaldisk -ComputerName (Get-Content "C:\Temp\Servers.txt") | Select-Object PSComputerName, DriveType, DeviceID, VolumeName, #{Name="Size";Expression={[math]::ceiling($_.Size /1GB)}} , #{Name="FreeSpace";Expression={[math]::ceiling($_.FreeSpace /1GB)}}, Compressed | where DriveType -eq 3 | Format-Table
Get-WmiObject -Class Win32_OperatingSystem -ComputerName (Get-Content "C:\Temp\Servers.txt")| Select-Object PSComputerName, BuildNumber, BuildType, Caption, CodeSet, OSArchitecture, SystemDrive, TotalVisibleMemorySize, Version | Format-Table
Get-WmiObject -Class win32_product -ComputerName (Get-Content "C:\Temp\Servers.txt") | Select-Object Name, Version, Vendor, InstallDate | Format-Table
Get-WmiObject -Class Win32_Service -ComputerName (Get-Content "C:\Temp\Servers.txt") | Select-Object PSComputerName, DisplayName, StartName, PathName, StartMode| where DisplayName -Like "*xyz*" |Format-Table
I have till now managed to piece together the above to get the information I need from serveral servers, however now I want to format it so that I can collate information for each server in a format that I can display
for eg.
Server : ABC
RAM : 64 GB
Number of Processors : 8
Disk :
Table of disk Sizes Etc
Any pointers would be appreciated
With all these properties, you would get a nested object array, which probably is easiest to view in JSON format.
I have changed all Get-WmiObject into the newer and faster Get-CimInstance cmdlets below
$result = Get-Content "C:\Temp\Servers.txt" | ForEach-Object {
# create an ordered hashtable to store the results for each server
$pcinfo = [ordered]#{}
# System info
$data = Get-CimInstance -ClassName Win32_ComputerSystem -ComputerName $_
$pcinfo['Computer'] = $data.PSComputerName
$pcinfo['Memory (RAM in GB)'] = '{0:N2}' -f ($data.TotalPhysicalMemory / 1GB)
# OS info
$data = Get-CimInstance -ClassName Win32_OperatingSystem -ComputerName $_
$pcinfo['BuildNumber'] = $data.BuildNumber
$pcinfo['BuildType'] = $data.BuildType
$pcinfo['Caption'] = $data.Caption
$pcinfo['CodeSet'] = $data.CodeSet
$pcinfo['OSArchitecture'] = $data.OSArchitecture
$pcinfo['SystemDrive'] = $data.SystemDrive
$pcinfo['TotalVisibleMemorySize'] = $data.TotalVisibleMemorySize
$pcinfo['Version'] = $data.Version
# Product info (array of objects)
$pcinfo['Products'] = Get-CimInstance -ClassName Win32_Product -ComputerName $_ |
Select-Object Name, Version, Vendor, InstallDate
# Local fixed disk info (array of objects)
$pcinfo['FixedDrives'] = Get-CimInstance -ClassName Win32_LogicalDisk -ComputerName $_ -Filter 'DriveType=3' |
Sort-Object DeviceID |
Select-Object DriveType, DeviceID, VolumeName,
#{Name="Size";Expression={"{0:N2} GB" -f ($_.Size / 1GB)}},
#{Name="FreeSpace";Expression={"{0:N2} GB" -f ($_.FreeSpace / 1GB)}},
Compressed
# Services info (array of objects)
$pcinfo['Services'] = Get-CimInstance -ClassName Win32_Service -ComputerName $_ |
Where-Object { $_.DisplayName -like '*Adobe*' } |
Select-Object DisplayName, StartName, PathName, StartMode
# convert the hashtable to PSObject and output
[PsCustomObject]$pcinfo
}
# output the whole structure as JSON for easier reading and optionally save it to file
$result | ConvertTo-Json -Depth 3 # | Set-Content -Path 'Path\To\Output.json' -Force
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
}
I'm a super-noob when it comes to powershell. I've been able to extract the WMIobject win32_LogicalDiskĀ info except for the ADComputer identity info. See my code and columns needed to populate. I keep getting a blank under user. Any thoughts?
$exportPath = "\\Server01\users\ohyeah\Downloads\testfolder"
$computers = Get-Content "\\Server01\users\ohyeah\Downloads\testfolder\computers.txt"
$driveinfo = Get-WMIobject win32_LogicalDisk -ComputerName $computers -filter "DriveType=3" | Select-Object SystemName, DeviceID, VolumeName,
#{Name="Size_GB"; Expression={"{0:N1}" -f($_.size/1gb)}},
#{Name="FreeSpace_GB"; Expression={"{0:N1}" -f($_.freespace/1gb)}},
#{Name="%_FreeSpace_GB"; Expression={"{0:N2}%" -f(($_.freespace/$_.size)*100)}},
#{Name="User"; Expression={$(Get-ADComputer -identity $_ -Properties Description | ft -a Description)}},
#{Name="Date"; Expression={$(Get-Date -format 'g')}}
$driveinfo | Out-GridView
$driveinfo | Format-Table -AutoSize
$driveinfo | Export-Csv "$exportPath\test.csv" -NoTypeInformation -NoClobber -Append
SystemName DeviceID VolumeName Size_GB FreeSpace_GB %_FreeSpace_GB User Date
You already have the computer name, via the SystemName property. Just pass that in directly.
You also cannot use that Format-Table in that calculated property that way, especially if all you are asking for is a single field property which is nothing but a note field.
Lastly, unless that description field is populated, it will be empty, also using that Description property is not part of the default list, you have to ask for it asking for all properties and then specific property.
Running this on a local domain joined host...
# This will give you the data in the Description property
Clear-Host
$computers = $env:COMPUTERNAME
$driveinfo = Get-WMIobject win32_LogicalDisk -ComputerName $computers -filter "DriveType=3" |
Select-Object SystemName, DeviceID, VolumeName,
#{Name="Size_GB"; Expression={"{0:N1}" -f($_.size/1gb)}},
#{Name="FreeSpace_GB"; Expression={"{0:N1}" -f($_.freespace/1gb)}},
#{Name="%_FreeSpace_GB"; Expression={"{0:N2}%" -f(($_.freespace/$_.size)*100)}},
#{Name="User"; Expression={$(Get-ADComputer -identity $_.SystemName -Properties *).Description}},
#{Name="Date"; Expression={$(Get-Date -format 'g')}}
$driveinfo | Out-GridView
# This will give you the full DN of the computer object
Clear-Host
$computers = $env:COMPUTERNAME
$driveinfo = Get-WMIobject win32_LogicalDisk -ComputerName $computers -filter "DriveType=3" |
Select-Object SystemName, DeviceID, VolumeName,
#{Name="Size_GB"; Expression={"{0:N1}" -f($_.size/1gb)}},
#{Name="FreeSpace_GB"; Expression={"{0:N1}" -f($_.freespace/1gb)}},
#{Name="%_FreeSpace_GB"; Expression={"{0:N2}%" -f(($_.freespace/$_.size)*100)}},
#{Name="User"; Expression={$(Get-ADComputer -identity $_.SystemName -Properties Description)}},
#{Name="Date"; Expression={$(Get-Date -format 'g')}}
$driveinfo | Out-GridView
I have a small script to get information about a network computer in my domain but during the execution the script omits two lines.
$HostName = Read-Host 'Veuillez donner le nom du poste que vous voulez verifier?'
If (Test-Connection $HostName -count 4 -quiet)
{
Write 'The host responded';
gwmi win32_volume -ComputerName $HostName -Filter 'drivetype = 3' | select driveletter, label, #{LABEL='GBfreespace';EXPRESSION={"{0:N2}" -f ($_.freespace/1GB)} };
Get-WmiObject Win32_OperatingSystem -ComputerName $HostName | Select-Object PSComputerName, Description, OSArchitecture;
Get-ADComputer $HostName | select DistinguishedName;
Get-WmiObject -Class Win32_Product -Computer $HostName | Format-Table -Property Name, Version, InstallDate, Vendor;
}
else
{
Write 'The host doesn''t responded';
}
The problem does not seems to be in the commands (I get the desired result by typing them directly in the console), but in the position of the command.
I tried to invert the order of the commands and the result was always like this:
After the if statement:
the two first commands are displayed
the 3rd and the 4th are omitted
the last one is again displayed
I also tried it like this:
If (Test-Connection $HostName -count 4 -quiet)
{
Get-WmiObject Win32_OperatingSystem -ComputerName $HostName | Select-Object PSComputerName, Description, OSArchitecture;
Get-ADComputer $HostName | select DistinguishedName;
Get-ADComputer $HostName | select DistinguishedName;
Get-WmiObject Win32_OperatingSystem -ComputerName $HostName | Select-Object PSComputerName, Description, OSArchitecture;
Get-ADComputer $HostName | select DistinguishedName;
Write 'The host responded';
gwmi win32_volume -ComputerName $HostName -Filter 'drivetype = 3' | select driveletter, label, #{LABEL='GBfreespace';EXPRESSION={"{0:N2}" -f ($_.freespace/1GB)} };
Get-WmiObject -Class Win32_Product -Computer $HostName | Format-Table -Property Name, Version, InstallDate, Vendor;
}
After the if statement:
the command 1 is displayed
the commands 2 and 3 are omitted
the 4th command is displayed
the 5th command is again omitted
the 6th is displayed
the 7th is omitted and finally the 8th is displayed
I'm trying to pull a machine's IPAddress, MACAddress, and DefaultIPGateway information from the Win32_NetworkAdapterConfiguration object into an exported CSV file named NetworkAdapterConfiguration.csv using this script:
$StrComputer = $env:computername
$NetAdConfig = gwmi Win32_NetworkAdapterConfiguration -Comp $StrComputer
$NetAdConfig | Select-Object IPAddress,MACAddress,DefaultIPGateway | Export-Csv -Path C:\CSVFolder\NetworkAdapterConfiguration.csv -Encoding ascii -NoTypeInformation
When I view this CSV I get "System.String[]" where the IP and DefaultIPGateway values should be displayed. I'm assuming this information gets represented as an array and that is why I'm seeing the System.String[] view, but I have little experience with Powershell. Any help, advice, and references are much appreciated.
The IPAddress and DefaultIPGateway properties are arrays. If you are sure your machines only have one IP address and default gateway, you can do this:
$computer = $ENV:COMPUTERNAME
get-wmiobject Win32_NetworkAdapterConfiguration -filter "IPEnabled=TRUE" -computername $computer | foreach-object {
new-object PSObject -property #{
"Computer" = $computer
"MACAddress" = $_.MACAddress
"IPAddress" = $_.IPAddress[0]
"DefaultIPGateway" = $_.DefaultIPGateway[0]
} | select-object Computer,MACAddress,IPAddress,DefaultIPGateway
}
Here's another way that uses Select-Object:
$computer = $ENV:COMPUTERNAME
get-wmiobject Win32_NetworkAdapterConfiguration -filter "IPEnabled=TRUE" -computername $computer | foreach-object {
$_ | select-object `
#{Name="ComputerName"; Expression={$_.__SERVER}},
#{Name="MACAddress"; Expression={$_.MACAddress}},
#{Name="IPAddress"; Expression={$_.IPAddress[0]}},
#{Name="DefaultIPGateway"; Expression={$_.DefaultIPGateway[0]}}
}
I have a function that I wrote called Convert-OutputForCSV that can help to remove the string[] issues you are seeing as well. You could do something like this to expand out the arrays into a more readable property.
$StrComputer = $env:computername
$NetAdConfig = gwmi Win32_NetworkAdapterConfiguration -Comp $StrComputer
$NetAdConfig | Select-Object IPAddress,MACAddress,DefaultIPGateway |
Convert-OutputForCSV |
Export-Csv -Path C:\CSVFolder\NetworkAdapterConfiguration.csv -Encoding ascii -NoTypeInformation