I had a scenario where some of our SCCM clients would fail to communicate to our Primary Servers. Upon investigation it was found that changing the sitecode of the client would resolve the issue at hand, however doing this on a couple of 100 clients would be time consuming.
Instead of logging into each client and clicking on the “Find Site” button, I decided to put Powershell to work and do the work for me. So basically the Powershell script reads a list of computers from a text file and performs the following tasks against each active client:
Powershell Code:
################################
#Script: Auto find the SCCM client SiteCode #
#Author: Anton Potgieter #
################################
$Computers = get-content -path C:\Temp\SCCM_Sitecode\SiteCodeComputers.txt
$Script=’C:\Windows\ccmsetup\sitecode.vbs’
$Source= “C:\Temp\SCCM_Sitecode\sitecode.vbs”
$FileExists = Test-Path $WantFile
$WantFile = “\\” + $Computer + “\Admin$\ccmsetup\sitecode.vbs”
$LogTime = Get-Date -Format “MM-dd-yyyy_hh-mm-ss”
ForEach ($Computer in $Computers){
write-host `n
write-host Processing computer $computer -ForegroundColor yellow
$column = 1
if ((Test-Connection -computername $Computer -count 1 -Quiet) -eq $true) {
($FileExists -eq $False)
Copy-Item $Source (“\\” + $Computer + “\Admin$\ccmsetup”)
add-content “C:\Temp\SCCM_Sitecode\sitecode.log” “$LogTime ,$Computer, script copy completed.”
E:\SMSSourceE\Applications\Packages_files\SCCM_Sitecode\PsExec.exe -s \\$Computer C:\Windows\System32\cscript.exe \\$Computer\$Script
add-content “C:\Temp\SCCM_Sitecode\sitecode.log” “$LogTime ,$Computer, finding site completed.”
}
else {
add-content “C:\Temp\SCCM_Sitecode\sitecode.log” “$LogTime ,$Computer, is offline.”
}
}
VBScript:
—Start of Script—
‘ Created by S. Thompson
‘ Version 1.00
‘ Function: Obtain current SMS site assignment and compare to “auto” discovered
‘ site. If these values are different, set client SMS site assignment
‘ to discovered site.
Option Explicit
dim oSMSClient
dim strDiscoverSite
dim strAssignedSite
On Error Resume Next
Set oSMSClient = CreateObject (“Microsoft.SMS.Client”)
If err.Number <> 0 then
‘ wscript.echo “Could not create SMS Client Object – quitting”
Else
strDiscoverSite = CallAutoDiscoverSite ()
strAssignedSite = CallGetAssignedSite ()
If strAssignedSite <> strDiscoverSite Then
Call SetAssignedSite (strDiscoverSite)
End If
End If
Set oSMSClient=nothing
Function CallAutoDiscoverSite ()
dim strDiscoveredSite
strDiscoveredSite = oSMSClient.AutoDiscoverSite
If Len(strDiscoveredSite & “”)>0 Then
‘ we have something
Else
strDiscoveredSite = “DiscoveredSiteNotFound”
End If
‘ wscript.echo “Discovered Site is : ‘” & strDiscoveredSite & “‘”
CallAutoDiscoverSite = strDiscoveredSite
End Function
Function CallGetAssignedSite ()
dim strAssignedSite
strAssignedSite = oSMSClient.GetAssignedSite
If Len(strAssignedSite & “”)>0 Then
‘ we have something
Else
strAssignedSite = “AssignedSiteNotFound”
End If
‘ wscript.echo “Assigned Site is : ‘” & strAssignedSite & “‘”
CallGetAssignedSite = strAssignedSite
End Function
Sub SetAssignedSite (SiteCode)
oSMSClient.SetAssignedSite SiteCode,0
End Sub
—End of Script—
Credit for the vbscript goes to S.Thompson
3,550 total views, 1 views today
Posted in: Configuration Manger, Powershell, Scripts | Tags: Change site code, Find SiteCode, SCCM SiteCode
One Response