Skip to content

Instantly share code, notes, and snippets.

@Willifreth
Created October 22, 2019 13:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Willifreth/2edbd3dd036c2f5684e309c050dd88a7 to your computer and use it in GitHub Desktop.
Save Willifreth/2edbd3dd036c2f5684e309c050dd88a7 to your computer and use it in GitHub Desktop.
Query Specific Installed Software on remote PC
Function Get-CompInventory{
<#
.SYNOPSIS
Gets information about one or more computers and populates a spreadsheet
.DESCRIPTION
Queries WMI objects on one or more systems and uses that information to populate an excel spreadsheet. Gets Machine Name,
OS Version, Manufacturer, Model, Domain Name, Serial Number, CPU Type, and software versions of Firefox, Java, Flash, and Adobe Reader
Script prompts for credentials, which can be local or domain admin. PSRemoting must be enabled on all machines where the query is run.
.PARAMETER compName
The name of the computer or computers that you want to get information about.
.EXAMPLE
Get-CompInventory -compName comp1, comp2, comp3
Gets inventory information for comp1, comp2, and comp3
.Example
Get-Content c:\computers.txt | Get-CompInventory
Gets inventory information for the list of computers in c:\computers.txt
.Example
(Get-ADComputer -filter {name -like "Server*"}).name | Get-CompInventory
Query Active Directory for computers who's name begin with server and passes those names to Get-CompInventory
.Notes
Versioning:
1.0 - First working file
1.1 - Added support for 64 bit operating systems
2.0 - Added dynamic input for program lookups
#>
[CmdletBinding()]
Param(
[Parameter(Mandatory=$True, valuefrompipeline=$true)]
[String[]]$compName)
Begin {
$date = Get-Date
$ErrorActionPreference = "Silentlycontinue"
Write-Host "Please login with your admin account"
$cred = Get-Credential
$arrayposition = -1
[Array]$program = @()
do {
$arrayposition++
$program += Read-Host "Specify program name keywords to search for (blank line to stop)"
} while ($program[$arrayposition] -ne "")
$arrayposition = 0
$excelColumn = 8
$excel = New-Object -ComObject Excel.Application
$excel.Visible = $true
$excelbook = $excel.Workbooks.Add()
$excelItem = $excelbook.Worksheets.Item(1)
$excelItem.Name = "Inventory on " + $date.Month + "-" + $date.Day + "-" + $Date.Year
$excelItem.Cells.Item(1,1) = "Machine Name"
$excelItem.Cells.Item(1,2) = "OS version"
$excelItem.Cells.Item(1,3) = "Manufacturer"
$excelItem.Cells.Item(1,4) = "Model"
$excelItem.Cells.Item(1,5) = "Domain Name"
$excelItem.Cells.Item(1,6) = "Serial Number"
$excelItem.Cells.Item(1,7) = "CPU Type"
Foreach ($name in $program) {
$excelItem.Cells.Item(1,$excelColumn) = $program[$arrayposition]
$arrayposition++
$excelColumn++
}
$range = $excelItem.UsedRange
$range.Font.Bold = $True
$range.EntireColumn.AutoFit()
[int]$excelRow = 2
}
process {
foreach ($comp in $compName) {
Try {
$OSinfo = Get-WmiObject -ComputerName $comp -Credential $cred -ErrorAction stop -Class win32_operatingsystem
$diskInfo = Get-WmiObject -ComputerName $comp -Credential $cred -Class win32_logicaldisk
$compInfo = Get-WmiObject -ComputerName $comp -Credential $cred -Class win32_computersystem
$biosInfo = Get-WmiObject -ComputerName $comp -Credential $cred -Class win32_bios
$CPUInfo = Get-WmiObject -ComputerName $comp -Credential $cred -Class win32_Processor
$machineName = $compInfo.Name
$OSversion = $OSInfo.caption + " " + $OSinfo.OSArchitecture
$Manuf = $biosInfo.Manufacturer
$Model = $compInfo.Model
$Domain = $compInfo.Domain
$Serial = $biosInfo.SerialNumber
$CPU = $CPUInfo.Name
$excelItem.Cells.Item($excelRow,1) = $machineName
$excelItem.Cells.Item($excelRow,2) = $OSversion
$excelItem.Cells.Item($excelRow,3) = $Manuf
$excelItem.Cells.Item($excelRow,4) = $Model
$excelItem.Cells.Item($excelRow,5) = $Domain
$excelItem.Cells.Item($excelRow,6) = $Serial
$excelItem.Cells.Item($excelRow,7) = $CPU
$excelColumn = 8
foreach ($name in $program) {
$RegProgram = Invoke-Command -Credential $cred -ComputerName $comp -ScriptBlock {
$RegBase = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey([Microsoft.Win32.RegistryHive]::LocalMachine,$env:COMPUTERNAME)
If ((Get-WmiObject win32_operatingsystem).OSArchitecture -contains '64-bit') {
$regvar = ('SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\*','SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*')
} Else {
$regvar = ('SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\*')
}
$RegUninstall = $RegBase.OpenSubKey($regvar)
$RegUninstall.GetSubKeyNames() | ForEach-Object {
$DisplayName = ($RegBase.OpenSubKey($regvar + "\$_")).GetValue('DisplayName')
if ($DisplayName -like "*" + $using:name + "*") {
New-Object -TypeName PSCustomObject -Property @{
ComputerName = $env:COMPUTERNAME
ProgramName = $DisplayName
}
}
}
}
If ($RegProgram -ne $null ) {
$excelItem.Cells.Item($excelRow, $excelColumn) = $RegProgram.ProgramName
} Else {
$excelItem.Cells.Item($excelRow, $excelColumn) = "Not Installed"
}
$excelColumn++
}
$excelRow++
} Catch {
Write-host "Error connecting to $compname, please check your credentials and power state of the remote system" -ForegroundColor red
}
}
}
End {
$range.EntireColumn.AutoFit()
Write-Verbose "Computer Inventory lookup complete"
$savePath = Read-Host "Specify a path to save your spreadsheet"
$excel.ActiveWorkbook._SaveAs($savePath)
$excel.quit()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment