This discussion has been locked. The information referenced herein may be inaccurate due to age, software updates, or external references.
You can no longer post new replies to this discussion. If you have a similar question you can start a new discussion in this forum.

SAM using specific PowerShell code

Hopefully somone can point me in the right direction as to what i am doing wrong here.

I've written a PS script that takes a hostname (or IP) input and then queries a remote host gets the data and returns it in the statistics format as required by SAM.

I am logged into the SW server as the service account that is specified in my template and i can run the PS sucesfully against our server estate (i tried 30 servers and got the results i was expecting).

My issue is when i put the same script into the template and run it from the, it always returns "Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))" as the script result.
i have posted an excerpt of the script below.

it's passed one variable which is the hostname and can be tested by replacing the SERVERNAME value in line 5

---Begin ---
Try {

$CBSRebootPend = $null

$HKLM = [UInt32] "0x80000002"
$WMI_Reg = [WMIClass] "\\SERVERNAME\root\default:StdRegProv"

$RegSubKeysCBS = $WMI_Reg.EnumKey($HKLM,"SOFTWARE\Microsoft\Windows\CurrentVersion\Component Based Servicing\")
$CBSRebootPend = $RegSubKeysCBS.sNames -contains "RebootPending"

Write-Host "Message.CBServicing: " $CBSRebootPend
if ($CBSRebootPend) {
Write-Host "Statistic.CBServicing: 3"
} else {
Write-Host "Statistic.CBServicing: 2"
}
Write-host "Message.Errors: None"
Write-host "Statistic.Errors: 2"
} catch {
Write-Host "Message.CBServicing: Unknown"
Write-Host "Statistic.CBServicing: 4"
Write-Host "Message.WindowsUpdate: Unknown"
Write-host "Message.Errors: Line "$_.InvocationInfo.ScriptLineNumber " -- " $_.Exception.Message
Write-host "Statistic.Errors: 4"
}
exit(0)
--- End ---

  • I would try changing this setting when editing the template.

    Under the advanced section of the template it defaults to 'Agent' for preferred polling method. So if you have Orion Agents installed it'll actually run that PowerShell code on the remote server. If your desire is to run it on the Orion server, then switching it to 'Agentless' will do it.

    You can also test the x86/x64 drop down too and see if that changes anything.

  • Thanks for the advice chad. We are running agentless which is what it's set to currently. I have also tried running it in both x86 and x64 mode's and sadly that makes no difference.

  • I'm assuming in the component monitor that you have the 'Run the script under specified account' checked?

    If that doesn't work that you'll most likely need to update this section of your script to use the Invoke-WmiMethod or Get-WmiObject.

    $WMI_Reg = [WMIClass] "\\SERVERNAME\root\default:StdRegProv"
    
    $RegSubKeysCBS = $WMI_Reg.EnumKey($HKLM,"SOFTWARE\Microsoft\Windows\CurrentVersion\Component

    Once you switch to either of those methods you'll be able to add a -Credential option to the command and force the creds you have assigned in the template like this -Credentials ${creds}

     docs.microsoft.com/.../invoke-wmimethod

  • Yes, have that ticked. I was really trying to avoid having to re-write them. I have a bunch of scripts that all use the class like this i want to just modify the output to fit SW and then off we go. Annoying thing is they just work from the CLI.

  • , curious if you update your $WMI_Reg line to this if that fixes it.

    $WMI_Reg = get-wmiobject -list "StdRegProv" -namespace root\default -computername ${IP} -Credential ${CREDENTIAL}

    The ${IP} should pass in the IP of the node the template is assigned too and ${Credential} should pass in the assigned credential on that component monitor.

  • I owe you a pint, that's working perfectly! I replaced the ${IP} with $args[0] so that I can pull the value from the template in case it needs changing for whatever reason at a later date.

    Just need to tweak the others in the same way when I import them into SAM and it should be golden!

  • Excellent! Glad to hear it worked!