I'm deploying Windows 7 with SCCM 2012 SP1. MDT is integrated in our task sequences.
Some of the machines have Roles assigned via the MDT database, some of the machines have Computer variables. During deployment, there is one variable that contains a semi-colon delimited string of collection names. Each role in the MDT database is named
for a corresponding collection in SCCM.
There is one script that runs that creates the Task Sequence variable called CollectionList which contains all the role names and collection names, they're all semi-colon delimited.
After CollectionList is generated, the script below runs and creates a direct collection membership rule for the computer it's running from.
At least, that's how it's supposed to work.
If I run the function below in a task sequence that only creates the CollectionList variable, then runs this script - it works. If this script runs as part of an OSD task sequence, the membership rules don't seem to be created. No errors are thrown, but
the rules just aren't there.
I'm out of ideas on what it might be, and open to suggestions - including suggestions on doing this all a different way.
Import-Module 'WriteLog'
$ScriptDir = Split-Path -Path $MyInvocation.MyCommand.Path -Parent
$ScriptName = Split-Path -Path $MyInvocation.MyCommand.Path -Leaf
# Import SMSTS Environment
try
{
$TSEnv = New-Object -ComObject Microsoft.SMS.TSEnvironment
Write-Host "$ScriptName - TSEnv: $TSEnv"
}
catch
{
# If the SMSTS Environment can't be created, exit the script
$Host.SetShouldExit(1)
}
Write-Log "-------------------- $ScriptName --------------------"
Write-Log "ScriptDir: $ScriptDir"
Write-Log "ScriptName: $ScriptName"
Write-Log "Date: $(Get-Date)"
Write-Log "-------------------- $ScriptName --------------------"
#region Add-SCCMResourceToCollection
Function Add-SCCMDeviceToCollection
{
[CmdLetBinding()]
param(
[Parameter( Mandatory=$true, ParameterSetName='NoCred' )]
[Parameter( Mandatory=$true, ParameterSetName='Cred' )]
[string] $SiteServer,
[Parameter( Mandatory=$true, ParameterSetName='NoCred' )]
[Parameter( Mandatory=$true, ParameterSetName='Cred' )]
[string] $SiteCode,
[Parameter( Mandatory=$true, ParameterSetName='NoCred' )]
[Parameter( Mandatory=$true, ParameterSetName='Cred' )]
[string[]] $CollectionName,
[Parameter( Mandatory=$true, ParameterSetName='NoCred' )]
[Parameter( Mandatory=$true, ParameterSetName='Cred' )]
[string] $DeviceName,
[Parameter( Mandatory=$true, ParameterSetName='Cred' )]
[System.Management.Automation.PSCredential] $Credential
)
process
{
switch ($PSCmdlet.ParameterSetName)
{
'NoCred' {
$Resource = Get-WmiObject -Namespace "root\SMS\Site_$SiteCode" -Class SMS_R_SYSTEM -Filter "Name='$DeviceName'" -ComputerName $SiteServer
$Collection = Get-WmiObject -Namespace "root\SMS\Site_$SiteCode" -Class SMS_Collection -Filter "Name='$CollectionName' and CollectionType='2'" -ComputerName $SiteServer
$NewRule = (Get-WmiObject -List -ComputerName $SiteServer -Namespace "root\SMS\Site_$SiteCode" -Class SMS_CollectionRuleDirect).CreateInstance()
}
'Cred' {
$Resource = Get-WmiObject -Namespace "root\SMS\Site_$SiteCode" -Class SMS_R_SYSTEM -Filter "Name='$DeviceName'" -ComputerName $SiteServer -Credential $Credential
$Collection = Get-WmiObject -Namespace "root\SMS\Site_$SiteCode" -Class SMS_Collection -Filter "Name='$CollectionName' and CollectionType='2'" -ComputerName $SiteServer -Credential $Credential
$NewRule = (Get-WmiObject -List -ComputerName $SiteServer -Namespace "root\SMS\Site_$SiteCode" -Class SMS_CollectionRuleDirect -Credential $Credential).CreateInstance()
}
}
$NewRule.ResourceClassName = "SMS_R_System"
$NewRule.ResourceID = $Resource.ResourceID
$NewRule.Rulename = $Resource.Name
# Commit changes and initiate the collection evaluator
$Collection.AddMembershipRule($NewRule)
$Collection.RequestRefresh()
}
}
#endregion
$User = 'DOMAIN\ServiceAccount'
$Pass = 'P4ssw0rd'
# Create a credential for connecting to the ARS Management Service
$Credential = New-Object System.Management.Automation.PSCredential(
$User,(ConvertTo-SecureString $Pass -AsPlainText -Force))
$User,$Pass = $null
If ($TSEnv.Value('OSDComputerName'))
{
Write-Log 'Getting computername from OSDComputerName task sequence variable.'
Write-Log ('Computer name:' -f $TSEnv.Value('OSDComputerName'))
$ComputerName = $TSEnv.Value('OSDComputerName')
}
else
{
Write-Log 'Getting computername from ComputerName environment variable.'
Write-Log ('Computer name:' -f $env:COMPUTERNAME)
$ComputerName = $env:COMPUTERNAME
}
if ($TSEnv.Value('CollectionList'))
{
$Collections = ($TSEnv.Value('CollectionList') -split ';' )
foreach ($Collection in $Collections)
{
Write-Log ('Adding direct membership rule for {0} in collection {1}' -f $ComputerName,$Collection)
Add-SCCMDeviceToCollection -SiteServer 'server.fqdn' -SiteCode 'CM1' -CollectionName "$Collection" -DeviceName $ComputerName -Credential $Credential
}
}