Yevrag35

Edits to Script

Jul 7th, 2020
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $credentials = Get-Credential
  2.  
  3. #Defining import and export variables
  4. $complist = ".\computer.txt"
  5. $outreport = ".\Server_Inventory_" + $((Get-Date).ToString('MM-dd-yyyy')) + ".csv"
  6.  
  7. #Start PSRemoting.
  8. $invokeArgs = @{
  9.     ComputerName = (Get-Content $complist)
  10.     Credential   = $credentials
  11. }
  12. [array]$allResults = Invoke-Command @invokeArgs -ScriptBlock {
  13.  
  14.     # NOTE -- I'm assuming you're using 'Get-WmiObject' for backwards
  15.     #         capability reasons?  Instead of 'Get-CimInstance'?
  16.  
  17.     #Run the commands concurrently for each server in the list
  18.     $CPUInfo = Get-WmiObject Win32_Processor #Get CPU Information
  19.     $OSInfo = Get-WmiObject Win32_OperatingSystem #Get OS Information
  20.  
  21.     #Get Memory Information. The data will be shown in a table as GB, rounded to the nearest second decimal.
  22.     $PhysicalMemory = Get-WmiObject CIM_PhysicalMemory | Measure-Object -Property capacity -Sum | ForEach-Object { [math]::round(($_.sum / 1GB), 2) }
  23.  
  24.     #Get Network Configuration
  25.     $Network = Get-WmiObject Win32_NetworkAdapterConfiguration -Filter 'ipenabled = "true"'
  26.  
  27.     #Get local admins.
  28.     # -- This 'will' fail on AD Domain Controllers, but can be ignored.
  29.     $localadmins = Get-LocalGroupMember -Group "Administrators"
  30.  
  31.     #Get Powershell Version
  32.     $powershell = $PSVersionTable
  33.  
  34.     #Get Filesystems.
  35.     # -- '$Disk' doesn't seem to be used anywhere?
  36.     $Disk = Get-WmiObject -Class Win32_logicaldisk -Filter "DeviceID = 'C:'" | Measure-Object -Property FreeSpace -Sum | ForEach-Object { [math]::round(($_.sum / 1GB), 2) }
  37.  
  38.     $drives = [System.Collections.Generic.List[object]]@(Get-WmiObject Win32_LogicalDisk -Filter "DriveType = 3")
  39.  
  40.     #Get list of shares
  41.     $Shares = Get-WmiObject Win32_share |
  42.         Where-Object { ($_.name -NotLike "*Admin*" -and $_.name -NotLike "*C*" -and $_.name -NotLike "*IPC*") }
  43.  
  44.     return [pscustomobject]@{
  45.         ServerName           = $CPUInfo.SystemName
  46.         "CPU_Core_Count"     = $CPUInfo.NumberOfCores
  47.         "TotalMemory_GB"     = $PhysicalMemory
  48.         "OS_Name"            = $OSInfo.Caption
  49.         "OS_Version"         = $OSInfo.Version
  50.         "IP Address"         = $Network.IPAddress
  51.         "Local Admins"       = $localadmins.Name
  52.         "PowerShell Version" = $powershell.PSVersion.ToString()
  53.         SharesName           = $Shares.Name
  54.         SharesPath           = $Shares.Path
  55.         Drives               = $drives
  56.     }
  57.     <#
  58.     Your original code
  59.     $infoObject = New-Object PSObject
  60.  
  61.     #Add data to the infoObjects.
  62.     Add-Member -InputObject $infoObject -MemberType NoteProperty -Name "ServerName" -Value $CPUInfo.SystemName
  63.     Add-Member -InputObject $infoObject -MemberType NoteProperty -Name "CPU_Core_Count" -Value $CPUInfo.NumberOfCores
  64.     Add-Member -InputObject $infoObject -MemberType NoteProperty -Name "TotalMemory_GB" -Value $PhysicalMemory
  65.     Add-Member -InputObject $infoObject -MemberType NoteProperty -Name "OS_Name" -Value $OSInfo.Caption
  66.     Add-Member -InputObject $infoObject -MemberType NoteProperty -Name "OS_Version" -Value $OSInfo.Version
  67.     Add-Member -InputObject $infoObject -MemberType NoteProperty -Name "IP Address" -Value $Network.IPAddress
  68.     Add-Member -InputObject $infoObject -MemberType NoteProperty -Name "Local Admins" -Value $localadmins.Name
  69.     Add-Member -InputObject $infoObject -MemberType NoteProperty -Name "Powershell Version" -Value $powershell.PSversion
  70.     Add-Member -InputObject $infoObject -MemberType NoteProperty -Name "SharesName" -Value $Shares.Name
  71.     Add-Member -InputObject $infoObject -MemberType NoteProperty -Name "SharesPath" -Value $Shares.Path
  72.  
  73.     # foreach ($drive in $drives)
  74.     # {
  75.     #     if ($drive.size -gt 0)
  76.     #     {
  77.     #         $drivename = $drive.DeviceID
  78.     #         $freespace = [int]($drive.FreeSpace / 1GB)
  79.     #         $totalspace = [int]($drive.size / 1GB)
  80.     #         Add-Member -InputObject $infoObject -MemberType NoteProperty `
  81.     #       -Name "$drivename Free Space" -Value ("$freespace GB / $totalspace GB")
  82.     #     }
  83.     # }
  84.  
  85.     $infoObject
  86.     #>
  87. }
  88.  
  89. # Get all unique Drive ID's
  90. # -- This will provide the maximum list of 'FreeSpace' columns to add.
  91. [string[]] $uniqueIds = $allResults.Drives.DeviceID | Select-Object -Unique
  92.  
  93. foreach ($result in $allResults) {
  94.  
  95.     # Store the computer's drives' DeviceID's.
  96.     [string[]] $deviceIDs = $result.Drives.DeviceID
  97.  
  98.     # Find which Drive ID's are present and not present on the
  99.     # current computer result.
  100.     $idsPresent, $idsNotPresent = $uniqueIds.Where( { $_ -in $deviceIDs }, "Split")
  101.  
  102.     # Create a dummy PSNoteProperty for the drives that are
  103.     # 'NOT' present on the computer.
  104.     foreach ($notPresent in $idsNotPresent) {
  105.         $propName = "$notPresent FreeSpace"
  106.         [void] $result.psobject.Properties.Add(
  107.             (New-Object PSNoteProperty($propName, $null))
  108.         )
  109.     }
  110.  
  111.     # Create a real PSNoteProperty for the drives that
  112.     # 'ARE' present on the computer.
  113.     foreach ($present in $result.Drives.Where( { $_.DeviceID -in $idsPresent })) {
  114.         $propName = "$($present.DeviceID) FreeSpace"
  115.         $freespace = [int]($present.FreeSpace / 1GB)
  116.         $totalspace = [int]($present.Size / 1GB)
  117.         $propValue = "$freespace GB / $totalspace GB"
  118.  
  119.         [void] $result.psobject.Properties.Add(
  120.             (New-Object PSNoteProperty($propName, $propValue))
  121.         )
  122.     }
  123. }
  124.  
  125. # Splat the Select-Object parameters.
  126. $selectArgs = @{
  127.     Property        = '*'
  128.     ExcludeProperty = @(
  129.         'Drives'
  130.         'PSComputerName',
  131.         'RunspaceId',
  132.         'PSShowComputerName'
  133.     )
  134. }
  135.  
  136. # Export to CSV.
  137. $allResults | Select-Object @selectArgs | Export-Csv -Path $outreport -NoTypeInformation
  138.  
  139. # Original Code
  140. #Select-Object * -ExcludeProperty PSComputerName, RunspaceId, PSShowComputerName | Export-Csv -Path $outreport -NoTypeInformation
Add Comment
Please, Sign In to add comment