Add computer variables for Installation of applications according to dynamic variable list in a task sequence. The applications are separated in blocks because a restart is requiered after block of applications has been installed. The list of the applications to be installed is defined by a configuration file. No access to the SCCM Administration console is requiered. Only Access to the configuration file and the script is requiered.
SOLUTION
An XML file where is used as a configuration file. The Task sequence "Install Application" step is added for each block in a Task Sequence.
.\CM_AddComputerVariables.ps1 -ComputerName "Computer01" -xmlFileName "XMLFile.xml"
After the script has been executed on the Computer "Computer01" will be created requiered variables for dynamic application installation. If the Scrips is run again all "SWD_%" variables will be removed and new will be added based on the XML file. The other Computer variables, if any, wont be deleted.
# Version 1.0 - 2016/09/09
###########################################
# 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:
# .\CM_AddComputerVariables.ps1 -ComputerName "Computer01" -xmlFileName "XMLFile.xml"
Param (
[string] $ComputerName = $(throw "Please provide the Name of the computer"),
[string]$xmlFileName = $(throw "XML File is requiered"),
[string] $SiteServer = "."
)
#=================================================
# 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
}
function GetResourceID($client)
{
$ResID = (Get-WmiObject -Namespace root\sms\site_$($SiteCode) -Class sms_r_system -Filter "Name='$client'" | Select-Object -Property ResourceId).ResourceId
return $ResID
}
#=================================================
# Validating Parameters and Values
#=================================================
$ComputerName = $ComputerName.ToUpper()
if ($ComputerName.Length -gt 15)
{
Write-Host "@ERROR: 1" -ForegroundColor Red
Write-Host "The computer name specified was to long. The maximum allowed Windows NetBIOS name length is 15 characters" -ForegroundColor Red
Write-Host "Computer name specified was" $ComputerName "(Character count is" $ComputerName.Length ")." -ForegroundColor Red
Exit 1
}
$File = (Get-Location).Path + "\$xmlFileName"
if (!(Test-Path -Path $File))
{
Write-Host "@ERROR 2" -ForegroundColor Red
Write-Host "The XML File specified does not exist or access is denied:" $File -ForegroundColor Red
Exit 1
}
$SiteCode = GetSiteCode($SiteServer)
$ResourceID = GetResourceID($ComputerName)
if ($ResourceID -eq $null)
{
Write-Host "@ERROR 3" -ForegroundColor Red
Write-Host "The SCCM Client $ComputerName does not exist!" -ForegroundColor Red
Exit 1
}
#=================================================
# Main Script
#=================================================
$ComputerVariableBaseName = "Install_App_"
$ComputerVariablesName = @()
$ComputerVariablesValue = @()
#Import CM12 PS cmdlets
Import-Module ($env:SMS_ADMIN_UI_PATH.Substring(0,$env:SMS_ADMIN_UI_PATH.Length -5) + '\ConfigurationManager.psd1') | Out-Null
$dir = Get-Location
#Read xml file here
[xml]$xmlFile = Get-Content "$File"
ForEach ($ConfigMgrMachineVariable in $xmlFile.ConfigMgrMachineVariableBlocks.ConfigMgrMachineVariableBlock)
{
$blockID = $ConfigMgrMachineVariable.ID
$suffix = switch ($blockID)
{
1 {"A"}
2 {"B"}
3 {"C"}
}
#Add suffix to the base variable
$varBaseName = $ComputerVariableBaseName + $suffix
#Get the number of values in the xml file
$numberOfxmlValues = $ConfigMgrMachineVariable.ChildNodes.Count
#Get the values
$xmlValues = $ConfigMgrMachineVariable.ChildNodes
#Populate the tables with the names and values
for ($i=0; $i -lt $numberOfxmlValues; $i++)
{
$ComputerVariablesValue = $ComputerVariablesValue + $xmlValues[$i].InnerText
$ComputerVariablesName = $ComputerVariablesName + ($varBaseName + $($i+1).ToString("00"))
}
}
#Get SCCM Client Settings
$MachineSettings = [WmiClass]"\\$SiteServer\Root\SMS\Site_$($siteCode):SMS_MachineSettings"
#$ResourceID = (Get-WmiObject -Namespace root\sms\site_p00 -Class sms_r_system -Filter "Name='CL02'" | Select-Object -Property ResourceId).ResourceId
$objPCSet = $MachineSettings.CreateInstance()
$objPCSet.Properties["ResourceID"].Value = $ResourceID
$objPCSet.Properties["SourceSite"].Value = $siteCode
#Read existing Citrix Variables und delete them
$objPCSet.Get()
$count = $objPCSet.MachineVariables.Length
Set-Location "$($siteCode):" | Out-Null
for ($i = 0;$i -lt $count; $i++)
{
If ($objPCSet.MachineVariables[$i].Name -like "$ComputerVariableBaseName*")
{
Write-Host "deleting computer variable: "$objPCSet.MachineVariables[$i].Name -ForegroundColor Green
$varName = $objPCSet.MachineVariables[$i].Name
Remove-CMDeviceVariable -ResourceId $ResourceID -VariableName $varName -Force
}
}
#Add Citrix Computer Variables
$objPCSet.Get()
for ($i = 0;$i -lt $ComputerVariablesName.Count; $i++)
{
$objPCSet.MachineVariables = $objPCSet.MachineVariables + [WmiClass]"\\$SiteServer\Root\SMS\Site_$($siteCode):SMS_MachineVariable"
}
$MachineVariables = $objPCSet.MachineVariables
$objPCSet.Get()
$count = $objPCSet.MachineVariables.Length
For ($i = 0;$i -lt $ComputerVariablesName.Count; $i++)
{
Write-Host "adding computer variable: "$ComputerVariablesName[$i]"="$ComputerVariablesValue[$i] -ForegroundColor Green
$MachineVariables[($count+$i)].Name = $ComputerVariablesName[$i]
$MachineVariables[($count+$i)].Value = $ComputerVariablesValue[$i]
}
#Write Citrix Computer variables
$objPCSet.MachineVariables = $MachineVariables
$objPCSet.Put()