VMware Cloud Community
Sureshadmin
Contributor
Contributor
Jump to solution

Need a powershell script to find the PID of VM's in a ESX box

Hi,

I need a powershell script to list out the PID of all the VM's running in a ESX box.

The script should prompt for ESX box name and for root password.

Expected Output:

VM Name | PID

Thanks in Advance!

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

My mistake, the dot (any character) is eating the rest of the message.

Try with

$mask = [regex]"vmid=(\d+)\s+([\w-]+)"

This will accept what regex considers word characters or the dash ( - ).

____________

Blog: LucD notes

Twitter: lucd22


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

View solution in original post

Reply
0 Kudos
8 Replies
LucD
Leadership
Leadership
Jump to solution

Is it the PID or the VMID that you need to see ?

If you could tell us for what you would need the number, that would make it perhaps a bit easier.

____________

Blog: LucD notes

Twitter: lucd22


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

Reply
0 Kudos
Sureshadmin
Contributor
Contributor
Jump to solution

hi Luc, thanks for ur reply.

I was looking for the PID of the virtual machine to kill some virtual machine process.

If you can give me a script to list all VMID also it would work for me, will use the vm-support command to kill it.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Afaik, you can only retrieve this information from the COS (so the following is not valid for ESXi).

It requires that root can do SSH to the COS or that you have sudo configured correctly in the COS

$User = <account-name>
$Pswd = <password>
$hostName = <ESX-hostname>
$plink = "<PuTTY directory>\plink.exe"

$plinkoptions = " -v -batch -pw $Pswd"
$cmd1 = '/usr/bin/vm-support -x'

$remoteCommand = '"' + $cmd1 + '"'
$command = $plink + " " + $plinkoptions + " " + $User + "@" + $hostName + " " + $remoteCommand

$msg = Invoke-Expression -command $command

$report = @()
$mask = [regex]"vmid=(\d+)\s+(\w+)"
$mask.Matches($msg) | %{
	$row = "" | Select VMname, VMID
	$row.VMname = $_.Groups[2]
	$row.VMID = $_.Groups[1]
	$report += $row
}
$report

____________

Blog: LucD notes

Twitter: lucd22


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

Sureshadmin
Contributor
Contributor
Jump to solution

hi Luc,

Thanks for the script.

We have the names of the virtual machines in the format of abc-abcd10-xp. The result of the script gives only the frist 3 characters of the virtual machines like

VMname VMID

-


abc 1111

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

That is because the regex expression didn't take special characters (or white spaces) into account.

Replace this line

$mask = [regex]"vmid=(\d+)\s+(\w+)"

by this line

$mask = [regex]"vmid=(\d+)\s+(.+)"

That should take care of the dashes in the names.

____________

Blog: LucD notes

Twitter: lucd22


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

Reply
0 Kudos
Sureshadmin
Contributor
Contributor
Jump to solution

Luc,

Now it shows the full name of the VM, but it gives a single truncated record for all VM's

The output is like this,

VMname VMID

-


-


xxx-xxxxxxx-xp vmid=1111 xxx-xxxxx-XP vmid=2222 xxx-xx... 3333

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

My mistake, the dot (any character) is eating the rest of the message.

Try with

$mask = [regex]"vmid=(\d+)\s+([\w-]+)"

This will accept what regex considers word characters or the dash ( - ).

____________

Blog: LucD notes

Twitter: lucd22


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

Reply
0 Kudos
Sureshadmin
Contributor
Contributor
Jump to solution

Works Perfect. Thank you so much, Luc. I admire your patience to answer all the queries till last and solving out the problem.

Reply
0 Kudos