{"id":995,"date":"2020-05-20T15:48:12","date_gmt":"2020-05-20T14:48:12","guid":{"rendered":"http:\/\/ihni.uk\/?p=995"},"modified":"2020-05-20T15:48:12","modified_gmt":"2020-05-20T14:48:12","slug":"powershell-system-data-gathering-script","status":"publish","type":"post","link":"https:\/\/ihni.uk\/?p=995","title":{"rendered":"Powershell &#8211; System Data Gathering Script"},"content":{"rendered":"<p>I&#8217;ve been meaning to get something like this together for a while, so it will be an always evolving script.<\/p>\n<p>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.<!--more--><\/p>\n<p>Already I have included a Wishlist of futures options I&#8217;d like to include.<\/p>\n<p>[powershell]<br \/>\n&lt;#<br \/>\n### Wishlist<br \/>\n&#8212;&#8212;&#8212;&#8212;<br \/>\n### 20.05.2020 &#8211; NOK<\/p>\n<p>Patch Level\/hotfixs installed<br \/>\nCentral CSV for collating info?<br \/>\nBackup of Registry? May not be required for this level.<\/p>\n<p>#&gt;<\/p>\n<p>Push-Location (Split-Path -path $MyInvocation.MyCommand.Definition -Parent)<\/p>\n<p># Cutting out manual input of file names for local running.<br \/>\n&lt;#Param(<br \/>\n  [Parameter(Mandatory=$true, position=0)][string]$infile,<br \/>\n  [Parameter(Mandatory=$true, position=1)][string]$outfile<br \/>\n)#&gt;<\/p>\n<p>$infile = &quot;servers.csv&quot;<br \/>\n$outfile = &quot;$($env:computername)_Output.csv&quot;<br \/>\n$ServicesFile = &quot;$($env:computername)_Services.csv&quot;<br \/>\n$AddRemoveProgramsFile = &quot;$($env:computername)_AddRemovePrograms.csv&quot;<br \/>\n$SchedTaskFile = &quot;$($env:computername)_SchedTask.csv&quot;<\/p>\n<p>#Column header in input CSV file that contains the host name<br \/>\n$ColumnHeader = &quot;ComputerName&quot;<br \/>\n$HostList = import-csv $infile | select-object $ColumnHeader<br \/>\n$out = @()<\/p>\n<p>foreach($object in $HostList) {<\/p>\n<p>    $os =  Get-CIMInstance -computername $object.(&quot;ComputerName&quot;) -class win32_operatingsystem<br \/>\n    $vol = Get-CIMInstance -computername $object.(&quot;ComputerName&quot;) -class Win32_Volume<br \/>\n    $siz = Get-CIMInstance -computername $object.(&quot;computername&quot;) -class Win32_LogicalDisk | where-object {$_.DriveType -eq &quot;3&quot;}<br \/>\n    $net = Get-CIMInstance -computername $object.(&quot;ComputerName&quot;) -class Win32_NetworkAdapterConfiguration | where-object { $_.IPAddress -ne $null }<br \/>\n    $cmp = Get-CIMInstance -computername $object.(&quot;ComputerName&quot;) -class Win32_ComputerSystem<br \/>\n    $stg = Get-CIMInstance -ComputerName $object.(&quot;Computername&quot;) -class Win32_SystemEnclosure<br \/>\n    $far = Get-WindowsFeature -ComputerName $object.(&quot;ComputerName&quot;) | Where-Object Installed<br \/>\n    $freespace = $siz.FreeSpace | %{[math]::round($_\/1GB)}<br \/>\n    $capacity = $siz.size | %{[math]::round($_\/1GB)}<br \/>\n    $MShypervisor = If(Test-Path &quot;HKLM:\\SOFTWARE\\Microsoft\\Virtual Machine\\Guest\\Parameters&quot;){(get-item &quot;HKLM:\\SOFTWARE\\Microsoft\\Virtual Machine\\Guest\\Parameters&quot;).GetValue(&quot;HostName&quot;)}else{&quot;&quot;}<br \/>\n    Write-Verbose &quot; Variables Fulfilled&quot;<\/p>\n<p>     # Create Additional Files to suppliment Output.csv<br \/>\n    Write-Verbose &quot;Getting Services&#8230;&quot;<br \/>\n    Get-CimInstance -ComputerName $object.(&quot;Computername&quot;) -class win32_service | Sort-Object | Select-Object Caption,Startmode,Started,StartName | Export-Csv $ServicesFile -NoTypeInformation<br \/>\n    Write-Verbose &quot;Getting Installed Applications&#8230;&quot;<br \/>\n    Get-CimInstance -ComputerName $object.(&quot;Computername&quot;) -class win32_Product | Sort-Object | Select-Object Name,Version,Installdate | Export-Csv $AddRemoveProgramsFile -NoTypeInformation<br \/>\n    Write-Verbose &quot;Getting Scheduled tasks&#8230;&quot;<br \/>\n    Get-ScheduledTask | Get-ScheduledTaskInfo | Sort-Object |Select-Object TaskName,Description,Author,State,LastRunTime,LastTaskResult,NextRunTime,TaskPath | Export-Csv $SchedTaskFile -NoTypeInformation<br \/>\n    Write-Verbose &quot;Extra Files Populated&quot;<\/p>\n<p>    $DeviceInfo= @{}<br \/>\n    $DeviceInfo.add(&quot;Operating System&quot;, $os.name.split(&quot;|&quot;)[0])<br \/>\n    $DeviceInfo.add(&quot;Version&quot;, $os.Version)<br \/>\n    $DeviceInfo.add(&quot;Architecture&quot;, $os.OSArchitecture)<br \/>\n    $deviceInfo.add(&quot;ServiceTag\/Serial&quot;, $stg.SerialNumber)<br \/>\n    $DeviceInfo.add(&quot;P or V&quot;, $cmp.model)<br \/>\n    $deviceInfo.add(&quot;HyperV Host&quot;, $MShypervisor)<br \/>\n    $deviceInfo.add(&quot;Logical Processors&quot;, $cmp.NumberOfLogicalProcessors -join (&quot;, &quot;))<br \/>\n    $DeviceInfo.add(&quot;Total Memory&quot;, &quot;{0:N2}&quot; -f ([math]::round($cmp.TotalPhysicalMemory \/ 1GB),2) + &quot; GB&quot;)<br \/>\n    $DeviceInfo.add(&quot;Serial Number&quot;, $os.SerialNumber)<br \/>\n    $DeviceInfo.add(&quot;Organization&quot;, $os.Organization)<br \/>\n    $DeviceInfo.add(&quot;Last Boot Time&quot;, $os.LastBootUpTime)<br \/>\n    $deviceInfo.add(&quot;Disk ID&quot;, ($siz.DeviceID -join (&quot;, &quot;)))<br \/>\n    $deviceInfo.Add(&quot;Disk Size&quot;, $capacity -join (&quot;, &quot;))<br \/>\n    $deviceInfo.Add(&quot;Free Capacity&quot;, $freespace -join (&quot;, &quot;))<br \/>\n    $DeviceInfo.add(&quot;System Name&quot;, $cmp.name -join (&quot;, &quot;))<br \/>\n    $DeviceInfo.add(&quot;File System&quot;, $vol.FileSystem -join (&quot;, &quot;))<br \/>\n    $DeviceInfo.add(&quot;IP Address&quot;, ($net.IPAddress -join (&quot;, &quot;)))<br \/>\n    $DeviceInfo.add(&quot;Default Gateway&quot;, ($net.DefaultIPGateway -join (&quot;, &quot;)))<br \/>\n    $DeviceInfo.add(&quot;DNS Server&quot;, ($net.DNSServerSearchOrder -join (&quot;, &quot;)))<br \/>\n    $DeviceInfo.add(&quot;Subnet&quot;, ($net.IPSubnet  -join (&quot;, &quot;)))<br \/>\n    $DeviceInfo.add(&quot;MAC Address&quot;, $net.MACAddress )<br \/>\n    $DeviceInfo.add(&quot;Roles and Features&quot;, $far.name -join (&quot;, &quot;))<\/p>\n<p>    $out += New-Object PSObject -Property $DeviceInfo | Select-Object `<br \/>\n              &quot;System Name&quot;, &quot;Last Boot Time&quot;, &quot;Organization&quot;, &quot;Serial Number&quot;,&quot;Operating System&quot;, &quot;ServiceTag\/Serial&quot;, `<br \/>\n              &quot;Version&quot;,&quot;Architecture&quot;,&quot;P or V&quot;, &quot;HyperV Host&quot;, &quot;Total Memory&quot;, &quot;File System&quot;, &quot;Disk ID&quot;, &quot;Disk Size&quot;, `<br \/>\n              &quot;Free Capacity&quot;,&quot;MAC Address&quot;,&quot;IP Address&quot;,&quot;Default Gateway&quot;, &quot;DNS Server&quot;, &quot;Subnet&quot;, &quot;Roles and Features&quot;<\/p>\n<p>    Write-Verbose ($out | Out-String) -Verbose<br \/>\n    $out | Export-CSV $outfile -NoTypeInformation<br \/>\n}<\/p>\n<p>[\/powershell]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve been meaning to get something like this together for a while, so it will&hellip;<\/p>\n","protected":false},"author":1,"featured_media":988,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[19,11],"tags":[],"class_list":["post-995","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-powershell","category-scripts"],"jetpack_featured_media_url":"https:\/\/ihni.uk\/wp-content\/uploads\/2020\/02\/Powershell.png","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/ihni.uk\/index.php?rest_route=\/wp\/v2\/posts\/995","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ihni.uk\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ihni.uk\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ihni.uk\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ihni.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=995"}],"version-history":[{"count":0,"href":"https:\/\/ihni.uk\/index.php?rest_route=\/wp\/v2\/posts\/995\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ihni.uk\/index.php?rest_route=\/wp\/v2\/media\/988"}],"wp:attachment":[{"href":"https:\/\/ihni.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=995"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ihni.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=995"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ihni.uk\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=995"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}