Reputation: 21
I am trying to write a powershell script that will Ping each Hostname in an OU in AD, Output a txt file of online hostnames, then using the Get-WmiObject to get a list of software on each online systems. I've tried and I can't seem to figure out how to output the pinged systems to a txt file of hostnames so I can use that file to create the software list. I know pretty much nothing about Powershell, so I've been trying to steal and borrow, but can't figure out how to make it all work. Here is what I've tried. Any help will be greatly appreciated.
Import-Module active*
$rtn = $null
Get-ADComputer -Filter * -Properties * -searchbase "OU=EnterOUstructurehere" |
ForEach-Object {
$rtn = Test-Connection -CN $_.dnshostname -Count 1 -BufferSize 16 -Quiet
IF($rtn -match ‘True’) {write-host -ForegroundColor green $_.dnshostname}
ELSE { Write-host -ForegroundColor red $_.dnshostname }
}
Using Out-file to output a txt file called computers. Use Get to to pull from the computer.txt file and then:
ForEach-Object {Get-WmiObject -Class Win32_Product -Computer $_.Name} | Select-Object Name, Version | Sort-Object Name | Export-CSV "C:\Users\steven.e.hendricks\OneDrive - US Army\Desktop\Software Inventory\SoftwareInventoryfile.csv" -Append -NoTypeInformation
Upvotes: 2
Views: 674
Reputation: 440037
First, the obligatory recommendation:
The CIM cmdlets (e.g., Get-CimInstance
) superseded the WMI cmdlets (e.g., Get-WmiObject
) in PowerShell v3 (released in September 2012). Therefore, the WMI cmdlets should be avoided, not least because PowerShell (Core) v6+, where all future effort will go, doesn't even have them anymore. Note that WMI still underlies the CIM cmdlets, however. For more information, see this answer.
Note, however, that the CIM cmdlets use the same remoting infrastructure as PowerShell's remoting, which differs from that of the WMI cmdlets.
You're probably looking for something like this:
Get-ADComputer -Filter * -Properties * -searchbase "OU=EnterOUstructurehere" |
Where-Object { Test-Connection -CN $_.dnshostname -Count 1 -BufferSize 16 -Quiet } |
ForEach-Object {
Get-WmiObject -Class Win32_Product -Computer $_.Name |
Select-Object Name, Version |
Sort-Object Name
} |
Export-Csv -NoTypeInformation "C:\path\to\SoftwareInventoryfile.csv"
However, note that you my want to add another property that identifies the computer name in each output object.
Also, you can speed up the operation with parallel processing, by passing an array of computer names to a single Get-WmiObject
call:
$onlineComputers =
Get-ADComputer -Filter * -Properties * -searchbase "OU=EnterOUstructurehere" |
Where-Object { Test-Connection -CN $_.dnshostname -Count 1 -BufferSize 16 -Quiet }
Get-WmiObject -Class Win32_Product -Computer $onlineComputers.Name |
Select-Object PSComputerName, Name, Version |
Sort-Object PSComputerName, Name |
Export-Csv -NoTypeInformation "C:\path\to\SoftwareInventoryfile.csv"
Upvotes: 2