Recently I was asked to pull the software inventory report from multiple severs hosted in Azure, but unfortunately pulling data from WMI not allowed, only PS Remoting was enabled, so I made some changes on my previous script and used Invoke-command to pull the date for all the servers. It was easy and the most effective way to extract that report and only took 5 minute to get the data. 🙂
Here is the modified script, I hope you might find this useful
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | $Credential = Get-Credential Clear-Host $ExportFileLocation = "$env:userprofile\desktop\InvTory.csv" $Servers = Get-Content "$env:userprofile\desktop\servers.txt" Get-Job "Inv*" | Stop-Job Get-Job "Inv*" | Remove-Job $MAXJOB = "20" foreach ($Server in $Servers) { $i++ Write-Progress -Activity "Creating job for Inventory Report.." -Status $Server -PercentComplete (100*$i/($Servers.count)) Invoke-command -Credential $Credential -AsJob -JobName "Inv.$Server" -ComputerName $Server -ScriptBlock {#SB-Start #SB-Start $GetInvs = gwmi win32_product $LocalReport = @() foreach ($GetInv in $GetInvs) { $CName = $args[0] $PName = $GetInv | % {$_.Name} $Version = $GetInv | % {$_.Version} $MyObject = New-Object PSObject -Property @{ ServerName="$CName" AppName="$PName" Version="$Version"} $LocalReport += $MyObject } return $LocalReport #SB-END } -ArgumentList $Server | Out-Null $getRunningJobsCount = (Get-Job "Inv*" | ? {$_.State -eq "Running"}).count while ($getRunningJobsCount -gt $MAXJOB) { Write-Progress -Activity "Reached maximum number of threads ($($MAXJOB))..." -Status "Wait till it gets reduced.." -PercentComplete (100*$i/($Servers.count)) Start-Sleep 10 $getRunningJobsCount = (Get-Job "Inv*" | ? {$_.State -eq "Running"}).count } } $JobStatus = "Please wait" While (Get-Job "Inv*" | ? {$_.State -eq "Running"}) { $CurrentRunningJobs = (Get-Job "Inv*" | ? {$_.State -eq "Running"}).count Write-Progress -Activity "Jobs are running, please wait." -Status "$($CurrentRunningJobs) jobs running" -PercentComplete (100*($i-$CurrentRunningJobs)/$i) #Get-Job | Get-Member #(Get-Job).ChildJobs Clear-Host $c ="." $JobStatus = "$JobStatus $c" $JobStatus Start-Sleep 1 } $Result=@() foreach ($Job in (Get-Job | ? { $_.Name -like "Inv*"})) { $JobResult = $null $JobResult = Receive-Job $Job $Result +=$JobResult Remove-Job $Job } $Result = $Result | select ServerName, AppName, Version $Result | Export-Csv $ExportFileLocation -NoTypeInformation |
Please do let me know in case of any query, happy scripting 🙂
Disclaimer: All posts and opinions on this site are provided AS IS with no warranties. These are our own personal opinions and do not represent our employer’s view in any way.
This article currently have 7,373 views
This work is licensed under a Creative Commons Attribution-NoDerivatives 4.0 International License.
Thanks for the blog. It was very helpful.