Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $credentials = Get-Credential
- #Defining import and export variables
- $complist = ".\computer.txt"
- $outreport = ".\Server_Inventory_" + $((Get-Date).ToString('MM-dd-yyyy')) + ".csv"
- #Start PSRemoting.
- $invokeArgs = @{
- ComputerName = (Get-Content $complist)
- Credential = $credentials
- }
- [array]$allResults = Invoke-Command @invokeArgs -ScriptBlock {
- # NOTE -- I'm assuming you're using 'Get-WmiObject' for backwards
- # capability reasons? Instead of 'Get-CimInstance'?
- #Run the commands concurrently for each server in the list
- $CPUInfo = Get-WmiObject Win32_Processor #Get CPU Information
- $OSInfo = Get-WmiObject Win32_OperatingSystem #Get OS Information
- #Get Memory Information. The data will be shown in a table as GB, rounded to the nearest second decimal.
- $PhysicalMemory = Get-WmiObject CIM_PhysicalMemory | Measure-Object -Property capacity -Sum | ForEach-Object { [math]::round(($_.sum / 1GB), 2) }
- #Get Network Configuration
- $Network = Get-WmiObject Win32_NetworkAdapterConfiguration -Filter 'ipenabled = "true"'
- #Get local admins.
- # -- This 'will' fail on AD Domain Controllers, but can be ignored.
- $localadmins = Get-LocalGroupMember -Group "Administrators"
- #Get Powershell Version
- $powershell = $PSVersionTable
- #Get Filesystems.
- # -- '$Disk' doesn't seem to be used anywhere?
- $Disk = Get-WmiObject -Class Win32_logicaldisk -Filter "DeviceID = 'C:'" | Measure-Object -Property FreeSpace -Sum | ForEach-Object { [math]::round(($_.sum / 1GB), 2) }
- $drives = [System.Collections.Generic.List[object]]@(Get-WmiObject Win32_LogicalDisk -Filter "DriveType = 3")
- #Get list of shares
- $Shares = Get-WmiObject Win32_share |
- Where-Object { ($_.name -NotLike "*Admin*" -and $_.name -NotLike "*C*" -and $_.name -NotLike "*IPC*") }
- return [pscustomobject]@{
- ServerName = $CPUInfo.SystemName
- "CPU_Core_Count" = $CPUInfo.NumberOfCores
- "TotalMemory_GB" = $PhysicalMemory
- "OS_Name" = $OSInfo.Caption
- "OS_Version" = $OSInfo.Version
- "IP Address" = $Network.IPAddress
- "Local Admins" = $localadmins.Name
- "PowerShell Version" = $powershell.PSVersion.ToString()
- SharesName = $Shares.Name
- SharesPath = $Shares.Path
- Drives = $drives
- }
- <#
- Your original code
- $infoObject = New-Object PSObject
- #Add data to the infoObjects.
- Add-Member -InputObject $infoObject -MemberType NoteProperty -Name "ServerName" -Value $CPUInfo.SystemName
- Add-Member -InputObject $infoObject -MemberType NoteProperty -Name "CPU_Core_Count" -Value $CPUInfo.NumberOfCores
- Add-Member -InputObject $infoObject -MemberType NoteProperty -Name "TotalMemory_GB" -Value $PhysicalMemory
- Add-Member -InputObject $infoObject -MemberType NoteProperty -Name "OS_Name" -Value $OSInfo.Caption
- Add-Member -InputObject $infoObject -MemberType NoteProperty -Name "OS_Version" -Value $OSInfo.Version
- Add-Member -InputObject $infoObject -MemberType NoteProperty -Name "IP Address" -Value $Network.IPAddress
- Add-Member -InputObject $infoObject -MemberType NoteProperty -Name "Local Admins" -Value $localadmins.Name
- Add-Member -InputObject $infoObject -MemberType NoteProperty -Name "Powershell Version" -Value $powershell.PSversion
- Add-Member -InputObject $infoObject -MemberType NoteProperty -Name "SharesName" -Value $Shares.Name
- Add-Member -InputObject $infoObject -MemberType NoteProperty -Name "SharesPath" -Value $Shares.Path
- # foreach ($drive in $drives)
- # {
- # if ($drive.size -gt 0)
- # {
- # $drivename = $drive.DeviceID
- # $freespace = [int]($drive.FreeSpace / 1GB)
- # $totalspace = [int]($drive.size / 1GB)
- # Add-Member -InputObject $infoObject -MemberType NoteProperty `
- # -Name "$drivename Free Space" -Value ("$freespace GB / $totalspace GB")
- # }
- # }
- $infoObject
- #>
- }
- # Get all unique Drive ID's
- # -- This will provide the maximum list of 'FreeSpace' columns to add.
- [string[]] $uniqueIds = $allResults.Drives.DeviceID | Select-Object -Unique
- foreach ($result in $allResults) {
- # Store the computer's drives' DeviceID's.
- [string[]] $deviceIDs = $result.Drives.DeviceID
- # Find which Drive ID's are present and not present on the
- # current computer result.
- $idsPresent, $idsNotPresent = $uniqueIds.Where( { $_ -in $deviceIDs }, "Split")
- # Create a dummy PSNoteProperty for the drives that are
- # 'NOT' present on the computer.
- foreach ($notPresent in $idsNotPresent) {
- $propName = "$notPresent FreeSpace"
- [void] $result.psobject.Properties.Add(
- (New-Object PSNoteProperty($propName, $null))
- )
- }
- # Create a real PSNoteProperty for the drives that
- # 'ARE' present on the computer.
- foreach ($present in $result.Drives.Where( { $_.DeviceID -in $idsPresent })) {
- $propName = "$($present.DeviceID) FreeSpace"
- $freespace = [int]($present.FreeSpace / 1GB)
- $totalspace = [int]($present.Size / 1GB)
- $propValue = "$freespace GB / $totalspace GB"
- [void] $result.psobject.Properties.Add(
- (New-Object PSNoteProperty($propName, $propValue))
- )
- }
- }
- # Splat the Select-Object parameters.
- $selectArgs = @{
- Property = '*'
- ExcludeProperty = @(
- 'Drives'
- 'PSComputerName',
- 'RunspaceId',
- 'PSShowComputerName'
- )
- }
- # Export to CSV.
- $allResults | Select-Object @selectArgs | Export-Csv -Path $outreport -NoTypeInformation
- # Original Code
- #Select-Object * -ExcludeProperty PSComputerName, RunspaceId, PSShowComputerName | Export-Csv -Path $outreport -NoTypeInformation
Add Comment
Please, Sign In to add comment