I'm trying to run a custom inventory rule to pull the name of a VM's Hyper-V Host server with the following powershell script:

(get-item "HKLM:\SOFTWARE\Microsoft\Virtual Machine\Guest\Parameters").GetValue("HostName")

I need to pull this information so I can make smart labels to manage VM's based on what host they are located on.  I can run the powershell on the VM locally and get the desired results.  I can save the .ps1 file to an accessible file share and call it on the VM locally and get the desired results.  However, whenever I try to run it as an inventory rule I get errors as results.  

This is the custom inventory rule I'm using:
ShellCommandTextReturn(powershell -ExecutionPolicy Bypass -file "\\FILESHAREPATH\hypervhost.ps1" )

as I said before I can run powershell -ExecutionPolicy Bypass -file "\\FILESHAREPATH\hypervhost.ps1"  on the VMand get the desired results.However, when the inventory rule runs this is what I get:

get-item : Cannot find path 'HKLM:\SOFTWARE\Microsoft\Virtual 
Machine\Guest\Parameters' because it does not exist.
At \\FILESHAREPATH\hypervhost.ps1:1 char:8
+ $key = get-item "HKLM:\SOFTWARE\Microsoft\Virtual Machine\Guest\Parameters"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (HKLM:\SOFTWARE\...uest\Paramete 
rs:String) [Get-Item], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetIt 
emCommand

You cannot call a method on a null-valued expression.
At \\FILESHAREPATH\hypervhost.ps1:2 char:1
+ $key.GetValue("HostName")
+ ~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull

Additionally, If I try to add JUST the powershell without calling a script like this:
ShellCommandTextReturn(Powershell "(get-item 'HKLM:\SOFTWARE\Microsoft\Virtual Machine\Guest\Parameters').GetValue('HostName')" )

The powershell again works if I run it from the VM but if I try it this way in the K1000, the custom inventory rule does not even populate. Not even with errors. I can't figure out why the code would work when run on the VM but not when run within the rule.  Is there anything obvious anyone sees that I am doing wrong?
2 Comments   [ + ] Show Comments

Comments

  • Is there a reason why you are not just using RegistryValueReturn?
    • That would return the whole registry value rather than just the host field.
  • This should return just the 'Hostname' value:
    RegistryValueReturn(HKLM64\SOFTWARE\Microsoft\Virtual Machine\Guest\Parameters, HostName, REG_SZ)

    If that is not acceptable, You may want to try calling on the 64bit powershell (Kace agent by default will run 32bit)
    • 32 vs 64 is what got me Thank you!
Please log in to comment

Answers

0
This should return just the 'Hostname' value:
RegistryValueReturn(HKLM64\SOFTWARE\Microsoft\Virtual Machine\Guest\Parameters, HostName, REG_SZ)

If that is not acceptable, You may want to try calling on the 64bit powershell (Kace agent by default will run 32bit)
Answered 05/19/2017 by: Desktop Jockey
Purple Belt

Please log in to comment
This content is currently hidden from public view.
Reason: Removed by member request
For more information, visit our FAQ's.

Answer this question or Comment on this question for clarity

Share