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
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
$ErrorActionPreference = 'SilentlyContinue'
$ComputerName =Get-ADComputer -Filter {(Name -like "*")} -SearchBase "OU=AsiaPacific,OU=Sales,OU=UserAccounts,DC=FABRIKAM,DC=COM" | Select-Object -ExpandProperty Name
$results = #{}
ForEach ($computer in $ComputerName) {
$Results += Get-NetAdapter -CimSession $ComputerName | Select-Object PsComputerName, InterfaceAlias, Status, MacAddress
}
$results | Export-csv -path C\users\bret.hooker\desktop\macaddress.csv -Append
Please note the base and filter are just examples and not the actual code due to work place confidentiality. Code currently will pull from AD all computer name, then will run the ForEach command to get the NetAdapter Information. I am unable to get it to output to the CSV file however. Any advice would be great.
My recommendations are 1) don't continuously append objects to an array, 2) avoid the -Append parameter of Export-Csv, and 3) take advantage of the pipeline. Example:
$computerNames = Get-ADComputer -Filter * -SearchBase "OU=AsiaPacific,OU=Sales,OU=UserAccounts,DC=FABRIKAM,DC=COM" | Select-Object -ExpandProperty Name
$computerNames | ForEach-Object {
Get-NetAdapter -CimSession $_ | Select-Object PSComputerName,InterfaceAlias,Status,MACAddress
} | Export-Csv "C\users\bret.hooker\desktop\macaddress.csv" -NoTypeInformation
I have the following PowerShell script which allows me to collect information about disks & volumes on Windows servers of the domain where the script is launched:
$ErrorActionPreference = 'SilentlyContinue'
Get-ADComputer -Filter 'OperatingSystem -like "*Server*"' -Properties * |
Select-Object Name |
ForEach-Object {
if (Test-Connection $_.Name -Count 1) {
Get-WmiObject -Class Win32_LogicalDisk -ComputerName $_.Name -Filter "DriveType=3" |
Select-Object PSComputerName, DeviceID,
#{Name="Size /GB";Expression={[math]::Round($($_.Size / 1GB), 2)}},
#{Name="Free /GB";Expression={[math]::Round($($_.Freespace / 1GB), 2)}},
#{Name="Free %";Expression={[math]::Round($($_.Freespace/$_.Size)*100, 1)}}
} else {
Write-Nost $_.Name " Connection Error"
}
} |
sort PSComputerName |
Format-Table -AutoSize
I get the following result:
SRV01 Connection Error
SRV02 Connection Error
PSComputerName DeviceID Size /GB Free /GB Free %
-------------- -------- ------------ --------- -------
SERVER03 C: 125,51 105,59 84,1
SERVER04 C: 24,83 7,38 29,7
SERVER05 E: 14,65 7,36 50,2
SERVER06 C: 49,66 29,28 59
I want to add an additional column with the OS for each server.
I would like this column to be in second position, after the "PSComputerName" column. How can I get this result?
I think I use a nested command by adding a Get-WmiObject Win32_OperatingSystem | Select-Object caption in the Get-WmiObject -Class Win32_LogicalDisk ..., but I don't know which syntax to use and how to imbricate a command in another command.
Don't use -properties *... it's going to retrieve every single populated property which you don't need in this script.
Get-ADComputer has an operatingsystem property.
Not tested:
Get-ADComputer -Filter 'OperatingSystem -like "*Server*"' -Properties OperatingSystem | ForEach-Object {
$OS = $_.OperatingSystem
If (Test-Connection $_.Name -Count 1 -Quiet){
Get-WmiObject -Class win32_logicalDisk -ComputerName $_.Name -Filter "DriveType=3" |
Select-Object pscomputername, #{Name="OS";Expression={$OS}} ,DeviceID,
#{Name="Size /GB";Expression={[math]::Round($($_.size / 1GB), 2)}},
#{Name="Free /GB";Expression={[math]::Round($($_.freespace / 1GB), 2)}},
#{Name="Free %";Expression={[math]::Round($($_.Freespace/$_.Size)*100, 1)}}
}
else {
Write-host $_.Name " Connection Error"
}
} | sort pscomputername | Format-Table -AutoSize
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
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.