Updated: SCCM Pacakge AdverFlags, RemoteClientFlags, PkgFlags

Device management

As I said in my last post “SCCM Content Settings Tool”, I would like to add packages and OSD in the tool, but didn’t know how to do it.

This is second time I write this pos, I was wrong in my last post, which I have deleted it.

Here is a updated post, this time should be correct.

Here are some links that what you could read:
http://2pintsoftware.com/branchcache-enable-task-sequences-sccm

http://powerschill.com/uncategorized/bitwise-operators

http://www.moyerteam.com/2013/11/determine-bits-set-configmgr-properties

Here are the examples:

#Get SiteSystem Informations
$CMModulePath = Join-Path -Path (Split-Path -Path "${Env:SMS_ADMIN_UI_PATH}" -ErrorAction Stop) -ChildPath "ConfigurationManager.psd1"
Import-Module $CMModulePath -ErrorAction Stop
$SiteCode = (get-psdrive -PSProvider CMSite).Name
$SiteServer = (get-psdrive -PSProvider CMSite).root
#Set-Location ($sitecode + ":")

Function Get-BitFlagsSet($FlagsProp, $BitFlagHashTable)
{
	#Original from http://www.moyerteam.com/2013/11/determine-bits-set-configmgr-properties/
	
	$ReturnHashTable = @{
	}
	$BitFlagHashTable.Keys | ForEach-Object {
		if (($FlagsProp -band $BitFlagHashTable.Item($_)) -ne 0)
		{
			$ReturnHashTable.Add($($_), $true)
		}
		else
		{
			$ReturnHashTable.Add($($_), $false)
		}
	}
	$ReturnHashTable
}


function Get-AdvInformation
{
	#Original from http://www.moyerteam.com/2013/11/determine-bits-set-configmgr-properties/
	#Modified by Sandy, 22.10.2016
	
	Param (
		[parameter(Mandatory = $true)]
		[ValidateNotNullOrEmpty()]
		[string]$SiteServer,
		[parameter(Mandatory = $true)]
		[ValidateNotNullOrEmpty()]
		[string]$SiteCode,
		[Parameter(Mandatory = $true)]
		[string]$AdvertisementID
	)
	
	
	try
	{
		$Error.Clear()
		
		#get an instance of the advertisement
		$PkgAdvs = Get-WmiObject -computername $siteserver -ns "root\SMS\Site_$sitecode" -class SMS_Advertisement | WHERE {
			$_.ProgramName -ne '*' -and $_.AdvertisementID -like $AdvertisementID
		}
		
		Write-host "Advertisement Name:" $PkgAdvs.AdvertisementName
		Write-host "Advertisement Id:" $PkgAdvs.AdvertisementId
		Write-host "AdvertFlags:" $PkgAdvs.AdvertFlags
		Write-host "RemoteClientFlags:" $PkgAdvs.RemoteClientFlags
		
		
		#hash table with bit flag definitions
		$AdvertFlagsHex = @{
			DONOT_FALLBACK = "0x00020000"
			BranchCache    = "0x00010000"
		}
		
		$RemoteClientFlagsHex = @{
			DOWNLOAD_FROM_REMOTE_DISPPOINT = "0x00000040"
			DONT_RUN_NO_LOCAL_DISPPOINT    = "0x00000020"
		}
		
		#Get the flags set
		Get-BitFlagsSet -FlagsProp $PkgAdvs.AdvertFlags -BitFlagHashTable $AdvertFlagsHex
		Get-BitFlagsSet -FlagsProp $PkgAdvs.RemoteClientFlags -BitFlagHashTable $RemoteClientFlagsHex
		
	}
	catch
	{
		Write-Host $error[0].Exception.tostring()
	}
	
}

