/build/static/layout/Breadcrumb_cap_w.png

Systems Management Question


Custom Inventory Rule Powershell failing in KACE but not locally?

05/19/2017 1747 views
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!

All 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
Second Degree Green Belt

This content is currently hidden from public view.
Reason: Removed by member request For more information, visit our FAQ's.
 
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