- SourceRoot (old share)
- DestinationRoot (new share)
- 3 switch parameters (OS, DRV and SW)
With these switch parameters ist controled for which object type is the source path customised.
- 1 switch parameter ListOnly
With used the objects will be only listed. No changes of the source path will be done.
NOTE: I assume that the folder structure has not be changed. That means that in the source path of the migrated object only its prefix has to be changed (e.g. \\ServerOld\ShareOld\... to \\ServerNew\ShareNew\...).
# Version 1.0 - 2016/12/12
###########################################
# Disclaimer
#
# The sample script is not supported under any standard support program or service. The sample script is provided
# AS IS without warranty of any kind.
#
# Examples:
# CMDL Args:
# .\SetObjectPatch.ps1 -SourceRoot "\\OldServer\OldSource" -DestinationRoot "\\NewServer\NewSource" -ComputerName "Computer01" -SW
# .\SetObjectPatch.ps1 -SourceRoot "\\OldServer\OldSource" -DestinationRoot "\\NewServer\NewSource" -ComputerName "Computer01" -SW -OS
# .\SetObjectPatch.ps1 -SourceRoot "\\OldServer\OldSource" -DestinationRoot "\\NewServer\NewSource"
# .\SetObjectPatch.ps1 -SW -ListOnly
Param (
[string] $SourceRoot = $(throw "Please provide the Name of the source root folder"),
[string]$DestinationRoot = $("Please provide the Name of the destination root folder"),
[string] $SiteServer = ".",
[switch] $OS,
[switch] $DRV,
[switch] $SW,
[switch] $ListOnly
)
#=================================================
# Declaring Functions
#=================================================
function GetSiteCode($server)
{
$wqlQuery = "SELECT * FROM SMS_ProviderLocation"
Get-WmiObject -Query $wqlQuery -Namespace "root\sms" -ComputerName $server | ForEach-Object {
If ($_.ProviderForLocalSite)
{$siteCode = $_.SiteCode}
}
return $siteCode.ToLower()
}
#=================================================
# Validating Parameters and Values
#=================================================
#Check SourceRoot
if (!($SourceRoot.StartsWith("\\")) -and !($ListOnly))
{
Write-Host "@ERROR: 1" -ForegroundColor Red
Write-Host "The Source : $SourceRoot does not start with \\ and ListOnly switch not specified" -ForegroundColor Red
Exit 1
}
else
{
#Initialize Parameters
$SourceRoot = $SourceRoot.ToLower()
}
#Check DestinationRoot
if (!($DestinationRoot.StartsWith("\\"))-and !($ListOnly))
{
Write-Host "@ERROR: 2" -ForegroundColor Red
Write-Host "The Destination : $DestinationRoot does not start with \\ and ListOnly switch not specified" -ForegroundColor Red
Exit 2
}
else
{
#Initialize Parameters
$DestinationRoot =$DestinationRoot.ToLower()
}
#=================================================
# Main Script
#=================================================
#Get Site Code
$SiteCode = GetSiteCode($SiteServer)
# All switch parameters selected $All = $True
$All = !($OS -or $DRV -or $SW)
# Change the Driver and Driver Package SourcePath
Write-Host "--- Driver and Driver Package Source Path Change ---" -ForegroundColor Green
If ($All -or $DRV)
{
# Drivers
$CMObjects=Get-WmiObject -Namespace "root\sms\site_$sitecode" -Class SMS_Driver
foreach ($CMObject in $CMObjects)
{
if (($CMObject.ContentSourcePath.ToLower().StartsWith($SourceRoot)) -and !($ListOnly))
{
Write-Host "# Changing Driver: " $CMObject.LocalizedDisplayName -ForegroundColor Green
Write-Host " Old Path: " $CMObject.ContentSourcePath.ToLower() -ForegroundColor White
$CMObjectPath = $CMObject.ContentSourcePath.ToLower().Replace($SourceRoot, $DestinationRoot)
Write-Host " New Path: " $CMObjectPath -ForegroundColor White
$CMObject.ContentSourcePath = $CMObjectPath
$CMObject.Put() > $null
}
else
{
Write-Host "INFO Driver: " $CMObject.LocalizedDisplayName " Path: " $CMObject.ContentSourcePath.ToLower() -ForegroundColor White
}
}
#DriverPackages
Write-Host "--- Driver Package Source Path Change ---" -ForegroundColor Green
$CMObjects=Get-WmiObject -Namespace "root\sms\site_$sitecode" -Class SMS_DriverPackage
foreach ($CMObject in $CMObjects)
{
if (($CMObject.PkgSourcePath.ToLower().StartsWith($SourceRoot)) -and !($ListOnly))
{
Write-Host "# Changing Driver Package: " $CMObject.Name -ForegroundColor Green
Write-Host " Old Path: " $CMObject.PkgSourcePath.ToLower() -ForegroundColor White
$CMObjectPath = $CMObject.PkgSourcePath.ToLower().Replace($SourceRoot, $DestinationRoot)
Write-Host " New Path: " $CMObjectPath -ForegroundColor White
$CMObject.PkgSourcePath = $CMObjectPath
$CMObject.Put() > $null
}
else
{
Write-Host "INFO Driver Package: " $CMObject.Name " Path: " $CMObject.PkgSourcePath.ToLower() -ForegroundColor White
}
}
Write-Host "!!! Driver Package Source Path Change DONE !!!" -ForegroundColor Green
}
# Change the OS Image SourcePath
Write-Host "--- OS Image Source Path Change ---" -ForegroundColor Green
If ($All -or $OS)
{
#WIM Images
$CMObjects=Get-WmiObject -Namespace "root\sms\site_$sitecode" -Class SMS_ImagePackage
foreach ($CMObject in $CMObjects)
{
if (($CMObject.PkgSourcePath.ToLower().StartsWith($SourceRoot)) -and !($ListOnly))
{
Write-Host "# Changing WIM: " $CMObject.Name -ForegroundColor Green
Write-Host " Old Path: " $CMObject.PkgSourcePath.ToLower() -ForegroundColor White
$CMObjectPath = $CMObject.PkgSourcePath.ToLower().Replace($SourceRoot, $DestinationRoot)
Write-Host " New Path: " $CMObjectPath -ForegroundColor White
$CMObject.PkgSourcePath = $CMObjectPath
$CMObject.Put() > $null
}
else
{
Write-Host "INFO WIM Image: " $CMObject.Name " Path: " $CMObject.PkgSourcePath.ToLower() -ForegroundColor White
}
}
}
# Change the Software Package SourcePath
Write-Host "--- Software Package Source Path Change ---" -ForegroundColor Green
If ($All -or $SW)
{
#Software Packages
$CMObjects=Get-WmiObject -Namespace "root\sms\site_$sitecode" -Class SMS_Package
foreach ($CMObject in $CMObjects)
{
if (!($CMObject.PkgSourcePath.ToLower().Contains("\sms_$siteCode\")))
{
if (($CMObject.PkgSourcePath.ToLower().StartsWith($SourceRoot)) -and !($ListOnly))
{
Write-Host "# Changing Package: " $CMObject.Name -ForegroundColor Green
Write-Host " Old Path: " $CMObject.PkgSourcePath.ToLower() -ForegroundColor White
$CMObjectPath = $CMObject.PkgSourcePath.ToLower().Replace($SourceRoot, $DestinationRoot)
Write-Host " New Path: " $CMObjectPath -ForegroundColor White
$CMObject.PkgSourcePath = $CMObjectPath
$CMObject.Put() > $null
}
else
{
Write-Host "INFO Software Package: " $CMObject.Name " Path: " $CMObject.PkgSourcePath.ToLower() -ForegroundColor White
}
}
}
}