I’ve been tasked with creating a server inventory script. I’ve been working on this for a while, and am close to the finish line, but I’m stumbling to get across it. I need to get ClusterName, HostName, VMName, Prod/Dev, Windows Edition, Windows Version, total Physical Processors, total Physical Cores and total Virtual Cores. I have the bulk of this, but I am struggling on where to create the PSCustomObject and add-members to it. Here’s my code:
‘’’
Script-ServerInventory.ps1
$InfoObject = New-Object PSObject
$VMs = Get-VM *
foreach ($vm in $vms){
# ClusterName
$Cluster = (Get-Cluster -VM $VM).Name
# Host Server name
$VMHost = (Get-VMHost -VM $VM).Name
# VMName
$servername = $vm.name
# Server Role (Physical, Virtual, Host)
# Prod/Dev
if ($servername -like "p*"){
$servertype = 'Prod'
}elseif($servername -like "r*"){
$servertype = 'Prod'
}else{
$servertype = 'Dev'
}
# HA on Y/N
# Edition (Std, Ent, Datacenter)
$WindowsEdition = Invoke-Command -ComputerName $vm -ScriptBlock {(Get-WindowsEdition -Online).Edition}
# Version
$WindowsVersion = $VM.Guest.OSFullName
# Physical Processors, Physical Cores, Virtual Cores
$ErrorActionPreference = 'SilentlyContinue'
$Error.Clear()
$ProcessorConfig = Get-WmiObject -class Win32_Processor -computername $servername -namespace root\CIMV2 | Select PSComputerName, Name, NumberOfCores, NumberOfLogicalProcessors
If ($Error.Count -gt 0)
{
$ProcessorConfig = New-Object psobject
$ProcessorConfig | Add-Member -type NoteProperty -name ComputerName ('$Computer-failed to connect')
$ProcessorConfig | Add-Member -type NoteProperty -name Name -value 'Unable to get ProcessorInfo'
$ProcessorConfig | Add-Member -type NoteProperty -name NumberOfCores -value $null
$ProcessorConfig | Add-Member -type NoteProperty -name NumberOfLogicalProcessors -value $null
Write-Debug Failed to connect to $servername;
}
$ErrorActionPreference = 'Continue'
$ProcessorConfig | FT * -AutoSize
$PhysicalProcessors = $ProcessorConfig.Name
$PhysicalCores = $ProcessorConfig.NumberOfCores
$VirtualCores = $ProcessorConfig.NumberOfLogicalProcessors
Add-Member -InputObject $InfoObject -MemberType NoteProperty -Name "Cluster" -Value $Cluster
Add-Member -InputObject $InfoObject -MemberType NoteProperty -Name "ServerName" -Value $servername
Add-Member -InputObject $InfoObject -MemberType NoteProperty -Name "Host" -Value -Force $VMHost
Add-Member -InputObject $InfoObject -MemberType NoteProperty -Name "Environment" -Value -Force $servertype
Add-Member -InputObject $InfoObject -MemberType NoteProperty -Name "Edition" -Value -Force $WindowsEdition
Add-Member -InputObject $InfoObject -MemberType NoteProperty -Name "Version" -Value -Force $WindowsVersion
Add-Member -InputObject $InfoObject -MemberType NoteProperty -Name "PhysicalProcessors" -Value -Force $PhysicalProcessors.Count
Add-Member -InputObject $InfoObject -MemberType NoteProperty -Name "PhysicalCores" -Value -Force $PhysicalCores.Count
Add-Member -InputObject $InfoObject -MemberType NoteProperty -Name "VirtualCores" -Value -Force $VirtualCores.Count
$InfoObject
} $InfoObject | Export-Csv -Path C:\temp\VMInventory.csv -Append -NoTypeInformation -Verbose
‘’’
I’m trying to get as much information from Get-VM as I can here due to the fact that we have several domains that these VMs are connected to. I keep getting "Missing an argument for -Value. Specify a parameter of type ‘System.Object’ and try again. "
Any guidance would be appreciated.