VMware Cloud Community
marco_shaw
Enthusiast
Enthusiast

Passing the VM name to a script

OK, this isn't directly related to the PowerShell VI TK...

I need to help my VI admin with a PowerShell script. When the CPU spikes on a VM, he wants some WMI results from the VM.

The script will run on the VI server, but how can I possibly pass the VM name to my script so it knows where to make the WMI call to?

(Side note: the admin really likes PowerShell so it is only a matter of time before he starts using the new toolkit.)

Marco

Reply
0 Kudos
8 Replies
LucD
Leadership
Leadership

A WMI call to the guest would normally need to use the hostname of the guest as defined in the OS.

Are the hostnames of your guest the same as the guestnames in VI ?


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

Reply
0 Kudos
marco_shaw
Enthusiast
Enthusiast

Yes, the VM name and hostname match. So I am told...

Reply
0 Kudos
LucD
Leadership
Leadership

The easiest method would be to use Powerscripter from icomasoft.

It's a plugin that you install on the VI Client.

From the VI client you can then launch PowerShell scripts on hosts and guests.

Parameter passing is built in.

See also Carter's blog entry.


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

Reply
0 Kudos
marco_shaw
Enthusiast
Enthusiast

I don't really want the easy way... I've asked my VI admin if we can run 2 actions on an alert: "log event", and "run script". I'll maybe be able to grab the hostname/VM name from the log entry from my script...

Reply
0 Kudos
LucD
Leadership
Leadership

Oh I see, you should have said that you wanted to run the script as a result of an alarm.

I suspect you want to create an alarm on a virtual machine ?

Running a script is a supported action, but what do you mean by "log event" ?


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

Reply
0 Kudos
marco_shaw
Enthusiast
Enthusiast

(Might be a duplicate, my first reply timed out when posting it seems).

I want to have a central Posh script do WMI queries to the VM that caused the alert.

So, the alert in VI, runs the Posh script to query the VM via WMI. Now I need to pass the Posh script that VM name that the alert is "tied" to.

Unless, there's some built-in parameters, I guess I can't do this.

What I'm also wondering if there's a "log event" action for alerts? Then I could do the "log event", then "run script" actions, where my script will query the log written to to capture the VM name...

Reply
0 Kudos
LucD
Leadership
Leadership

There is no "log event" action for alarms, afaik.

You can pass some parameters to the script.

Have a look at Alarms and Events in the VI client help file.

It also contains some samples.


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

Reply
0 Kudos
hugopeeters
Hot Shot
Hot Shot

The "Run Script" feature in Alarms supports the following parameters:

{eventDescription} – Full formatted message for alarm triggering event.
{targetName} – Name of the entity name where the alarm is triggered. 
{alarmName} – Name of the alarm that is triggered.
{triggeringSummary} – Summary info of the alarm with triggering values.
{declaringSummary} – Summary info of the alarm declaration.
{oldStatus} – Alarm status before it is triggered.
{newStatus} – Alarm status after it is triggered.
{target} – Inventory object as triggering alarm.

You could use the targetname, but this is the name of the VM, not the hostname of the VM OS. You can process this VM name in a script like so:

$HostName = (Get-VM "VMName" | Get-View).Guest.HostName

Reply
0 Kudos