VMware Cloud Community
Michelle_Laveri
Virtuoso
Virtuoso

How to get the moref value for an ESX host

Part of a powershell script I'm writing requires the ESX host moref value... like this:

host-746

I've found away of finding the moref value with:

$vmhost = "esx4.vi4book.com"

$esxhost = Get-VMHost $vmhost

$hostview = $esxhost | Get-View

$hostview.moref

But when I insert $hostview.moref into the method i'm using it returns an error... I think it because $hostview.moref doesn't return just the moref value but other parameters as well, and need away of just present the raw value - host-746...

Any ideas?

Regards

Mike Laverick

RTFM Education

http://www.rtfm-ed.co.uk

Author of the SRM Book: http://www.lulu.com/content/4343147

Regards
Michelle Laverick
@m_laverick
http://www.michellelaverick.com
Reply
0 Kudos
25 Replies
Michelle_Laveri
Virtuoso
Virtuoso

Found it:

$vmhost = "esx4.vi4book.com"

$targethostMoRef = (get-VMHost $vmhost | get-view).MoRef

$targethostMoRef.value

Regards

Mike Laverick

RTFM Education

http://www.rtfm-ed.co.uk

Author of the SRM Book: http://www.lulu.com/content/4343147

Regards
Michelle Laverick
@m_laverick
http://www.michellelaverick.com
Reply
0 Kudos
LucD
Leadership
Leadership

What method were you trying to call ?


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

Reply
0 Kudos
alanrenouf
VMware Employee
VMware Employee

Or you could have just put your last object inside of brackets and used the value property as below:

$vmhost = "esx4.vi4book.com"

$esxhost = Get-VMHost $vmhost

$hostview = $esxhost | Get-View

($hostview.moref).value

The trick here is to use the Get-Member cmdlet to find out what you are looking at, if you would have piped $hostview.moref to get-member like ....$hostview.moref | Get-Member

You would have seen the object had 2 properties, Type and Value. I often add things like this to a variable and view them in powergui script editor as it gives a nice GUI view of the properties you are returning. I can show you at the next UK vmug if you like Smiley Wink

If you found this information useful, please consider awarding points for Correct or Helpful.

Alan Renouf

Blog: http://virtu-al.net Twitter: http://twitter.com/alanrenouf Co-author of the PowerCLI Book: http://powerclibook.com
Reply
0 Kudos
Michelle_Laveri
Virtuoso
Virtuoso

What method were you trying to call ?

I was writing a script to add & license an esxhost...

http://www.rtfm-ed.co.uk/?p=1536

$vmhost = “esx4.vi4book.com”

$targethostMoRef = (get-VMHost $vmhost | get-view).MoRef

$si = Get-View ServiceInstance

$LicManRef=$si.Content.LicenseManager

$LicManView=Get-View $LicManRef

$licassman = Get-View $LicManView.LicenseAssignmentManager

