Thursday, 20 September 2012

A script to perform a Discover AMT status task remotely for SCCM 2012

Hi Everyone,

Have you every needed to Discover the AMT status of a SCCM client remotely. 

Basically I am provisioning my SCCM 2012 clients with SCS 8.1. When you provision the systems with SCS 8.1 they will show up in SCCM 2012 as externally provisioned

Once the systems are provisioned with SCS you need to manually run a Discover AMT Status on the collection or resource, only then will they show up as externally provisioned. I was looking for a method to automate this process when I came across this article.

Unfortunately this only works with SCCM 2007. Daniel from Intel, who wrote this script pointed out that SCCM 2012 requires two other parameters. It can be found in the following MSDN.

I have modified the script to work with SCCM 2012. It has to be run from the client and requires an extra parameter, the collection ID.

You can call it like this. 

cscript sccmamtmgnt.vbs /s:<SiteServer> /c:<SiteCode> /a:discover /h:%computername% /D:<CollectionID>

I just used the All Systems collection ID

' I have updated the script for SCCM 2012. Please refer to the following Microsoft Website
' SCCM 2012 requires two extra variables. CollectionID and extData. exData can be set to 0
' I am running this from the client. It doesn't pick up the right Resource ID from the SCCM site server

Option Explicit

'Create some variables
'Blair Muller. Updated to include variable CollectionID
dim objItem, objService, objNetwork, userName, userPassword, strHostName, amtAction, amtActionDesc, amtOpCode, server,CollectionID, colNamedArguments, objLocation, WbemServices, sResourceID(2), colMember, colMembers, sitecode

wscript.echo "SCCM AMT Management Script 1.0"

'Bring in the command line arguments
Set colNamedArguments = WScript.Arguments.Named

'Blair Muller. Updated to include CollectionID variable
CollectionID = colNamedArguments.Item("d")

'SCCM site server name
server = colNamedArguments.Item("s")

'SCCM site code
sitecode = colNamedArguments.Item("c")

'AMT Action to perform
amtAction = colNamedArguments.Item("a")
select case amtAction
   case "poweron"
      amtOpCode = "1" 'Power On
      amtActionDesc = "Attempting to power on client"
   case "poweroff"
      amtOpCode = "3" 'Power Off
      amtActionDesc = "Attempting to power off client"
   case "powerrestart"
      amtOpCode = "2" 'Power Restart
      amtActionDesc = "Attempting to restart client"
   case "discover"
      amtOpCode = "16" 'AMT Discovery
      amtActionDesc = "Attempting to perform a Management Controller Discovery..."
   case "updatemc"
      amtOpCode = "256" 'Update Management Controllers
      amtActionDesc = "Attempting to perform a Management Controller Update..."
   case "enableauditlog"
      amtOpCode = "2048" 'Enable Audit Log
      amtActionDesc = "Attempting to enable Audit Log..."
   case "disableauditlog"
      amtOpCode = "4096" 'Disable Audit Log
      amtActionDesc = "Attempting to disable Audit Log..."
   case "clearauditlog"
      amtOpCode = "8192" 'Clear Audit Log
      amtActionDesc = "Attempting to clear Audit Log..."
   case "partialunprovision"
      amtOpCode = "32" 'Partial Unprovision
      amtActionDesc = "Attempting to perform partial unprovision..."
   case "fullunprovision"
      amtOpCode = "64" 'Full Unprovision
      amtActionDesc = "Attempting to perform full unprovision..."
   case "clearprovflag"
      amtOpCode = "1024" 'Clear flag that disable automatic provisioning
      amtActionDesc = "Attempting to clear Auto Provisioning surpression flag..."
   case else
      amtOpCode = ""
      amtActionDesc = ""
end select

'Optional credentials to add the computer to SCCM
userName = colNamedArguments.Item("u")
userPassword = colNamedArguments.Item("p")

'Optional hostname
strHostName = colNamedArguments.Item("h")