function Set-AdvInformation
{
	
	#More information check https://msdn.microsoft.com/en-gb/library/cc146108.aspx
	#More information check http://2pintsoftware.com/branchcache-enable-task-sequences-sccm/
	#More information check http://powerschill.com/uncategorized/bitwise-operators/
	
	Param (
		[CmdletBinding(SupportsShouldProcess = $true)]
		[parameter(Mandatory = $true)]
		[ValidateNotNullOrEmpty()]
		[string]$SiteServer,
		[parameter(Mandatory = $true)]
		[ValidateNotNullOrEmpty()]
		[string]$SiteCode,
		[Parameter(Mandatory = $false)]
		[string]$AdvertisementID,
		[Parameter(Mandatory = $false)]
		[bool]$DONOTFALLBACK,
		[Parameter(Mandatory = $false)]
		[bool]$BranchCache,
		[Parameter(Mandatory = $false)]
		[bool]$DOWNLOADONSLOW
	)
	
	$DONOTFALLBACKHEX = "0x00020000"
	$BranchCacheHEX = "0x00010000"
	$DOWNLOADFROMREMOTEDISPPOINTHEX = "0x00000040"
	$DONTRUNNOLOCALDISPPOINTHEX = "0x00000020"
	
	try
	{
		$Error.Clear()
		
		#Set Download On Slow Network, base on DOWNLOAD_FROM_REMOTE_DISPPOINT and DONT_RUN_NO_LOCAL_DISPPOINT
		$PkgAdvs = Get-WmiObject -computername $siteserver -ns "root\SMS\Site_$sitecode" -class SMS_Advertisement | Where-Object {
			$_.ProgramName -ne '*' -and $_.AdvertisementID -like $AdvertisementID
		}
		$NewAdv = [wmi]$PkgAdvs.__PATH
		if ($DOWNLOADONSLOW -eq $true)
		{
			#Set DOWNLOADFROMREMOTEDISPPOINTHEX
			$NewAdv.RemoteClientFlags = $PkgAdvs.RemoteClientFlags -bor $DOWNLOADFROMREMOTEDISPPOINTHEX
			$NewAdv.Put()
			#
			#Set DONTRUNNOLOCALDISPPOINT
			#
			$PkgAdvs = Get-WmiObject -computername $siteserver -ns "root\SMS\Site_$sitecode" -class SMS_Advertisement | Where-Object {
				$_.ProgramName -ne '*' -and $_.AdvertisementID -like $AdvertisementID
			}
			$NewAdv = [wmi]$PkgAdvs.__PATH
			if ($PkgAdvs.RemoteClientFlags -band $DONTRUNNOLOCALDISPPOINTHEX)
			{
				$NewAdv.RemoteClientFlags = $PkgAdvs.RemoteClientFlags -bxor $DONTRUNNOLOCALDISPPOINTHEX
				$NewAdv.Put()
			}
		}
		if ($DOWNLOADONSLOW -eq $false)
		{
			#Set DOWNLOADFROMREMOTEDISPPOINTHEX
			if ($PkgAdvs.RemoteClientFlags -band $DOWNLOADFROMREMOTEDISPPOINTHEX)
			{
				$NewAdv.RemoteClientFlags = $PkgAdvs.RemoteClientFlags -bxor $DOWNLOADFROMREMOTEDISPPOINTHEX
				$NewAdv.Put()
			}
			#
			#Set DONTRUNNOLOCALDISPPOINT
			#
			$PkgAdvs = Get-WmiObject -computername $siteserver -ns "root\SMS\Site_$sitecode" -class SMS_Advertisement | Where-Object {
				$_.ProgramName -ne '*' -and $_.AdvertisementID -like $AdvertisementID
			}
			$NewAdv = [wmi]$PkgAdvs.__PATH
			$NewAdv.RemoteClientFlags = $PkgAdvs.RemoteClientFlags -bor $DONTRUNNOLOCALDISPPOINTHEX
			$NewAdv.Put()
		}
		#
		#
		#Set BranchCache
		#
		$PkgAdvs = Get-WmiObject -computername $siteserver -ns "root\SMS\Site_$sitecode" -class SMS_Advertisement | Where-Object {
			$_.ProgramName -ne '*' -and $_.AdvertisementID -like $AdvertisementID
		}
		$NewAdv = [wmi]$PkgAdvs.__PATH
		if ($BranchCache -eq $true)
		{
			$NewAdv.AdvertFlags = $PkgAdvs.AdvertFlags -bor $BranchCacheHEX
			$NewAdv.Put()
		}
		if ($BranchCache -eq $false)
		{
			if ($PkgAdvs.AdvertFlags -band $BranchCacheHEX)
			{
				$NewAdv.AdvertFlags = $PkgAdvs.AdvertFlags -bxor $BranchCacheHEX
				$NewAdv.Put()
			}
		}
		#
		#
		#Set Do Not Fall Back
		#
		$PkgAdvs = Get-WmiObject -computername $siteserver -ns "root\SMS\Site_$sitecode" -class SMS_Advertisement | Where-Object {
			$_.ProgramName -ne '*' -and $_.AdvertisementID -like $AdvertisementID
		}
		$NewAdv = [wmi]$PkgAdvs.__PATH
		if ($DONOTFALLBACK -eq $true)
		{
			$NewAdv.AdvertFlags = $PkgAdvs.AdvertFlags -bor $DONOTFALLBACKHEX
			$NewAdv.Put()
		}
		if ($DONOTFALLBACK -eq $false)
		{
			if ($PkgAdvs.AdvertFlags -band $DONOTFALLBACKHEX)
			{
				$NewAdv.AdvertFlags = $PkgAdvs.AdvertFlags -bxor $DONOTFALLBACKHEX
				$NewAdv.Put()
			}
		}
	}
	catch
	{
		Write-Host $error[0].Exception.tostring()
	}
	
}

$AdvertisementID = "ZIT20009"
Get-AdvInformation -SiteServer $SiteServer -SiteCode $SiteCode -AdvertisementID $AdvertisementID
Set-AdvInformation -SiteServer $SiteServer -SiteCode $SiteCode -AdvertisementID $AdvertisementID -DOWNLOADONSLOW $false -BranchCache $false -DONOTFALLBACK $false

