Any possible way to get route print from the VM's .
I am trying using invoke method but can't write the output properly to csv.
foreach($vms in Get-ResourcePool test1 |Get-VM |? {$_.PowerState -eq 'Poweredon'}){
$command1 = @'
$hst=hostname
route print
'@
$command2 = @'
$hst1=hostname
ipconfig /all
'@
$output1=Invoke-VMScript -VM $vms -GuestUser 'administrator' -GuestPassword 'Adm1n1strat0r' -ScriptType Powershell -ScriptText $command1 | Select -ExpandProperty ScriptOutPut
$output2=Invoke-VMScript -VM $vms -GuestUser 'administrator' -GuestPassword 'Adm1n1strat0r' -ScriptType Powershell -ScriptText $command2 | Select -ExpandProperty ScriptOutPut
""|select @{Name="VM Name";E={$vms.name}},@{Name="Route Print";E={$output1}},@{Name="ipconfig";E={$output2}}
}
1. Properties fixed
2. One way of exporting could be like this.
Note that the script produces two separate CSV files, you could merge the info and produce one CSV file
$cmd1 = @'
Write-Host "Host,Name,Mask,Gateway,Metric"
Get-WmiObject Win32_IP4PersistedRouteTable | %{
Write-Host "$($_.PSComputerName),$($_.Name),$($_.Mask),$($_.NextHop),$($_.Metric1)"
}
'@
$cmd2 = @'
Write-Host "Host,Interface,Interface Alias,MAC,Status,DNS Suffix,NetworkCategory,IP,Gateway,DNS"
Get-NetIPConfiguration -Detailed | %{
Write-Host ("$($_.ComputerName),$($_.InterfaceDescription),$($_.InterfaceAlias),$($_.NetAdapter.LinkLayerAddress)," +
"$($_.NetAdapter.Status),$($_.NetProfile.Name),$($_.NetProfile.NetworkCategory),$($_.IPv4Address.IPv4Address),$($_.IPv4DefaultGateway.Nexthop)," +
"$($_.DNSServer.ServerAddresses -join '|')")
}
'@
$report1 = @()
$report2 = @()
foreach($vms in Get-VM test1 |? {$_.PowerState -eq 'Poweredon'}){
$report1 += Invoke-VMScript -VM $vms -GuestUser 'administrator' -GuestPassword 'Adm1n1strat0r' -ScriptType Powershell -ScriptText $cmd1 |
Select -ExpandProperty ScriptOutPut |
ConvertFrom-Csv
$report2 += Invoke-VMScript -VM $vms -GuestUser 'administrator' -GuestPassword 'Adm1n1strat0r' -ScriptType Powershell -ScriptText $cmd2 |
Select -ExpandProperty ScriptOutPut |
ConvertFrom-Csv
}
$report1 | Export-Csv c:\report1.csv -NoTypeInformation -UseCulture
$report2 | Export-Csv c:\report2.csv -NoTypeInformation -UseCulture
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Note that the Invoke-VMScript cmdlet returns an object, and that all the output of the script is returned as text ([string]).
$output2.ScriptOutput
In that text you will have to extract (RegEx, ConvertFrom-Csv...) the actual data you want to use.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Can you please rewrite it as I am failing to fetch the output.
Exactly which information from the 'route print' output do you want to extract to the CSV?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
What I am trying to achieve with this script is HostName,IPconfig details for all active adapters,Persistent Routes info
====CSV=====> HostName || IP Info || Persistent Route
Something like this for IPConfig and route print
For Ipconfig output to CSV (Invoke Get-NetIPConfiguration -Detailed)
ComputerName : test01
InterfaceDescription : vmxnet3 Ethernet Adapter
NetAdapter.LinkLayerAddress : 00-50-56-BB-78-9A
IPv4Address : 10.XX.XX.XX
IPv4DefaultGateway :
NetIPv4Interface.NlMTU : 1500
NetIPv4Interface.DHCP : Disabled
DNSServer : 8.8.8.8
ComputerName : test01
InterfaceAlias : VLAN 3504
InterfaceIndex : 13
InterfaceDescription : vmxnet3 Ethernet Adapter #2
NetAdapter.LinkLayerAddress : 00-50-56-BB-4F-3A
NetProfile.Name : servereps.local
NetProfile.NetworkCategory : DomainAuthenticated
IPv4Address : 153.71.4.36
IPv4DefaultGateway : 153.71.4.126
NetIPv4Interface.NlMTU : 1500
NetIPv4Interface.DHCP : Disabled
DNSServer : 8.8.8.8
--------------------------------------------------------
For route print output to CSV
Host name & Persistent Routes are enough.
Something like this output..
Persistent Routes:
Network Address Netmask Gateway Address Metric
10.1.10.0 255.255.255.0 10.1.20.254 1
10.1.30.0 255.255.255.0 10.1.20.254 1
Hi LucD, I modified the script as below but two steps are missing
1. Unable to get gateway and DNS property in powershell output. Please correct me to fetch those details (@{N="Gateway";E={$_.IPv4DefaultGateway}},@{N="DNS";E={$_.DNSServer}})
2.In $output variable I have required details. How to export those details to .csv
Can we export both(IPconfig & DNS) in $output at a single time or should i run two scripts for route print and IP config. Please suggest.
foreach($vms in Get-VM test1 |? {$_.PowerState -eq 'Poweredon'}){
$command = @'
$routeprint=Get-WmiObject Win32_IP4PersistedRouteTable |Select-Object @{N="Host";E={$_.PSComputerName}},@{N="Name";E={$_.Name}},@{N="Mask";E={$_.Mask}},@{N="Gateway";E={$_.NextHop}},@{N="Metric";E={$_.Metric1}}
$IPADD=Get-NetIPConfiguration -Detailed |Select-Object @{N="Host";E={$_.ComputerName}},@{N="Interface";E={$_.InterfaceDescription}},@{N="Interface Alias";E={$_.InterfaceAlias}},@{N="MAC";E={$_.NetAdapter.LinkLayerAddress}},@{N="Status";E={$_.NetAdapter.Status}},@{N="DNS Suffix";E={$_.NetProfile.Name}},@{N="NetworkCategory";E={$_.NetProfile.NetworkCategory}},@{N="IP";E={$_.IPv4Address}},@{N="Gateway";E={$_.IPv4DefaultGateway}},@{N="DNS";E={$_.DNSServer}}
$report=$routeprint+$IPADD
$report
'@
$output=Invoke-VMScript -VM $vms -GuestUser 'administrator' -GuestPassword 'Adm1n1strat0r' -ScriptType Powershell -ScriptText $command | Select -ExpandProperty ScriptOutPut
$output
}
Sample Output :
1. Properties fixed
2. One way of exporting could be like this.
Note that the script produces two separate CSV files, you could merge the info and produce one CSV file
$cmd1 = @'
Write-Host "Host,Name,Mask,Gateway,Metric"
Get-WmiObject Win32_IP4PersistedRouteTable | %{
Write-Host "$($_.PSComputerName),$($_.Name),$($_.Mask),$($_.NextHop),$($_.Metric1)"
}
'@
$cmd2 = @'
Write-Host "Host,Interface,Interface Alias,MAC,Status,DNS Suffix,NetworkCategory,IP,Gateway,DNS"
Get-NetIPConfiguration -Detailed | %{
Write-Host ("$($_.ComputerName),$($_.InterfaceDescription),$($_.InterfaceAlias),$($_.NetAdapter.LinkLayerAddress)," +
"$($_.NetAdapter.Status),$($_.NetProfile.Name),$($_.NetProfile.NetworkCategory),$($_.IPv4Address.IPv4Address),$($_.IPv4DefaultGateway.Nexthop)," +
"$($_.DNSServer.ServerAddresses -join '|')")
}
'@
$report1 = @()
$report2 = @()
foreach($vms in Get-VM test1 |? {$_.PowerState -eq 'Poweredon'}){
$report1 += Invoke-VMScript -VM $vms -GuestUser 'administrator' -GuestPassword 'Adm1n1strat0r' -ScriptType Powershell -ScriptText $cmd1 |
Select -ExpandProperty ScriptOutPut |
ConvertFrom-Csv
$report2 += Invoke-VMScript -VM $vms -GuestUser 'administrator' -GuestPassword 'Adm1n1strat0r' -ScriptType Powershell -ScriptText $cmd2 |
Select -ExpandProperty ScriptOutPut |
ConvertFrom-Csv
}
$report1 | Export-Csv c:\report1.csv -NoTypeInformation -UseCulture
$report2 | Export-Csv c:\report2.csv -NoTypeInformation -UseCulture
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks Guru This worked perfectly.
Mean time I managed Like this to export to CSV.
Anyway I prefer your code. Thanks a lot.
foreach($vms in Get-VM test1 |? {$_.PowerState -eq 'Poweredon'}){
$command = @'
$routeprint=Get-WmiObject Win32_IP4PersistedRouteTable |Select-Object @{N="Host";E={$_.PSComputerName}},@{N="Name";E={$_.Name}},@{N="Mask";E={$_.Mask}},@{N="Gateway";E={$_.NextHop}},@{N="Metric";E={$_.Metric1}} |ConvertTo-Csv -NoTypeInformation
#$IPADD=Get-NetIPConfiguration -Detailed |Select-Object @{N="Host";E={$_.ComputerName}},@{N="Interface";E={$_.InterfaceDescription}},@{N="Interface Alias";E={$_.InterfaceAlias}},@{N="MAC";E={$_.NetAdapter.LinkLayerAddress}},@{N="Status";E={$_.NetAdapter.Status}},@{N="DNS Suffix";E={$_.NetProfile.Name}},@{N="NetworkCategory";E={$_.NetProfile.NetworkCategory}},@{N="IP";E={$_.IPv4Address}},@{N="Gateway";E={$_.IPv4DefaultGateway}},@{N="DNS";E={$_.DNSServer}} |ConvertTo-Csv -NoTypeInformation
#$report=$routeprint+$IPADD
#$report
#$routeprint
#$IPADD
'@
$output=Invoke-VMScript -VM $vms -GuestUser 'administrator' -GuestPassword 'Adm1n1strat0r' -ScriptType Powershell -ScriptText $command | Select -ExpandProperty ScriptOutPut
$tst=$output |ConvertFrom-Csv
$tst |Export-Csv -Path 'D:\All Excel Info\tststst.csv' -NoTypeInformation -NoClobber
}