/build/static/layout/Breadcrumb_cap_w.png

Custom Inventory Rule Powershell failing in KACE but not locally?

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
  • Is there a reason why you are not just using RegistryValueReturn? - Desktop Jockey 7 years ago
    • That would return the whole registry value rather than just the host field. - Bsickinger 7 years ago
  • 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) - Desktop Jockey 7 years ago
    • 32 vs 64 is what got me Thank you! - Bsickinger 7 years ago

Answers (1)

Posted by: Desktop Jockey 7 years ago
Second Degree Green Belt
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)

Don't be a Stranger!

Sign up today to participate, stay informed, earn points and establish a reputation for yourself!

Sign up! or login

Share

 
This website uses cookies. By continuing to use this site and/or clicking the "Accept" button you are providing consent Quest Software and its affiliates do NOT sell the Personal Data you provide to us either when you register on our websites or when you do business with us. For more information about our Privacy Policy and our data protection efforts, please visit GDPR-HQ