[ASK]
How to grab vm by datacenter name and grab status vm on/off ?
i just want to grab KP2 Virtual Data Center/ another data center
this is my code please help, i was try to change/add code but its still error..
&{foreach($vm in (Get-VM)) {
$vm.ExtensionData.Guest.Net | select -Property @{N='VM';E={$vm.Name.split('(')[0].TrimEnd(' ')}},
@{N='PIC Aplikasi';E={$vm.Name.split('()')[1]}},
@{N='OS';E={$vm.Guest.OSFullName}},
@{N="NumCPU";E={$vm.NumCPU}},
@{N="MemoryMB";E={$vm.MemoryMB}},
@{N='IP';E={[string]::Join(',',($vm.Guest.IPAddress | Where {($_.Split(".")).length -eq 4}))}},
@{N='Gateway';E={[string]::Join(',',($vm.ExtensionData.Guest.IpStack.IpRouteConfig.IpRoute | %{if($_.Gateway.IpAddress){$_.Gateway.IpAddress}}))}},
@{N='Subnet Mask';E={
$dec = [Convert]::ToUInt32($(('1' * $_.IpConfig.IpAddress[0].PrefixLength).PadRight(32, '0')), 2)
$DottedIP = $( For ($i = 3; $i -gt -1; $i--) {
$Remainder = $dec % [Math]::Pow(256, $i)
( $dec - $Remainder) / [Math]::Pow(256, $i)
$dec = $Remainder
} )
[String]::Join('.', $DottedIP)
}},
@{N='MAC';E={[string]::Join(',',$_.MacAddress)}}
}
} | Export-Csv -NoTypeInformation C:\Users\xyz\Desktop\xyz.csv
Thank You So Much
Tomi Irawan
You mean like this?
& { foreach ($vm in (Get-Datacenter -Name $dcName | Get-VM))
{
$vm.ExtensionData.Guest.Net | select -Property @{N = 'VM'; E = { $vm.Name.split('(')[0].TrimEnd(' ') } },
@{N = 'State'; E = { $vm.PowerState } },
@{N = 'PIC Aplikasi'; E = { $vm.Name.split('()')[1] } },
@{N = 'OS'; E = { $vm.Guest.OSFullName } },
@{N = "NumCPU"; E = { $vm.NumCPU } },
@{N = "MemoryMB"; E = { $vm.MemoryMB } },
@{N = 'IP'; E = { [string]::Join(',', ($vm.Guest.IPAddress | Where { ($_.Split(".")).length -eq 4 })) } },
@{N = 'Gateway'; E = { [string]::Join(',', ($vm.ExtensionData.Guest.IpStack.IpRouteConfig.IpRoute | % { if ($_.Gateway.IpAddress) { $_.Gateway.IpAddress } })) } },
@{N = 'Subnet Mask'; E = {
$dec = [Convert]::ToUInt32($(('1' * $_.IpConfig.IpAddress[0].PrefixLength).PadRight(32, '0')), 2)
$DottedIP = $( For ($i = 3; $i -gt -1; $i--)
{
$Remainder = $dec % [Math]::Pow(256, $i)
( $dec - $Remainder) / [Math]::Pow(256, $i)
$dec = $Remainder
} )
[String]::Join('.', $DottedIP)
}
},
@{N = 'MAC'; E = { [string]::Join(',', $_.MacAddress) } }
}
} | Export-Csv -NoTypeInformation C:\Users\xyz\Desktop\xyz.csv
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
You mean like this?
& { foreach ($vm in (Get-Datacenter -Name $dcName | Get-VM))
{
$vm.ExtensionData.Guest.Net | select -Property @{N = 'VM'; E = { $vm.Name.split('(')[0].TrimEnd(' ') } },
@{N = 'State'; E = { $vm.PowerState } },
@{N = 'PIC Aplikasi'; E = { $vm.Name.split('()')[1] } },
@{N = 'OS'; E = { $vm.Guest.OSFullName } },
@{N = "NumCPU"; E = { $vm.NumCPU } },
@{N = "MemoryMB"; E = { $vm.MemoryMB } },
@{N = 'IP'; E = { [string]::Join(',', ($vm.Guest.IPAddress | Where { ($_.Split(".")).length -eq 4 })) } },
@{N = 'Gateway'; E = { [string]::Join(',', ($vm.ExtensionData.Guest.IpStack.IpRouteConfig.IpRoute | % { if ($_.Gateway.IpAddress) { $_.Gateway.IpAddress } })) } },
@{N = 'Subnet Mask'; E = {
$dec = [Convert]::ToUInt32($(('1' * $_.IpConfig.IpAddress[0].PrefixLength).PadRight(32, '0')), 2)
$DottedIP = $( For ($i = 3; $i -gt -1; $i--)
{
$Remainder = $dec % [Math]::Pow(256, $i)
( $dec - $Remainder) / [Math]::Pow(256, $i)
$dec = $Remainder
} )
[String]::Join('.', $DottedIP)
}
},
@{N = 'MAC'; E = { [string]::Join(',', $_.MacAddress) } }
}
} | Export-Csv -NoTypeInformation C:\Users\xyz\Desktop\xyz.csv
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Dear LuCD
Thank You for your fast respond. i wil try it first thank you so much
Dear LucD
Please help me how to split this table,
sample i have vm who have a lot of ip, ip1,ip2 and gateway 1,gateway2.etc , can i split this ?
thank you so much
The question you will have to answer first is, how do you want to show this in your output?
If an array has rows with different numbers of properties, there is a problem with for example Export-Csv.
To guarantee the same number of properties/columns, you could for example create columns like IP1, IP2, IP3, IP4.
But that would mean that several rows have no values in some columns.
You decide.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Dear LucD
Thank you for your fast resond .
look like this is no problem because there is some vm who have a more IP & Gateway and some server have the only one IP & Gateway.
Header 1 | Header 2 | Header 3 | Header 4 |
---|---|---|---|
vm A | IP1 | IP2 | |
vm B | IP1 | ||
vm C | IP1 | IP2 | IP3 |
Thank You For your help.
Not sure what you are trying to say here.
But the point I'm trying to make is demonstrated by this example
"" | Select @{N='F1';E={'abc'}}
"" | Select @{N='F1';E={'abc'}},@{N='F2';E={'xyz'}}
)
$t2 = @(
"" | Select @{N='F1';E={'abc'}},@{N='F2';E={'xyz'}}
"" | Select @{N='F1';E={'abc'}}
)
$t1 | Export-Csv -Path .\report1.csv -NoTypeInformation -UseCulture
Invoke-Item -Path .\report1.csv
$t2 | Export-Csv -Path .\report2.csv -NoTypeInformation -UseCulture
Invoke-Item -Path .\report2.csv
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Dear LucD
thank you for the answer but, i want the text between in a separate column in one report.?
Thank You Mr. LucD
up
You didn't really answer my question.
For Export-Csv all the rows should have the same number of properties.
How do you intend to display that?
Would you go for a fixed number of (potentially empty) columns for IP, Gateway and netmask?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Dear Mr. LucD
Sorry for that,
it doesn't matter for a few empty columns because it doesn't mean the number of ip and gateway in one vm.
can separate columns according to the number of texts ?
Thank You So Much