0

The following PowerShell code works locally but I have not been able to convert all of it to use CIM commands. I have been unable to convert the first three queries to CIM. The reason I need to use CIM is because that is how my network currently allows for PowerShell remote access. The test computer has the latest SCCM Client installed.

# Check all locations that would contain a pending reboot flag
$RebootPending = $false
$RebootPending = $RebootPending -or ([wmiclass]'ROOT\ccm\ClientSDK:CCM_ClientUtilities').DetermineIfRebootPending().RebootPending
$RebootPending = $RebootPending -or ([wmiclass]'ROOT\ccm\ClientSDK:CCM_ClientUtilities').DetermineIfRebootPending().IsHardRebootPending
$RebootPending = $RebootPending -or (@(((Get-ItemProperty("HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager")).$("PendingFileRenameOperations")) |
      Where-Object { $_ }
  ).length -ne 0)
$RebootPending = $RebootPending -or (
  @(Get-WmiObject -Query "SELECT * FROM CCM_SoftwareUpdate" -Namespace "ROOT\ccm\ClientSDK" |
      Where-Object {
      $_.EvaluationState -eq 8 -or # patch pending soft reboot
      $_.EvaluationState -eq 9 -or # patch pending hard reboot
      $_.EvaluationState -eq 10 } # reboot needed before installing patch
  ).length -ne 0)

I have managed to convert the last query to Cim but I cannot seem to determine how to convert the other three queries to Cim

Converted Query:

$RebootPending = $RebootPending -or (
  @(Get-CimInstance -CimSession $($Computer.CimSession) -Namespace 'ROOT\ccm\ClientSDK' -Query "SELECT * FROM CCM_SoftwareUpdate" |
      Where-Object {
      $_.EvaluationState -eq 8 -or # patch pending soft reboot
      $_.EvaluationState -eq 9 -or # patch pending hard reboot
      $_.EvaluationState -eq 10 } # reboot needed before installing patch
  ).length -ne 0
)
Keith
  • 689
  • 10
  • 27
  • `[wmiclass]` is just a short-hand accelerator for the `System.Management.ManagementClass` object. You can replace those with cim pretty easily: `(Get-CimInstance -ClassName CCM_ClientUtilities -Namespace root\ccm\clientsdk -CimSession $computer.CimSession).Methodname()` – Maximilian Burszley Jul 06 '18 at 14:57

1 Answers1

0

You can use the Invoke-CimMethod cmdlet if you want to use CIM sessions. For the first two items in your example, call it as follows:

Invoke-CimMethod -ClassName 'CCM_ClientUtilities' `
                 -CimSession $Computer.CimSession `
                 -MethodName 'DetermineIfRebootPending' `
                 -Namespace 'ROOT\ccm\ClientSDK'

You will get an object back like this:

DisableHideTime     : 01/01/1970 00:00:00
InGracePeriod       : False
IsHardRebootPending : False
NotifyUI            : False
RebootDeadline      : 01/01/1970 00:00:00
RebootPending       : True
ReturnValue         : 0
PSComputerName      : 

For the Get-ItemProperty example, call it slightly differently:

Invoke-CimMethod -ClassName 'StdRegProv ' `
                 -CimSession $Computer.CimSession `
                 -MethodName 'GetMultiStringValue' `
                 -Namespace 'ROOT\Cimv2' `
                 -Arguments @{hDefKey=[uint32]2147483650; 
                              sSubKeyName="SYSTEM\CurrentControlSet\Control\Session Manager"; 
                              sValueName="PendingFileRenameOperations"}

The output will be an array of strings like this:

sValue
-----
{\??\C:\ProgramData\Microsoft\...
boxdog
  • 7,894
  • 2
  • 18
  • 27
  • 1
    [Please see my comments on this answer](https://stackoverflow.com/questions/51157841/send-gmail-using-powershell-current-suggestions-on-s-o-are-not-working/51158377#comment89300437_51158377) about escaping newlines in powershell. – Maximilian Burszley Jul 06 '18 at 15:10
  • 1
    @TheIncorrigible1. Saw it. I use this layout a lot on SO as it formats nicely and tends to be more readable and understandable for those who don't know about splatting. It's not meant to be production code, only an example - readers are free to re-work it to suit themselves. – boxdog Jul 06 '18 at 15:18
  • Splatting should be fundamental knowledge to write maintainable code and is encouraged by the community standards on formatting. If you persist in bad practices, people who don't know about features will emulate those bad practices until it bites them (and maybe even after that). – Maximilian Burszley Jul 06 '18 at 15:30
  • @boxdog That works great for the first two queries. Do you have any recommendations for the third query? `(@(((Get-ItemProperty("HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager")).$("PendingFileRenameOperations")) | Where-Object { $_ } ).length -ne 0)` – Keith Jul 06 '18 at 15:39
  • @Keith, I edited my answer to show how to use CIM sessions with that example [more backticks, I'm afraid :-( ] – boxdog Jul 08 '18 at 21:07
  • @boxdog The backticks are great for learning. Thank you very much. It works great!!! – Keith Jul 09 '18 at 14:12
  • @boxdog, I have another CIM issue. Mind taking a look at it? https://stackoverflow.com/questions/51388966/convert-wmi-call-to-cim-call/51389862#51389862 – Keith Jul 18 '18 at 14:11