Showing posts with label ps. Show all posts
Showing posts with label ps. Show all posts

07 December, 2012

Machine Policy Retrieval - SCCM

A very efficient way to break tens of thousands of machines is to install something on them using a deployment / configuration management software, such as SCCM.

It's always a bad idea to send out a package on a Friday afternoon - and hope for the best - because the longest afternoons are usually the Friday ones, they can last several days when you need to remediate a couple of 1000 hosts where application installations got corrupted because of your Friday SCCM package. What you want, before hitting the full list of 1000s of servers with a package is to send it out to a couple and quickly see the result. However, if you send out a package it can take up to an hour to see the results in SCCM - depending on your SCCM site settings... How can you make it quicker? Invoke a client action on each host called "Machine Policy Retrieval & Evaluation Cycle".

SCCM Client Actions
I know there are many very good tools out there for SCCM with UI and multiple functions and fancy remote host handling capabilities, however, a couple of years ago - because I like the command line way - I thought I'd write a quick script which is able to invoke SCCM Client side actions on multiple hosts remotely At the same time it would have the capability of pipe'ing the output to another PowerShell script or some file or export it to csv...whatever.

Background:
SCCM client has several agents and those can perform actions, such as downloading policies and packages from the server, perform software or hardware inventories...etc. Because all these agents are built on WMI, you can invoke actions by creating WMI instances of specific WMI classes on the remote machine.


OK, so the idea is there. When you want to break.. khm deploy something on many, many servers, you just pick about 50 or maybe 100 firts, send out the package and then... and this is where powershell comes in: run the following script to invoke the MAchine Policy Retrieval & Evaluation Cycle which will make the client download the package and run it + report back to the SCCM server.

$hostlist = @($Input)

if($($hostlist.length) -gt 0){
    foreach ($srv in $hostlist) {

        # Binding \\$srv\root\ccm:SMS_Client
        $SMSCli = [wmiclass] "\\$srv\root\ccm:SMS_Client"

        if($SMSCli){

            #Invoking $actionName
            $check = $SMSCli.RequestMachinePolicy()
            $check = $SMSCli.EvaluateMachinePolicy()
        }
        else{
            write-host "$srv, Could not bind WMI class SMS_Client"
        }
    }
}


Save it as a something.ps1 file and use it like this:
PS C:\> gc hostlist.txt | something.ps1

Clipboard friendly code:
 $hostlist = @($Input)  
   
 if($($hostlist.length) -gt 0){  
      foreach ($srv in $hostlist) {  
           # Binding \\$srv\root\ccm:SMS_Client  
           $SMSCli = [wmiclass] "\\$srv\root\ccm:SMS_Client"  
           if($SMSCli){  
                #Invoking $actionName   
                $check = $SMSCli.RequestMachinePolicy()  
                $check = $SMSCli.EvaluateMachinePolicy()  
                  
           }  
           else{  
                write-host "$srv, Could not bind WMI class SMS_Client"  
           }  
      }  
 }