VMware Cloud Community
Thk99
Contributor
Contributor
Jump to solution

Script to export vms if they are connected to the network and reachable.

Hey Guys,

I'm relatively new to VMware and looking for a way to check if specific VMs (from a CSV file that I prepared) are connected to their network adapters and reachable in their network after restarting them.

Hope to hear from u guys. 🙂 

Thanks!

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Why are you using Out-File in the 2nd part?

You can read the CSV you created in the 1st part, add the new fields (mind with different column names) and then export those new rows to the CSV


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
4 Replies
LucD
Leadership
Leadership
Jump to solution

You can use the Get-VM | Get-NetworkAdapter cmdlets to check if the vNIC(s) are connected.
To check if a VM is reachable over the network you might want to use the Test-Connection cmdlet.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
Thk99
Contributor
Contributor
Jump to solution

Thank you for the reply. 

What I am trying to do is rightsizing a few hundred VMs and I want to compare them afterwards. Are they on? Did the CPU & RAM configuration change? Are they connected to their vNICs? Are they reachable? 

The Test-Connection cmdlet wouldn't work because some of them are in different networks , that i can't reach from my jump host


"----------BEFORE RIGHTSIZING----------"|Export-Csv -Path "C:\Powershell\VM_Rightsizing\logtest.csv"

foreach ($item in $vmlist)
{
$vmname = $item.vmname

Get-VM -Name $vmname |Select-Object -property Name,MemoryGB,NumCpu,PowerState,NetworkAdapter |
Export-Csv -Path "C:\Powershell\VM_Rightsizing\logtest.csv"
}


"----------AFTER RIGHTSIZING----------"|Export-Csv -Append -Path "C:\Powershell\VM_Rightsizing\logtest.csv"

foreach ($item in $vmlist)
{
$vmname = $item.vmname

Get-VM -Name $vmname |Select-Object -property Name,MemoryGB,NumCpu,PowerState,NetworkAdapter |
Out-File -Append -FilePath "C:\Powershell\VM_Rightsizing\logtest.csv"
}

Is it possible to write "After rightsizing" next to "Before Rightszing" and not under ?

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Why are you using Out-File in the 2nd part?

You can read the CSV you created in the 1st part, add the new fields (mind with different column names) and then export those new rows to the CSV


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
Thk99
Contributor
Contributor
Jump to solution

Between those Out-File i am changing the cpu and ram config. I want to see the config of the vms before and after the change in one csv 

Maybe this script can be optimized in diffrent parts


Start-Transcript -path C:\Powershell\VM_Rightsizing\errorlogs.txt

Connect-VIServer -Server server.xxxxx.com

$vmlist = Import-CSV C:\Powershell\VM_Rightsizing\ListofVMstest.csv

foreach ($item in $vmlist)
{
$vmname = $item.vmname
Get-VM -VM $vmname | Where-Object {$_.PowerState -eq 'PoweredOn'}|Select-Object -property Name |
Export-Csv -Path "C:\Powershell\VM_Rightsizing\powon_vms.csv"

}

 

"----------BEFORE RIGHTSIZING----------"|Export-Csv -Path "C:\Powershell\VM_Rightsizing\logtest.csv"

foreach ($item in $vmlist)
{
$vmname = $item.vmname

Get-VM -Name $vmname |Select-Object -property Name,MemoryGB,NumCpu,PowerState,NetworkAdapter |
Export-Csv -Append -Path "C:\Powershell\VM_Rightsizing\logtest.csv"
}

foreach ($item in $vmlist)
{
$vmname = $item.vmname
Shutdown-VMguest -VM $vmname -Confirm:$false | Where-Object {$_.PowerState -eq 'PoweredOn'}

}

Sleep 60

foreach ($item in $vmlist)
{

$vmname = $item.vmname
$cpu = $item.cpu
$mem = [int]$item.mem * 1024
Set-VM -VM $vmname -NumCpu $cpu -MemoryMB $mem -Confirm:$false

}

Sleep 30

$powonlist = Import-CSV C:\Powershell\VM_Rightsizing\powon_vms.csv

foreach ($item in $vmlist)
{
$vmname = $powonlist.Name
Start-VM -VM $vmname
sleep 10
}

"----------AFTER RIGHTSIZING----------"|Export-Csv -Append -Path "C:\Powershell\VM_Rightsizing\logtest.csv"

foreach ($item in $vmlist)
{
$vmname = $item.vmname

Get-VM -Name $vmname |Select-Object -property Name,MemoryGB,NumCpu,PowerState,NetworkAdapter |
Out-File -Append -FilePath "C:\Powershell\VM_Rightsizing\logtest.csv"
}


Stop-Transcript

0 Kudos