Get information about virtual machines in Hyper-V (powershell, wmi)
#by alex
filter Import-CimXml
{
$CimXml = [Xml]$_
$CimObj = New-Object -TypeName System.Object
foreach ($CimProperty in $CimXml.SelectNodes("/INSTANCE/PROPERTY[@NAME='Name']"))
{
$CimObj | Add-Member -MemberType NoteProperty -Name $CimProperty.NAME -Value $CimProperty.VALUE
}
foreach ($CimProperty in $CimXml.SelectNodes("/INSTANCE/PROPERTY[@NAME='Data']"))
{
$CimObj | Add-Member -MemberType NoteProperty -Name $CimProperty.NAME -Value $CimProperty.VALUE
}
$CimObj
}
$vmsresult = @()
$HyperVServers = @("SERVER-VH01","SERVER-VH02","SERVER-VH03")
foreach($HyperVServer in $HyperVServers){
$VMNames = Invoke-Command -computername $HyperVServer {Get-VM | where {$_.State -eq 'Running'}}
foreach($VMName in $VMNames){
write-host "Work for vm: $($VMName.Name)"
$query = "Select * From Msvm_ComputerSystem Where ElementName='" + $($VMName.Name) + "'"
$Vm = gwmi -computername $HyperVServer -namespace root\virtualization\v2 -query $query
$query = "Associators of {$Vm} Where AssocClass=Msvm_SystemDevice ResultClass=Msvm_KvpExchangeComponent"
$Kvp = gwmi -computername $HyperVServer -namespace root\virtualization\v2 -query $query
$vmdata = $Kvp.GuestIntrinsicExchangeItems | Import-CimXml
$VmObj = New-Object -TypeName System.Object
$VmObj | Add-Member -MemberType NoteProperty -Name "Name" -Value $($VMName.Name)
$VmObj | Add-Member -MemberType NoteProperty -Name "OS" -Value $(($vmdata | ?{$_.Name -eq "OSName"}).Data)
$VmObj | Add-Member -MemberType NoteProperty -Name "IPv4" -Value $(($vmdata | ?{$_.Name -eq "NetworkAddressIPv4"}).Data)
$vmsresult+=$VmObj
}
}
#by alex
filter Import-CimXml
{
$CimXml = [Xml]$_
$CimObj = New-Object -TypeName System.Object
foreach ($CimProperty in $CimXml.SelectNodes("/INSTANCE/PROPERTY[@NAME='Name']"))
{
$CimObj | Add-Member -MemberType NoteProperty -Name $CimProperty.NAME -Value $CimProperty.VALUE
}
foreach ($CimProperty in $CimXml.SelectNodes("/INSTANCE/PROPERTY[@NAME='Data']"))
{
$CimObj | Add-Member -MemberType NoteProperty -Name $CimProperty.NAME -Value $CimProperty.VALUE
}
$CimObj
}
$vmsresult = @()
$HyperVServers = @("SERVER-VH01","SERVER-VH02","SERVER-VH03")
foreach($HyperVServer in $HyperVServers){
$VMNames = Invoke-Command -computername $HyperVServer {Get-VM | where {$_.State -eq 'Running'}}
foreach($VMName in $VMNames){
write-host "Work for vm: $($VMName.Name)"
$query = "Select * From Msvm_ComputerSystem Where ElementName='" + $($VMName.Name) + "'"
$Vm = gwmi -computername $HyperVServer -namespace root\virtualization\v2 -query $query
$query = "Associators of {$Vm} Where AssocClass=Msvm_SystemDevice ResultClass=Msvm_KvpExchangeComponent"
$Kvp = gwmi -computername $HyperVServer -namespace root\virtualization\v2 -query $query
$vmdata = $Kvp.GuestIntrinsicExchangeItems | Import-CimXml
$VmObj = New-Object -TypeName System.Object
$VmObj | Add-Member -MemberType NoteProperty -Name "Name" -Value $($VMName.Name)
$VmObj | Add-Member -MemberType NoteProperty -Name "OS" -Value $(($vmdata | ?{$_.Name -eq "OSName"}).Data)
$VmObj | Add-Member -MemberType NoteProperty -Name "IPv4" -Value $(($vmdata | ?{$_.Name -eq "NetworkAddressIPv4"}).Data)
$vmsresult+=$VmObj
}
}
$vmsresult contains VMs
Get same information about virtual machines in PowerCLI :
Connect-VIServer VServer
get-vm * | where {$_.PowerState -eq "PoweredOn"} | Sort | Get-View -Property @("Guest.HostName", "Guest.GuestFullName", "Guest.Ipaddress", "Name") | Select -Property @{N="HostName";E={$_.Guest.HostName}}, @{N="OS";E={$_.Guest.GuestFullName}}, @{N="IPv4";E={$_.Guest.IPaddress}}, @{N="VM Name";E={$_.Name}} | Format-Table -AutoSize
Get same information about hosts in AD by powershell :
$hostsresult = @()
$hosts = get-adcomputer -property * -Filter {Enabled -eq $true} | sort-object name | where {$_.OperatingSystem -like "*Server*"}
foreach($shost in $hosts){
write-host "Work for $shost"
$HostObj = New-Object -TypeName System.Object
$HostObj | Add-Member -MemberType NoteProperty -Name "Name" -Value $($shost.Name)
$HostObj | Add-Member -MemberType NoteProperty -Name "OS" -Value $($shost.OperatingSystem)
$ipaddresses = Invoke-Command -ComputerName $shost.name -EA SilentlyContinue -ScriptBlock {(gwmi Win32_NetworkAdapterConfiguration -EA SilentlyContinue | ?{$_.IPAddress}).IPAddress}
if(-not $ipaddresses){$ipaddresses = "not access winrm"}
if([string]$ipaddresses.GetType().BaseType -eq "array"){
$ipaddresses = $ipaddresses -Join ', '
}
$HostObj | Add-Member -MemberType NoteProperty -Name "IPv4" -Value $ipaddresses
$hostsresult+= $HostObj
}
$hostsresult contains hosts
================================================ (another)
$clusterNodes = Get-ClusterNode
ForEach($item in $clusterNodes){
$VMS= Get-VM -ComputerName $item.Name | Where-Object {$_.State –eq 'Running'}
foreach ($vm in $VMS){
$hdds=Get-VMHardDiskDrive $vm
foreach($hdd in $hdds) {
$hd = Invoke-Command -computername $item.Name -ScriptBlock {param($hdd); Get-VHD -Path $hdd.Path} -argumentlist $hdd
$hd | select @{N='VMname'; e={$vm.Name}}, @{N='Mb'; e={$_.FileSize/1Mb}}, @{N='Time'; e={[int]($_.FileSize/1Mb/150/60+30)}}
}
}
}
Get same information about virtual machines in PowerCLI :
Connect-VIServer VServer
get-vm * | where {$_.PowerState -eq "PoweredOn"} | Sort | Get-View -Property @("Guest.HostName", "Guest.GuestFullName", "Guest.Ipaddress", "Name") | Select -Property @{N="HostName";E={$_.Guest.HostName}}, @{N="OS";E={$_.Guest.GuestFullName}}, @{N="IPv4";E={$_.Guest.IPaddress}}, @{N="VM Name";E={$_.Name}} | Format-Table -AutoSize
Get same information about hosts in AD by powershell :
$hosts = get-adcomputer -property * -Filter {Enabled -eq $true} | sort-object name | where {$_.OperatingSystem -like "*Server*"}
foreach($shost in $hosts){
write-host "Work for $shost"
$HostObj = New-Object -TypeName System.Object
$HostObj | Add-Member -MemberType NoteProperty -Name "Name" -Value $($shost.Name)
$HostObj | Add-Member -MemberType NoteProperty -Name "OS" -Value $($shost.OperatingSystem)
$ipaddresses = Invoke-Command -ComputerName $shost.name -EA SilentlyContinue -ScriptBlock {(gwmi Win32_NetworkAdapterConfiguration -EA SilentlyContinue | ?{$_.IPAddress}).IPAddress}
if(-not $ipaddresses){$ipaddresses = "not access winrm"}
if([string]$ipaddresses.GetType().BaseType -eq "array"){
$ipaddresses = $ipaddresses -Join ', '
}
$HostObj | Add-Member -MemberType NoteProperty -Name "IPv4" -Value $ipaddresses
$hostsresult+= $HostObj
}
$hostsresult contains hosts
================================================ (another)
$clusterNodes = Get-ClusterNode
ForEach($item in $clusterNodes){
$VMS= Get-VM -ComputerName $item.Name | Where-Object {$_.State –eq 'Running'}
foreach ($vm in $VMS){
$hdds=Get-VMHardDiskDrive $vm
foreach($hdd in $hdds) {
$hd = Invoke-Command -computername $item.Name -ScriptBlock {param($hdd); Get-VHD -Path $hdd.Path} -argumentlist $hdd
$hd | select @{N='VMname'; e={$vm.Name}}, @{N='Mb'; e={$_.FileSize/1Mb}}, @{N='Time'; e={[int]($_.FileSize/1Mb/150/60+30)}}
}
}
}
Комментариев нет:
Отправить комментарий