$licassman.UpdateAssignedLicense($targethostMoRef.value,”YOUR LIC KEY”,”vSphere4 Enterprise Plus (1-12 cores per CPU”)

Regards

Mike Laverick

RTFM Education

http://www.rtfm-ed.co.uk

Author of the SRM Book: http://www.lulu.com/content/4343147

Regards
Michelle Laverick
@m_laverick
http://www.michellelaverick.com
Reply
0 Kudos
LucD
Leadership
Leadership

Ok, it seems that this method doesn't ask for a MoRef but for a "ID of the entity" which is a string and not a MoRef.

Strange that this entity property diverts from the most common type for an entity property, which is a MoRef.

Perhaps this method was written by another lab Smiley Wink


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

Reply
0 Kudos
Michelle_Laveri
Virtuoso
Virtuoso

Yeah, I'm not helped by general and relative ignorance of the SDK. There's a been a couple of times when I've been looking for away to apply a setting, only to find its not in the SDK...

Anyway, I'm working on another ESX4i configuration powershell. That to setup the Power Management Settings...

Here's where I've got so far:

$vmhost = "esx4.vi4book.com"

$esxhost = Get-VMHost $vmhost

$hostview = $esxhost | Get-View

$impi = New-Object VMware.Vim.HostIpmiInfo

$impi.bmcIpAddress = "192.168.3.204"

$impi.bmcMacAddress = "00:16:35:37:F8:02"

$impi.login = "vmware_dpm_user"

$impi.password = "password"

$hostview.UpdateIpmi($ipmi)

trouble is it comes back with a "Exception calling "UpdateIpmi" with "1" argument(s): "ipmiInfo". Seems like I'm missing an argument... or improviding the date incorrectly...

Any pointers?

Regards

Mike Laverick

RTFM Education

http://www.rtfm-ed.co.uk

Author of the SRM Book: http://www.lulu.com/content/4343147

Regards
Michelle Laverick
@m_laverick
http://www.michellelaverick.com
Reply
0 Kudos
sradnidge
Enthusiast
Enthusiast

I'm trying to do the same, with no joy. According to the SDK docs, the login and password parameters must be null terminated strings. So it should be

$impi.login = "vmware_dpm_user`0"

$impi.password = "password`0"

However whenever I try that I get HTTP bad request errors! Maybe it's a bug? Carter or someone?

Reply
0 Kudos
Michelle_Laveri
Virtuoso
Virtuoso

I'm trying to do the same, with no joy. According to the SDK docs, the login and password parameters must be null terminated strings. So it should be

$impi.login = "vmware_dpm_user`0"

$impi.password = "password`0"

However whenever I try that I get HTTP bad request errors! Maybe it's a bug? Carter or someone?

Just to clarify...

The password/username strings require an apostrophe ' and 0 (zero) according to the SDK guide?

I'm looking at the MAC address values - they appear to need to be null terminated.

"MAC address of the BMC on the host. The MAC address should be of the form xx:xx:xx:xx:xx:xx where each x is a hex digit. It should be null terminated. "

Wish there was a better definition of that that actually means!

Regards

Mike Laverick

RTFM Education

http://www.rtfm-ed.co.uk

Author of the SRM Book: http://www.lulu.com/content/4343147

Regards
Michelle Laverick
@m_laverick
http://www.michellelaverick.com
Reply
0 Kudos
Michelle_Laveri
Virtuoso
Virtuoso

Tried that with the '0 value for password/username/mac address...

I got the same error back:

"Exception calling "UpdateIpmi" with "1" argument(s): "ipmiInfo"

At C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\dpmsettings.ps1:11 char:21

+ $hostview.UpdateIpmi( <<<< $ipmi)"

Regards

Mike Laverick

RTFM Education

http://www.rtfm-ed.co.uk

Author of the SRM Book: http://www.lulu.com/content/4343147

Regards
Michelle Laverick
@m_laverick
http://www.michellelaverick.com
Reply
0 Kudos
sradnidge
Enthusiast
Enthusiast

That'll be because it's not an apostrophe - it's a backtick Smiley Happy

So backtick zero (`0) is the null terminator character sequence for PowerShell, which is why I thought it might be necessary for those values that apparently require null termination. However after posting here, i tried it again about 2 hours later with the exact same code I used originally (without any null terminators) and it worked!! The only difference I could think of was the first time I tried it, none of the hosts in the cluster had DPM details configured (the cluster had it enabled of course). Then I switched on verbose logging in the VI client and manually configured one host to check that I was calling the right method, which indeed it was, and then when I tried it again via the script it worked.

But then I tried the script on another new completely unconfigured cluster, and it worked first time there too... so yeh, no real pattern. Maybe it just doesnt like people who live in the UK :smileygrin:

Reply
0 Kudos
halr9000
Commander
Commander

Let me add a correction here. PowerShell does not have any special null-termination sequence. In PowerShell, a string for example is solely delimited by quotes, and an integer by whitespace, and so forth. There really is no such concept in PowerShell itself. What you are seeing here (perhaps, I haven't even read the whole thread yet), are requirements put in place by a lower-level API.

However, just because another API (I'm being generic here) requires a null-terminated string does not mean that you must append any such sequence to the end of a string in PowerShell. It really depends on the situation, because for example, once you type "a string" in PoewrShell and enclose it in quotes, it's been terminated. PoewrShell can pass that around any .NET assembly all day long and work just fine. When you step outside of your PowerShell cmdlets, outside of .NET assemblies, and into, for example, web services, it's possible that you will need to do something special, but that would be the exception, not the rule.

The reason that backtick-zero was tossed out as 'the" null terminator for PowerShell is because an assumption was made that if you put PowerShell's escape character (the backtick), in front of a zero, then you have a null terminator. This isn't correct.. The way to do null in PowerShell is just the word null with a dollar sign in front of it. As an example, I've seen some COM objects which really have to have $null's passed into their methods, and that might look like this:

$obj = new-object fictional.comObject -comobject
$obj.Method("arg1", $null, "arg3", $null, $null, $null, "arg7")

That having been said, it's entirely possible that backtick-zero is needed here for some reason. I haven't done the research myself and wouldn't want to stick my foot in my mouth on it. I just didn't want people to start tossing that around as a generic solution when they can't get something to work. Null termination is a programmer's concept and really does not belong in a scripting toolkit. By that I mean to say that all interfaces in PowerShell SHOULD be so intelligent (i.e. a higher level) that we'll never need to think in that fashion. If I saw otherwise, then I'd consider that a design flaw and would log a defect against the vendor, whether it's VMware or anyone else.

I hope that makes sense.




[vExpert|http://www.vmware.com/communities/vexpert/], PowerShell MVP, VI Toolkit forum moderator

Author of the book: Managing VMware Infrastructure with PowerShell

Co-Host, PowerScripting Podcast (http://powerscripting.net)

Need general, non-VMware-related PowerShell Help? Try the forums at PowerShellCommunity.org

My signature used to be pretty, but then the forum software broked it. vExpert. Microsoft MVP (Windows PowerShell). Author, Podcaster, Speaker. I'm @halr9000
Reply
0 Kudos
sradnidge
Enthusiast
Enthusiast

Hi Hal,

I agree with you entirely about lower level API's requiring null terminated strings not necessitating that PowerShell needs it, however your statement about string termination and how powershell treats things wrapped in double quotes is not correct - following that logic, the output of Write-Host "This is a string`n" and Write-Host "This is a string`t" would be identical, when clearly they are not. And don't confuse `0 with $null - they are not the same, even get-help about_Special_Characters tells us that.

I agree when dealing purely with .NET the whole way then stuff like null tmerinated strings is of little consequence, but when talking to *nix based web-services like those exposed by ESX or Java based ones like those exposed by vCenter, who knows what crazy shit you might encounter Smiley Happy

Reply
0 Kudos
Michelle_Laveri
Virtuoso
Virtuoso

Stu,

Could you share your working powershell code for enabling DPM here?

Regards

Mike Laverick

RTFM Education

http://www.rtfm-ed.co.uk

Author of the SRM Book: http://www.lulu.com/content/4343147

Regards
Michelle Laverick
@m_laverick
http://www.michellelaverick.com
Reply
0 Kudos
halr9000
Commander
Commander

I checked--you are right that `0 is a special character. However, I maintain that it is largely irrelevant. I've never needed to use it in a few years of PowerShell hacking. There's always a first for everything...




[vExpert|http://www.vmware.com/communities/vexpert/], PowerShell MVP, VI Toolkit forum moderator

Author of the book: Managing VMware Infrastructure with PowerShell

Co-Host, PowerScripting Podcast (http://powerscripting.net)

Need general, non-VMware-related PowerShell Help? Try the forums at PowerShellCommunity.org

My signature used to be pretty, but then the forum software broked it. vExpert. Microsoft MVP (Windows PowerShell). Author, Podcaster, Speaker. I'm @halr9000
Reply
0 Kudos
Michelle_Laveri
Virtuoso
Virtuoso

Anyone got a functioning DPM PS script they would like to share?

Regards

Mike Laverick

RTFM Education

http://www.rtfm-ed.co.uk

Author of the SRM Book: http://www.lulu.com/content/4343147

Regards
Michelle Laverick
@m_laverick
http://www.michellelaverick.com
Reply
0 Kudos
sradnidge
Enthusiast
Enthusiast

Sorry mate yeh, so after you have connected to the vCenter managing the target host, the following works for me and is identical to your code!

$esx = "hostname.fqdn"

$bmcIp = "1.1.1.1"

$bmcMac = "00:00:00:00:00:00"

$login = "dpm_usr"

$password = "a_strong_password"

$esxMoRef = get-vmhost $esx | % {Get-View $_.Id}

$IpmiInfo = New-Object Vmware.Vim.HostIpmiInfo

$IpmiInfo.BmcIpAddress = $bmcIp

$IpmiInfo.BmcMacAddress = $bmcMac

$IpmiInfo.Login = $login

$IpmiInfo.Password = $password

$esxMoRef.UpdateIpmi($IpmiInfo)

Reply
0 Kudos
Michelle_Laveri
Virtuoso
Virtuoso

Well, your script worked 1st time without an error.

So the scripts must similar but not exactly the same....

Anyway, I will go with yours (as it works!)

Gonna add it to my configureESX.ps1 script on RTFM, and blog about this. Full credit to you accordingly...

Regards

Mike Laverick

RTFM Education

http://www.rtfm-ed.co.uk

Author of the SRM Book: http://www.lulu.com/content/4343147

Regards
Michelle Laverick
@m_laverick
http://www.michellelaverick.com
Reply
0 Kudos
sradnidge
Enthusiast
Enthusiast

Nah is cool mate - no need for attribution as you really did have it figured out - just one of those things. Thanks for the offer though - just throw in a swear for me somewhere :smileygrin:

Reply
0 Kudos
alanrenouf
VMware Employee
VMware Employee

As a side note we interviewed Carter on the Get-Scripting podcast the other day, we are editing it at the moment and its should be out end of this week but he mentioned that one of the demo's in his session would be enabling DPM on a cluster of servers.

Mike, I think you are at VMworld, you may want to catch his session.

He also mentioned a tool which will make this kind of thing a lot easier in the future, catch episode 12 for full details Smiley Wink

http://get-scripting.blogspot.com/

Alan Renouf

Blog: http://virtu-al.net Twitter: http://twitter.com/alanrenouf Co-author of the PowerCLI Book: http://powerclibook.com
Reply
0 Kudos