#Load AD module #import-module activedirectory $d = [DateTime]::Today.AddDays(-90) #Initialize ESX hosts array $esxhosts = @{} #Connect to your VCENTER, change Vcenter name #Connect-VIServer "ldap389-Vcenter" #Get all vms $allvm = get-view -ViewType virtualmachine #Change the searchbase DN DC=ldap389,DC=info $servers = Get-ADComputer -searchbase "DC=ldap389,DC=info" -Properties OperatingSystem, OperatingSystemServicePack -Filter 'PasswordLastSet -ge $d' | Where-Object {$_.OperatingSystem -like ‘*server*’} 'Name;Model;CPU tot;Core tot;RAM;OS' | Out-file result.csv -append foreach ($server in $servers) {$ServerName= $Server.name $ServerDNSname = $Server.DNSHostName $query = "select * from win32_pingstatus where address = '$ServerName'" $result = Get-WmiObject -query $query #Test connectivity on the server if ($result.protocoladdress) { $Win32_COMP = Get-WmiObject Win32_ComputerSystem -computer $ServerName $mem = [int] ($Win32_COMP.TotalPhysicalMemory/1mb) #$cpucomp determines the method we use to count total CPUs and Cores see http://support.microsoft.com/kb/932370 $cpucomp = "old" if ($server.OperatingSystem -match '2008'){ $cpucomp = "new" $versionOS = $server.OperatingSystem } else{ $Win32_OS = Get-WmiObject Win32_OperatingSystem -computer $ServerName $versionOS = $Win32_OS.caption $kbs = get-wmiobject -class "Win32_QuickFixEngineering" -namespace "root\CIMV2" -ComputerName $ServerName foreach($kb in $kbs){if ($kb.HotFixID -match '932370'){$cpucomp = "new"}} } #New method NumberOfCores exists if ($cpucomp -eq "new"){ $Win32_cpu = Get-WmiObject -class win32_processor -computer $ServerName $cpu = ($Win32_cpu | measure-object).count $cores = ($Win32_cpu | measure-object NumberOfCores -sum).sum} #Old method NumberOfCores does not exist else{ $Win32_cpu = Get-WmiObject -class win32_processor -computer $ServerName $physCount = new-object hashtable $Win32_cpu |%{$physCount[$_.SocketDesignation] = 1} $cpu = $physCount.count $cores = ($Win32_cpu | measure-object).count } # If server is a VMware: ESX host name is stored in a hashtable, VMs are stored in a hashtable of the ESX host hashtable if ($Win32_COMP.Manufacturer -eq "VMware, Inc."){ $value = $ServerName + ';VM;' + $cpu + ';' + $cores + ';' + $mem + ';' + $versionOS $vm = $allvm | where{$_.guest.hostname -match $ServerDNSname} if($vm){ $esxhostv = (get-view $vm.Runtime.Host).Name #If ESX host is already scanned, the VM is linked to this host (In the VM hashtable), Otherwise we create a new entry for that ESX host. if ($esxhosts.Containskey($esxhostv) -eq $true){ $esxhosts.item($esxhostv).add(($esxhosts.item($esxhostv).values | measure-object).count +1 ,$value)} else{$esxhosts.add($esxhostv,@{"1" = $value})} } else {$ServerName + ';CHECKVC;' + $cpu + ';' + $cores + ';' + $mem + ';' + $versionOS | Out-file result.csv -append} } #If server is a physical machine, results retrieved with WMI are written in the result file else{$model = $Win32_COMP.Model $ServerName + ';' +$model + ';' + $cpu + ';' + $cores + ';' + $mem + ';' + $versionOS | Out-file result.csv -append} } } #We browse the ESX hashtable and retrieve information about each host foreach($esx in $esxhosts.keys){ $hard = (get-vmhost $esx).Extensiondata.Summary.Hardware $mem = [int] ($hard.MemorySize/1mb) $cpu = $hard.NumCpuPkgs $cores = $hard.NumCpuCores $model = $hard.Model $esx + ';' +$model + ';' + $cpu + ';' + $cores + ';' + $mem + ';ESX' | Out-file result.csv -append $esxhosts.item($esx).values | Out-file result.csv -append }