'Throw an error if the server, site code, or AMT Action arguments are empty
'Blair Muller. Updated to include CollectionID
if server="" or sitecode="" or amtOpCode="" or CollectionID ="" then
   WScript.Echo "Usage: [CScript | WScript] sccmamtmgnt.vbs [/s:SCCM Server] [/c:SCCM Site Code] [/a:AMT Action] [/h:Host] [/u:username] [/p:password]"
   Wscript.Echo "Required parameters:"
   Wscript.Echo "/s: - Name of your SCCM site collection server."
   wscript.Echo "/c: - Your SCCM three character site code"
   wscript.Echo "/a: - SCCM Site Server AMT action"
   wscript.Echo "/d: - Collection ID where the recourse is located."
   wscript.Echo "  /a:poweron                     Powers on client"
   wscript.Echo "  /a:poweroff                    Powers off client"
   wscript.Echo "  /a:powerrestart                Restarts client"
   wscript.Echo "  /a:discover                    Performs a Management Controller Discovery"
   wscript.Echo "  /a:updatemc                    Performs an Update Management Controller"
   wscript.Echo "  /a:enableauditlog              Enables AMT Audit Log"
   wscript.Echo "  /a:clearauditlog               Clears AMT Audit Log"
   wscript.Echo "  /a:disableauditlog             Disables AMT Audit Log"
   wscript.Echo "  /a:partialunprovision          Performs partial Unprovision"
   wscript.Echo "  /a:fullunprovision             Performs full Unprovision"
   wscript.Echo "  /a:clearprovflag               Clears flag that prevent auto provisioning"
   Wscript.Echo "Optional parameters:"
   Wscript.Echo "/h: - Remote host; if blank it will use host of machine running script"
   Wscript.Echo "/u: - User name to access the SCCM server."
   Wscript.Echo "/p: - Password for the user with access to the SCCM server."
   Wscript.Echo "Example: cscript sccmamtmgnt.vbs / /c:VPD /a:discovery /u:domain\user /p:password"
   WScript.Quit 1
end if

'Create an object to connect to the local WMI provider
Set objService  = GetObject("winmgmts:\root\cimv2")

'Get the hostname from the local WMI agent

if strHostName = "" then
'Update the status on the screen
wscript.echo "Looking up host name..."
Set objNetwork = objService.ExecQuery ("Select * from Win32_NetworkAdapterConfiguration where IPEnabled = True")
for each objItem in objNetwork
    strHostName = ucase(objItem.DNSHostName)
end if

wscript.echo "Hostname: " & strHostName

'Build the WMI connection to the SCCM server
Set objLocation = CreateObject("WbemScripting.SWbemLocator")
Set WbemServices = objLocation.ConnectServer(server, "root\SMS\site_" & sitecode, username, userpassword)

wscript.echo "Looking up host resource ID..."

'Look up the Resouce ID for the computer and place the value in an array (The array is required for the AMTOperateForMachines method)
Set colMembers = wbemServices.ExecQuery("Select * From SMS_FullCollectionMembership WHERE name='" & strHostname & "'")
for each colMember in colMembers
   sResourceID(0) = colMember.ResourceID

wscript.echo "Resource ID: " & sResourceID(0)

'Throw an error if no resource ID is returned
if sResourceID(0) = "" then
  wscript.echo "Error: Unable to locate the computer on the SCCM server."
  WScript.Quit 1
end if

'Tell the SCCM server to perform AMT action.
wscript.echo amtActionDesc
call RunAmtAction(wbemServices, sResourceID, amtOpCode)

Function RunAmtAction(connection, machineResourceIds, actionType)

    On Error Resume Next
    Dim classObj: Set classObj = connection.Get("SMS_Collection")
    Dim inParams: Set inParams = classObj.Methods_("AMTOperateForMachines").InParameters.SpawnInstance_()
    Dim outParams

    inParams.Properties_.Item("Opcode") = actionType
    inParams.Properties_.Item("ResourceIDs") = machineResourceIds
inParams.Properties_.Item("CollectionID") = CollectionID
inParams.Properties_.Item("extData") = 0

    wscript.echo "Sending update management controller request to " & server & "..."
    Set outParams = connection.ExecMethod("SMS_Collection", "AMTOperateForMachines", inParams)
    wscript.echo "Request sent"
End Function

You can see the results in the amtopmgr.log file located in C:\Program Files\Microsoft Configuration Manager\Logs

You can also see the action in the status Message Viewer

Hope this helps.

1 comment:

  1. Usually I do not read post on blogs, however I wish to
    say that this write-up very pressured me to check out and
    do so! Your writing style has been surprised me.
    Thank you, quite nice post.

    my blog post - Mulberry Bags