Enterprise Mobility and Security Blog

RSS

Test script

 

$ErrorActionPreference = "Stop"

function RunCmd($cmd)
{
& $cmd
if ($LASTEXITCODE -gt 0) {
echo "Fail to run command with error $LASTEXITCODE : $cmd"
exit;
}
}

function fRoboCopy($cmd)
{
& $cmd
if ($LASTEXITCODE -eq 0  -or $LASTEXITCODE > 3) {
echo "Fail to run command with error $LASTEXITCODE : $cmd"
exit;
}
}
function RecoverTopMostSite($path)
{
echo "Recover top-most site server $path"
fRoboCopy {robocopy "$path\EasySetupPayload\609f1263-04e0-49a8-940b-09e0e34de2d2\SMSSETUP\CLIENT" "$path\CMUClient" client.msi ccmsetup.exe ccmsetup.cab /S}
RunCmd {xcopy /y "$path\EasySetupPayload\609f1263-04e0-49a8-940b-09e0e34de2d2\redist\Silverlight.exe" "$path\CMUClient\i386\" }

# Recover 1602 rollup client patch msp file
fRoboCopy {robocopy "$path\EasySetupPayload\59BCA34E-DF87-4041-B9B7-F53395849E81\SMSSETUP\CLIENT" "$path\CMUClient" *.msp /S}

# recover for all the client package folders
RunCmd {xcopy /s /y "$path\CMUClient" "$path\StagingClient\"}
RunCmd {xcopy /s /y "$path\CMUClient" "$path\Client\"}

# trigger the update of the client package
RunCmd {New-Item "$path\inboxes\hman.box\client.acu" -type file}
}

function RecoverChildSites($caspath, $pspath)
{
echo "Recover child primary site $pspath from $caspath"
RunCmd {xcopy /s /y "$caspath\CMUClient" "$pspath\CMUClient\"}
RunCmd {xcopy /s /y "$caspath\CMUClient" "$pspath\Client\"}

# trigger the update of the client package for remote MPs
RunCmd {New-Item "$pspath\inboxes\sitecomp.box\resetmpbin.trn" -type file}
}
if ($args.Count -eq 2) {
# Recover the child primary site using the CAS as a source
$CASShareFolder = $args[0]
$PSShareFolder = $args[1]

echo "Recover $PSShareFolder from $CASShareFolder"
if (-Not (Test-Path $CASShareFolder)) {
echo "Error to access $CASShareFolder"
exit
}
RecoverChildSites $CASShareFolder $PSShareFolder

}
elseif ($args.Count -eq 1) {
# Reover the Top-Most site
$CASShareFolder = $args[0]
echo "Recover top-most site $CASShareFolder"
RecoverTopMostSite($CASShareFolder)
}
else {
echo "Recover all the sites"
# Fetch the SQL Server and Database name from registry key

$sitecode = (Get-ItemProperty -Path "Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\SMS\Identification").'Site Code'
if (-Not $sitecode) {
echo "ERROR: Faile to get the SiteCode from registry"
exit
}

echo "Site Code: $sitecode"

# Top Site Server
$foundCAS = $FALSE
Get-WmiObject -query 'select * from sms_site where ReportingSiteCode = ""' -Namespace root\sms\site_$sitecode |  ForEach-Object {
$path = '\\' + $_.ServerName + '\SMS_' + $_.SiteCode
RecoverTopMostSite($path)
$foundCAS = $TRUE
# Child Primary Site
Get-WmiObject -query 'select * from sms_site where ReportingSiteCode != "" and Type = 2' -Namespace root\sms\site_$sitecode |  ForEach-Object {
$pspath = '\\' + $_.ServerName + '\SMS_' + $_.SiteCode
RecoverChildSites $path $pspath
}
break
}

if(-Not $foundCAS) {
echo "Error: cannot find the CAS from WMI, you may have no permission to access ConfigMgr"
exit
}
}