VMware Cloud Community
ATX_Andy
Contributor
Contributor

vm health check network - ping default gw

Does anyone have a powercli script that could login to guest vm's (from a list), run a ping on the guest and return the output (success or fail) to a text file?

Reply
0 Kudos
11 Replies
nielse
Expert
Expert

You could try this with the Invoke-VMscript cmdlet.

Maybe something like this:

# vCenter and command Details
$vCenter = "<IP>"
$command = "ping -n 1 <IP-TO-PING>"

# CSV file
$vmlist = Import-CSV C:\windows.csv

# Connect to vCenter
$VIServer = Connect-VIServer $vCenter -User '<username>' -Password '<password>' | Out-Null

foreach ($item in $vmlist) {
   $vmname = $item.vmname
   $user = $item.username
   $pass = $item.password

    Invoke-VMScript -VM $vmname -ScriptText $command -GuestUser $user -GuestPassword $pass
}

# Disonnect from vCenter
Disconnect-VIServer -Confirm:$false

Place the csv file under c:\windows.csv and enter vm's like this:

vmname,username,password
server01,Administrator,mypassword1234

server02,Administrator,mypassword

server03,Administrator,mypassword4321

server04,Administrator,mypasswordtest

@nielsengelen - http://foonet.be - VCP4/5
ATX_Andy
Contributor
Contributor

Wow, thanks. Is it possible to add the gw ip as a variable since they would be different ip's for different vm's?

How do I capture the results of the ping into a file?

Reply
0 Kudos
nielse
Expert
Expert

What exactly do you mean with the gateway is different?

You can run the script via PowerCLI and save the output by writing this to a file for eg:

.\pingtest.ps1 > output.txt

This will give the output in a text file which u can then parse.

@nielsengelen - http://foonet.be - VCP4/5
ATX_Andy
Contributor
Contributor

Just mean in my list of vm's, they belong to different networks.

I'm thinking my csv could look like this

vm, ip, username, pasword

Reply
0 Kudos
LucD
Leadership
Leadership

Create the contents of $command inside the loop.

# vCenter Details
$vCenter = "<IP>"
# CSV file
$vmlist = Import-CSV C:\windows.csv
# Connect to vCenter
$VIServer = Connect-VIServer $vCenter -User '<username>' -Password '<password>' | Out-Null
foreach ($item in $vmlist) {
   $vmname = $item.vm
  
$user = $item.username
   $pass = $item.password
  
$command = "ping -n 1 $item.ip"

   Invoke-VMScript -VM $vmname -ScriptText $command -GuestUser $user -GuestPassword $pass
}
# Disonnect from vCenter Disconnect-VIServer -Confirm:$false


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

Reply
0 Kudos
amitkadam10
Contributor
Contributor

Hello Luc ,

I used this script to ping the GW and DNS of the machine.

My Input CSV files looks like

VM,username,password

Test,admin,vmware

when i run the script i get the following error

pastedImage_0.png

Also i do not understand the script here .

$command = "ping -n 1 $item.ip" will it ping Gw and DNS automatically ? should i specify them in the CSV format and modify my command and CSV file lile below:

where is $item.ip varaible declared ?

CSV files

VM,GW,DNSUsername,Password

Test,1.1.1.1,2.2.2.2,admin,vmware

and the command as

$command = "ping -n 1 $item.gw;ping -n 1 $item.dns"

Reply
0 Kudos
LucD
Leadership
Leadership

The script assumes that each row in the CSV contains the following info: vm, ip, username, pasword

But the error you are getting seems to indicate that the VM is not found.


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

Reply
0 Kudos
amitkadam10
Contributor
Contributor

when i do Get-VM Test(machine name) i get the output not sure why the script does not find the same .

is there any way i can sahre my screen with you and show error via skype

Reply
0 Kudos
amitkadam10
Contributor
Contributor

can you help me the example csv file with some example values

pastedImage_0.png

this is my CSV file ... not sure what is going wrong

pastedImage_1.png

Reply
0 Kudos
LucD
Leadership
Leadership

You seem to be passing the complete row instead of just $item.ip.


Can you attach, as a file, the script you are using?

It looks as if there might be an issue in there.


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

Reply
0 Kudos
amitkadam10
Contributor
Contributor

Hello Luc ,

I  was able to figure it out  it here is script as below , i was missing the    $ip = $item.ip in the script and earlier it was taking the entire input as one full string as the CSV format was not correct.

$vmlist = Import-CSV C:\Users\amit.kadam\Desktop\ServerName.csv

foreach ($item in $vmlist) {

   $vmname = $item.vm

   $user = $item.username

   $pass = $item.password

   $ip = $item.ip

$command = "xxxxxxxx"

Invoke-VMScript -VM $vmname -ScriptText $command -GuestUser $user -GuestPassword $pass

}

Reply
0 Kudos