Showing posts with label ccmexec. Show all posts
Showing posts with label ccmexec. Show all posts

Monday, 21 May 2012

SCCM - ClientAgent - Check installed correct

This is a quick script I wrote to ensure the agent was installing correctly on secondary servers. However should work anywhere, some slight modifications may be required. Also can be used in conjunction with a number of other scripts and checks.



CheckAgent()


Sub CheckAgent()
On Error Resume Next

strCmd = objShell.ExpandEnvironmentStrings("%comspec%")
objShell = WScript.CreateObject("WScript.Shell")
wscript.echo "Checking the state of the SMSAgent"
Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
Err.Clear
theService = "CcmExec"
bInstallClient = False
bReinstallClient = False
bServiceExists = False
Set colServiceList = objWMIService.ExecQuery("Select * from Win32_Service where Name = '" & theService & "'")
If (Err.Number = 0) And IsObject(colServiceList) Then
If colServiceList.Count > 0 Then
bServiceExists = True
'wscript.echo "SMSAgent Service Exists"
Else
bServiceExists = False
wscript.echo "ERROR service does not exist: " & theService
End If
If bServiceExists = True Then
Set oSMSClient = CreateObject("Microsoft.SMS.Client")
If Err.Number <> 0 Then
'Client is not installed correctly
bReinstallClient = True
Else
'wscript.echo "SMSAgent health seems to be fine"
bReinstallClient = False
End If
Else
bInstallClient = True
End If
If bReinstallClient = False And bServiceExists = True Then
site = UCase(oSMSClient.GetAssignedSite)
If Len(site) < 1 Then
'not assigned to a valid site
wscript.echo "SMSAgent not assigned to a valid site"
wscript.msgbox("Please check the SMSAgent the site is not assigned")
Else
'wscript.echo "SMSAgent seems to be working fine"
End If
End If
End If
If bInstallClient = True Then
wscript.echo "INSTALLING THE SMSAGENT PLEASE WAIT"
cMD = strCmd & " /c C:\windows\system32\ccmsetup\ccmsetup.exe"
Return = objShell.Run(cMD,1,True)
End If
If bReinstallClient = True Then
If FileMinsSinceModified("c:\windows\system32\ccmsetup\ccmsetup.log") < 15 Then
strMsg = "It seems ccmsetup was run in only" & vbCrLf & _
FileMinsSinceModified("c:\windows\system32\ccmsetup\ccmsetup.log") & " minutes ago.." & vbCrLf & _
"are you sure you wish to continue the CCMEXEC reinstall?" & vbCrLf & _
"this install may take some time if you continue"
return=wscript.msgbox(strMsg,4,"CONTINUE REINSTALL?")
If return = 6 Then
wscript.echo "UNINSTALLING THE SMSAGENT PLEASE WAIT"
cMD = strCmd & " /c C:\windows\system32\ccmsetup\ccmsetup.exe /uninstall"
Return = objShell.Run(cMD,1,True)
wscript.echo "INSTALLING THE SMSAGENT. DO NOT CLOSE THE CMD WINDOW!"
cMD = strCmd & " /c C:\windows\system32\ccmsetup\ccmsetup.exe"
Return = objShell.Run(cMD,1,True)
Else
wscript.echo "Reinstallation will not continue at this time.."
End If
Else
wscript.echo "UNINSTALLING THE SMSAGENT PLEASE WAIT"
cMD = strCmd & " /c C:\windows\system32\ccmsetup\ccmsetup.exe /uninstall"
Return = objShell.Run(cMD,1,True)
wscript.echo "INSTALLING THE SMSAGENT. DO NOT CLOSE THE CMD WINDOW!"
cMD = strCmd & " /c C:\windows\system32\ccmsetup\ccmsetup.exe"
Return = objShell.Run(cMD,1,True)
End If
End If
If bServiceExists = True And bReinstallClient = False and bInstallClient = False Then
wscript.echo "SMSAgent seems to be fine"
Else
wscript.echo "The SMSAgent had an issue, Check to see if it was installed or reinstalled"
End If
Set colServiceList = Nothing
Set oSMSClient = Nothing
On Error GoTo 0
End Sub


Function FileMinsSinceModified(filename)
On Error Resume Next
Dim daysold, fso, f, filespec_date
set fso = CreateObject("Scripting.FileSystemObject")
FileMinsSinceModified = 0
If fso.FileExists(UCase(filename)) Then
Set f = fso.GetFile(filename)
filespec_date = f.DateLastModified
Set f = Nothing
If IsDate(filespec_date) Then
minsold = DateDiff("n", filespec_date,Now())
FileMinsSinceModified = minsold 
Else
wscript.echo "WARNING: Not a valid date: " & filespec_date 
End If
Else
wscript.echo "The file does not exist: " & filename 
End If
End Function 

Thursday, 17 May 2012

SCCM - ClientAgent - Check Site Assignment

This script subroutine will quickly check the SMSAgent(ccmexec.exe) site assignment to ensure it is assigned to the correct SiteCode. This version is written for a multi-hierarchy site configuration. Modify/extend as required.

Sub CheckSite()
On Error Resume Next


        wscript.echo "Checking site assignment"
Dim oSMSClient, Site, objSysInfo, domain, strSiteCode
Set objSysInfo = CreateObject("ADSystemInfo")
domain = ucase(objSysInfo.DomainDNSName)
Set objSysInfo = Nothing
If Len(domain) < 0 Then
            domain = GetDomain
End If 
wscript.echo "Computer Domain: " & domain 
Set oSMSClient = CreateObject("Microsoft.SMS.Client")
If Err.Number <> 0 Then
strMsg = "SMSAgent is not installed correctly or not running." & vbCrLf & _
"Object Microsoft.SMS.Client does not exist"
wscript.MsgBox(strMsg)
Err.Clear
Exit Sub
End If
Site = ucase(oSMSClient.GetAssignedSite)
If Len(Site) < 1 Then
wscript.echo "The machine is not assigned to a valid sitecode"
                strSiteCode = FindSiteCode(domain)
wscript.echo "Should be set to " & strSiteCode
If strSiteCode = "999" Then
strMsg = "Site code could not be found correctly for domain"
                        wscript.msgbox(strMsg)
Exit Sub
End If
oSMSClient.SetAssignedSite(strSiteCode)
If Err.Number <> 0 Then
strMsg "Site reassign failed with error: " & Err.Number
wscript.msgbox(strMsg)
Err.Clear
Else
wscript.echo "Site reassignment successful"
End If
Else
  wscript.echo "Site: " & Site
End If
  mgmt =  oSMSClient.GetCurrentManagementPoint
wscript.echo "Mgmt Point: " & mgmt
Set oSMSClient = Nothing
End Sub
Function FindSiteCode(dom)
On Error Resume Next
Dim FQDN
FQDN = ".blah.com"
Select Case dom
Case "0" & FQDN
FindSiteCode="000"
Case "1" & FQDN
FindSiteCode="001"
Case "2" & FQDN
FindSiteCode="002"
Case "3" & FQDN
FindSiteCode="003"
Case "4" & FQDN
FindSiteCode="004"
Case "5" & FQDN
FindSiteCode="005"
Case "6" & FQDN
FindSiteCode="006"
Case "7" & FQDN
FindSiteCode="007"
Case "8" & FQDN
FindSiteCode="008"
Case "9" & FQDN
FindSiteCode="009"
Case "10" & FQDN
FindSiteCode="010"
Case Else
FindSiteCode="999"
' site cannot be found for the domain
  End Select
End Function