Powershell – System Data Gathering Script

Powershell – System Data Gathering Script

I’ve been meaning to get something like this together for a while, so it will be an always evolving script.

Currently it outputs to a single line in a CSV per machine, but I hope to change this to give the option of outputting to a HTML page/report.

Already I have included a Wishlist of futures options I’d like to include.

[powershell]
<#
### Wishlist
————
### 20.05.2020 – NOK

Patch Level/hotfixs installed
Central CSV for collating info?
Backup of Registry? May not be required for this level.

#>

Push-Location (Split-Path -path $MyInvocation.MyCommand.Definition -Parent)

# Cutting out manual input of file names for local running.
<#Param(
[Parameter(Mandatory=$true, position=0)][string]$infile,
[Parameter(Mandatory=$true, position=1)][string]$outfile
)#>

$infile = "servers.csv"
$outfile = "$($env:computername)_Output.csv"
$ServicesFile = "$($env:computername)_Services.csv"
$AddRemoveProgramsFile = "$($env:computername)_AddRemovePrograms.csv"
$SchedTaskFile = "$($env:computername)_SchedTask.csv"

#Column header in input CSV file that contains the host name
$ColumnHeader = "ComputerName"
$HostList = import-csv $infile | select-object $ColumnHeader
$out = @()

foreach($object in $HostList) {

$os = Get-CIMInstance -computername $object.("ComputerName") -class win32_operatingsystem
$vol = Get-CIMInstance -computername $object.("ComputerName") -class Win32_Volume
$siz = Get-CIMInstance -computername $object.("computername") -class Win32_LogicalDisk | where-object {$_.DriveType -eq "3"}
$net = Get-CIMInstance -computername $object.("ComputerName") -class Win32_NetworkAdapterConfiguration | where-object { $_.IPAddress -ne $null }
$cmp = Get-CIMInstance -computername $object.("ComputerName") -class Win32_ComputerSystem
$stg = Get-CIMInstance -ComputerName $object.("Computername") -class Win32_SystemEnclosure
$far = Get-WindowsFeature -ComputerName $object.("ComputerName") | Where-Object Installed
$freespace = $siz.FreeSpace | %{[math]::round($_/1GB)}
$capacity = $siz.size | %{[math]::round($_/1GB)}
$MShypervisor = If(Test-Path "HKLM:\SOFTWARE\Microsoft\Virtual Machine\Guest\Parameters"){(get-item "HKLM:\SOFTWARE\Microsoft\Virtual Machine\Guest\Parameters").GetValue("HostName")}else{""}
Write-Verbose " Variables Fulfilled"

# Create Additional Files to suppliment Output.csv
Write-Verbose "Getting Services…"
Get-CimInstance -ComputerName $object.("Computername") -class win32_service | Sort-Object | Select-Object Caption,Startmode,Started,StartName | Export-Csv $ServicesFile -NoTypeInformation
Write-Verbose "Getting Installed Applications…"
Get-CimInstance -ComputerName $object.("Computername") -class win32_Product | Sort-Object | Select-Object Name,Version,Installdate | Export-Csv $AddRemoveProgramsFile -NoTypeInformation
Write-Verbose "Getting Scheduled tasks…"
Get-ScheduledTask | Get-ScheduledTaskInfo | Sort-Object |Select-Object TaskName,Description,Author,State,LastRunTime,LastTaskResult,NextRunTime,TaskPath | Export-Csv $SchedTaskFile -NoTypeInformation
Write-Verbose "Extra Files Populated"

$DeviceInfo= @{}
$DeviceInfo.add("Operating System", $os.name.split("|")[0])
$DeviceInfo.add("Version", $os.Version)
$DeviceInfo.add("Architecture", $os.OSArchitecture)
$deviceInfo.add("ServiceTag/Serial", $stg.SerialNumber)
$DeviceInfo.add("P or V", $cmp.model)
$deviceInfo.add("HyperV Host", $MShypervisor)
$deviceInfo.add("Logical Processors", $cmp.NumberOfLogicalProcessors -join (", "))
$DeviceInfo.add("Total Memory", "{0:N2}" -f ([math]::round($cmp.TotalPhysicalMemory / 1GB),2) + " GB")
$DeviceInfo.add("Serial Number", $os.SerialNumber)
$DeviceInfo.add("Organization", $os.Organization)
$DeviceInfo.add("Last Boot Time", $os.LastBootUpTime)
$deviceInfo.add("Disk ID", ($siz.DeviceID -join (", ")))
$deviceInfo.Add("Disk Size", $capacity -join (", "))
$deviceInfo.Add("Free Capacity", $freespace -join (", "))
$DeviceInfo.add("System Name", $cmp.name -join (", "))
$DeviceInfo.add("File System", $vol.FileSystem -join (", "))
$DeviceInfo.add("IP Address", ($net.IPAddress -join (", ")))
$DeviceInfo.add("Default Gateway", ($net.DefaultIPGateway -join (", ")))
$DeviceInfo.add("DNS Server", ($net.DNSServerSearchOrder -join (", ")))
$DeviceInfo.add("Subnet", ($net.IPSubnet -join (", ")))
$DeviceInfo.add("MAC Address", $net.MACAddress )
$DeviceInfo.add("Roles and Features", $far.name -join (", "))

$out += New-Object PSObject -Property $DeviceInfo | Select-Object `
"System Name", "Last Boot Time", "Organization", "Serial Number","Operating System", "ServiceTag/Serial", `
"Version","Architecture","P or V", "HyperV Host", "Total Memory", "File System", "Disk ID", "Disk Size", `
"Free Capacity","MAC Address","IP Address","Default Gateway", "DNS Server", "Subnet", "Roles and Features"

Write-Verbose ($out | Out-String) -Verbose
$out | Export-CSV $outfile -NoTypeInformation
}

[/powershell]

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *