Create a Powershell server report using wmi - Noob needs help, please

I’m in need of creating a report that will help retrieve the following items in a CSV format:

Server/ ReportDate / TotalAllocation / UsedGB / SubtractedAmount / BilledAllocation / OperatingSystem / Version

I was able to modify a great script to get a few of the items on my report using the Class Win32_volume but not sure how to get the complete Total disk amount along with the UsedGB for all the Disks on a server.

$Server = Get-WmiObject -Class Win32_volume -computer $computer -filter "drivetype=3 AND DriveLetter != NULL"

I was trying to find a way to utilize the Win32_logicalDisk from the initial script I found but with my limited powershell skills, I was unable to gather/collect the needed information of each server.

$Disks = Get-WmiObject Win32_LogicalDisk -ComputerName $Computer -Filter "DriveType = 3" -ErrorAction Stop

 

Initial script - https://community.spiceworks.com/scripts/show/2147-get-diskinfo

 

Current attempt:

#Working on learning the steps of the script:

#This allows you to control the function of how the cmdlet itself works.  So it will require a Parameter and allows piping.

# Typical input - Get-Content c:\scripts\servers.txt | .\GetServerInfo.ps1 | Sort SystemName | Format-Table

#Most servers being queried will be SErver 2008 R2 or 2012 R2.

[CmdletBinding()]

Param(

[Parameter(Mandatory=$true,

ValueFromPipeline=$true)]

[String[]]$ComputerName,

[switch]$ShowAll

)

#This section creates HASH tables for each iteration it passes the ForEach loop

Begin {

$ServerResults = @()

$DiskResults = @()         

}

#Here is where each server in my servers.txt file will be processed.

Process {

    ForEach ($Computer in $ComputerName)

    {                         

                                $vp = "V"

                                $SAmount = '80'

                                $Drives = $computer.group        

                                $csvdetail = $drives | Select @{ Name = "Server"; Expression = { $computer.name } },

                                @{ Name = "ReportDate"; Expression = { Get-Date -Format g } },

                                @{ Name = "Drive"; Expression = { $_.DriveLetter } },

                                @{ Name = "SizeGB"; Expression = { "{0:N2}" -f ($_.Capacity/1GB) } },

                                @{ Name = "UsedGB"; Expression = { "{0:N2}" -f (($_.Capacity - $_.Freespace)/1GB) } },

                                @{ Name = "FreeGB"; Expression = { "{0:N2}" -f ($_.FreeSpace/1GB) } }

                                $Server = Get-WmiObject -Class Win32_volume -computer $computer -filter "drivetype=3 AND DriveLetter != NULL"

                                $SystemName                                  = $Server.SystemName

                                $ReportDate                      = Get-Date -format g

                                $MachineType                 = $vp

                                $subtractedAmount       = $SAmount

                                <#

                OperatingSystem = $operatingSystem.caption

                Version = $operatingSystem.Caption + " " + $operatingSystem.Version

                TotalAllocation = "{0:N2}" -f (($Drives | Measure-Object Capacity -Sum).Sum/1GB)

                FreeGB = "{0:N2}" -f (($Drives | Measure-Object FreeSpace -Sum).Sum/1GB)

                UsedGB = "{0:N2}" -f ((($Drives | Measure-Object Capacity -Sum).Sum/1GB) - (($Drives | Measure-Object FreeSpace -Sum).Sum/1GB))

                BilledAllocation = "{0:N2}" -f ((($Drives | Measure-Object Capacity -Sum).Sum/1GB) - $subtractedAmount)

                #>

                $ServerInfo = @{

                Server                                                   = $SystemName

                ReportDate                                        = $ReportDate

                MachineType                                    = $MachineType

                SubtractedAmount         = $subtractedAmount

                                                <#

                                                Additional hash variables will be added to above $serverInfo

                                                #>                                          

                                }

                                New-Object PSObject -Property $ServerInfo

                                $ServerInfo

                                }             

    }

End {     

                $ServerInfo

}

I know there are missing parts and I would be very grateful for any help on getting this report complete.

Cheers,

Parents
No Data
Reply
  • I thought I might have solved my issue but still unable to get the final output I need.

    At the ps prompt:

    .\GetServerInfo.ps1 -ComputerName ServerName

    And here is the contents of GetServerInfo.ps1:

    [CmdletBinding()]
    Param(
    [Parameter(Mandatory=$true,
    ValueFromPipeline=$true)]
    [String[]]$ComputerName,
    [switch]$ShowAll
    )

    $exportLocation = 'f:\temp\ServerReport.csv'

    foreach ($computer in $computers) {

    $Drives = $computer.group
    $csvdetail = $drives | Select @{ Name = "Server"; Expression = { $computer.name } },
    @{ Name = "ReportDate"; Expression = { Get-Date -Format g } },
    @{ Name = "Drive"; Expression = { $_.DriveLetter } },
    @{ Name = "SizeGB"; Expression = { "{0:N2}" -f ($_.Capacity/1GB) } },
    @{ Name = "UsedGB"; Expression = { "{0:N2}" -f (($_.Capacity - $_.Freespace)/1GB) } },
    @{ Name = "FreeGB"; Expression = { "{0:N2}" -f ($_.FreeSpace/1GB) } }



    $computerSystem = Get-WmiObject -Computer $computer.Name -Class 'Win32_ComputerSystem'
    $operatingSystem = Get-WmiObject -ComputerName $computer.Name -Class 'Win32_OperatingSystem'
    $Server = $computer.Name
    $ReportDate = Get-Date -format g
    $MachineType = "V"
    $SubtractedAmount = "80"
    $OperatingSystem = $operatingSystem.caption
    $Version = $operatingSystem.Caption + " " + $operatingSystem.Version
    $TotalAllocation = "{0:N2}" -f (($Drives | Measure-Object Capacity -Sum).Sum/1GB)
    $FreeGB = "{0:N2}" -f (($Drives | Measure-Object FreeSpace -Sum).Sum/1GB)
    $UsedGB = "{0:N2}" -f ((($Drives | Measure-Object Capacity -Sum).Sum/1GB) - (($Drives | Measure-Object FreeSpace -Sum).Sum/1GB))
    $BilledAllocation = "{0:N2}" -f ((($Drives | Measure-Object Capacity -Sum).Sum/1GB) - $subtractedAmount)


    $OutputObj = New-Object -Type PSObject
    $OutputObj | Add-Member -MemberType NoteProperty -Name ComputerName -Value $Computer.ToUpper()
    $OutputObj | Add-Member -MemberType NoteProperty -Name Report Date -Value $ReportDate
    $OutputObj | Add-Member -MemberType NoteProperty -Name TotalAllocation -Value $TotalAllocation
    $OutputObj | Add-Member -MemberType NoteProperty -Name UsedGB -Value $UsedGB
    $OutputObj | Add-Member -MemberType NoteProperty -Name SubtractedAmount -Value $SubtractedAmount
    $OutputObj | Add-Member -MemberType NoteProperty -Name BilledAllocation -Value $BilledAllocation
    $OutputObj | Add-Member -MemberType NoteProperty -Name Operating_System -Value $OperatingSystem
    $OutputObj | Add-Member -MemberType NoteProperty -Name Operating_System_BuildVersion -Value $Version
    $OutputObj | Export-Csv $exportLocation -Append -NoTypeInformation
    #$OutputObj | convertto-csv -NoTypeInformation | Out-File f:\Temp\Serverreport.csv
    }


    As you can notice, I commented out the last line, I first wanted to export to csv but tried using the other method. Neither provides me with the csv file.
    Any help would be appreciated.
Children