#Get SiteSystem Informations
$CMModulePath = Join-Path -Path (Split-Path -Path "${Env:SMS_ADMIN_UI_PATH}" -ErrorAction Stop) -ChildPath "ConfigurationManager.psd1"
Import-Module $CMModulePath -ErrorAction Stop
$SiteCode = (get-psdrive -PSProvider CMSite).Name
$SiteServer = (get-psdrive -PSProvider CMSite).root
#Set-Location ($sitecode + ":")

Function Get-BitFlagsSet($FlagsProp, $BitFlagHashTable)
{
	#Original from http://www.moyerteam.com/2013/11/determine-bits-set-configmgr-properties/
	
	$ReturnHashTable = @{
	}
	$BitFlagHashTable.Keys | ForEach-Object {
		if (($FlagsProp -band $BitFlagHashTable.Item($_)) -ne 0)
		{
			$ReturnHashTable.Add($($_), $true)
		}
		else
		{
			$ReturnHashTable.Add($($_), $false)
		}
	}
	$ReturnHashTable
}


function Get-PackageInformation
{
	#Original from http://www.moyerteam.com/2013/11/determine-bits-set-configmgr-properties/
	#Modified by Sandy, 22.10.2016
	
	Param (
		[parameter(Mandatory = $true)]
		[ValidateNotNullOrEmpty()]
		[string]$SiteServer,
		[parameter(Mandatory = $true)]
		[ValidateNotNullOrEmpty()]
		[string]$SiteCode,
		[Parameter(Mandatory = $true)]
		[string]$PackageID
	)
	
	
	try
	{
		$Error.Clear()
		
		#get an instance of the advertisement
		$PkgInfo = Get-WmiObject -computername $siteserver -ns "root\SMS\Site_$sitecode" -class SMS_package | Where-Object {
			$_.PackageID -like $PackageID
		}
		
		Write-host "Package Name:" $PkgInfo.Name
		Write-host "Package Id:" $PkgInfo.PackageID
		Write-host "Package Flags:" $PkgInfo.PkgFlags
		
		
		#hash table with bit flag definitions
		$PERSISTCONNECTIONHex = @{
			PERSISTCONNECTION = "0x02000000"
		}
		
		#Get the flags set
		Get-BitFlagsSet -FlagsProp $PkgInfo.PkgFlags -BitFlagHashTable $PERSISTCONNECTIONHex
		
	}
	catch
	{
		Write-Host $error[0].Exception.tostring()
	}
	
}

function Set-PackagePersistContent
{
	
	#More information check https://msdn.microsoft.com/en-gb/library/cc146108.aspx
	#More information check http://2pintsoftware.com/branchcache-enable-task-sequences-sccm/
	#More information check http://powerschill.com/uncategorized/bitwise-operators/
	
	Param (
		[CmdletBinding(SupportsShouldProcess = $true)]
		[parameter(Mandatory = $true)]
		[ValidateNotNullOrEmpty()]
		[string]$SiteServer,
		[parameter(Mandatory = $true)]
		[ValidateNotNullOrEmpty()]
		[string]$SiteCode,
		[Parameter(Mandatory = $false)]
		[string]$PackageID,
		[Parameter(Mandatory = $false)]
		[bool]$PERSISTCONNECTION
	)
	
	$PERSISTCONNECTIONHEX = "0x02000000"
	
	
	try
	{
		$Error.Clear()
		
		
		#Set PERSISTCONNECTIONHEX
		#
		$PkgInfo = Get-WmiObject -computername $siteserver -ns "root\SMS\Site_$sitecode" -class SMS_package | Where-Object {
			$_.PackageID -like $PackageID
		}
		$NewPkg = [wmi]$PkgInfo.__PATH
		if ($PERSISTCONNECTION -eq $true)
		{
			$NewPkg.PkgFlags = $NewPkg.PkgFlags -bor $PERSISTCONNECTIONHEX
			$NewPkg.Put()
		}
		if ($PERSISTCONNECTION -eq $false)
		{
			if ($PkgInfo.PkgFlags -band $PERSISTCONNECTIONHEX)
			{
				$NewPkg.PkgFlags = $NewPkg.PkgFlags -bxor $PERSISTCONNECTIONHEX
				$NewPkg.Put()
			}
		}
		
	}
	catch
	{
		Write-Host $error[0].Exception.tostring()
	}
	
}

$PackageID = "ZIT00013"
Get-PackageInformation -SiteServer $SiteServer -SiteCode $SiteCode -PackageID $PackageID
Set-PackagePersistContent -SiteServer $SiteServer -SiteCode $SiteCode -PackageID $PackageID -PERSISTCONNECTION $false

You can download these scripts in here: Set-AdvInformation.ps1  and Set-PackagePersistContent.ps1

PS. Content Setting Tool is updated, added packages, please download again from TechNet. Click Here

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.