17 February, 2013

Start Resource Explorer from PowerShell - SCCM

Here is another SCCM snippet. The other day, I put in some custom entries into the SCCM hardware inventory config to be able to report on firmware version of RAID controllers of IBM and HP servers. When you do that, you are either lucky to get everything right first or you need to go down the harder route to troubleshoot why the data is not flowing into the SCCM database.

A good place to look at whether you have the data in SCCM already is SCCM Resource Explorer, where you can see the software and hardware inventory data of one machine:


Resource Explorer - SCCM 2007


















I'm an server ops guy, I've been in operations for too long, so I'm not very patient or I should say I like to be efficient. Opening Resource Explorer for 3-5 hosts is a pain in my view. You need to open the SCCM console, go to Collections, find a collection where your machine is probably included, open the collection (I usually have 10000+ machines in those collections), filter for your machine, wait a bit until Mr. MMC thinks about life and Einstein's relativity theory and then it shows your the machine. Then right click, Start, Resource Explorer. So this is about 90 seconds and 6-8 click (depending on how much you like to use mouse instead of keyboard).

Because this blog is mainly about Powershell, you could figure it by now, that the solution will be a PS script. Let's take it step by step:
First we need to find the SCCM installation bin folder where the resourceexplorer.msc file is located:
$adminui = Get-Itemproperty -path hklm:\SOFTWARE\Microsoft\ConfigMgr\Setup -name "UI Installation Directory" | %{$_."UI Installation Directory"}
$bin = $adminui + "\bin"
$command = "$bin\resourceexplorer.msc"

Obviously a fully fledged strip will contain a test-path check against the file, error handling if we couldn't find the msc file...etc. but I don't want to bore you with this.
The next step is to go through each host in the list and open Resource Explorer for them.
foreach($srv in $hostlist){
   $inputhost = "`'" + $srv + "`'"
   $cmd = $command + " -s -sms:ResExplrQuery=`"SELECT ResourceID FROM SMS_R_SYSTEM WHERE NAME = $inputhost`" -sms:connection=\\$sccmsrv\root\sms\site_cen"
   $tmpVal = [diagnostics.process]::start("mmc", $cmd)
}

There are two tricks in this code, the first was to find out how to specify the necessary parameters for the msc file, it's basically a WQL query:
SELECT ResourceID FROM SMS_R_SYSTEM WHERE NAME = $inputhost

You also need to escape the quotes for the WQL query string, that's why the `" characters are there.

The second trick is to put the server name into single quotes:
$inputhost = "`'" + $srv + "`'"

The full script:
 param ([string] $hosts = "")
  
 $adminui = Get-Itemproperty -path hklm:\SOFTWARE\Microsoft\ConfigMgr\Setup -name "UI Installation Directory" | %{$_."UI Installation Directory"}
 $bin = $adminui + "\bin"
 $command = "$bin\resourceexplorer.msc"
 $sccmsrv = $env:COMPUTERNAME
  
 #### Collate the host list.
 $hostlist = @($Input)
 if ($hosts) {
      if($hosts -imatch " "){
           $hostsArr = @($hosts.split(" "))
           $hostlist += $hostsArr
      }
      else{
           $hostlist += $hosts
      }
 }
  
 foreach($srv in $hostlist){
      $inputhost = "`'" + $srv + "`'"
      $cmd = $command + " -s -sms:ResExplrQuery=`"SELECT ResourceID FROM SMS_R_SYSTEM WHERE NAME = $inputhost`" -sms:connection=\\$sccmsrv\root\sms\site_cen"
      $tmpval = [diagnostics.process]::start("mmc", $cmd)
 }  


Let me know if there's any suggestion, comment...etc. May The Force be with you all.
t


No comments:

Post a Comment