Monday, August 01, 2016

Script to Pull Applications Assigned to User for OSD

GetUserAppList.ps1
#Set $Username to SMSTSUdaUsers variable
$TaskSequenceEnvironment = New-Object -COMObject Microsoft.SMS.TSEnvironment
$Username = $TaskSequenceEnvironment.Value("SMSTSUdaUsers")

if ($UserName -like '*\*') { $UserName = ($UserName -split '\\')[1] }

# Define default parameters (Splatting)
$Splatting = @{
    ComputerName = "infcmps01.corplan.net"
    NameSpace = "root\SMS\Site_XXX"
}

# Find the User in SCCM CMDB
$User = Get-WMIObject @Splatting -Query "Select * From SMS_R_User WHERE UserName='$UserName'"

#Retrieve the collections the user is member of
# Find the collections where the user is member of
$Collections = Get-WmiObject -Class sms_fullcollectionmembership @splatting -Filter "ResourceID = '$($user.resourceid)'"

#Retrieve the deployments of each Collections and output the information
# For each collection we find the deployments
# Then output an object with information of the user, collection and application advertised
Foreach ($Collection in $collections)
{
    # Find the Deployment on one collection                    
    $Deployments = (Get-WmiObject @splatting -Query "Select * From SMS_DeploymentInfo WHERE CollectionID='$($Collection.CollectionID)'")
    
    Foreach ($Deploy in $Deployments)
    {
        $deploy.targetname | Out-File -append C:\SCCM_Logs\UDAList.txt
                    
    }
   

    }

SetTSUserAppVariables
# Create an TaskSequence Environment Object
$TaskSequenceEnvironment = New-Object -COMObject Microsoft.SMS.TSEnvironment
$basevariablename = 'XXX'
$applicationlist = get-content 'C:\SCCM_logs\UDAList.txt'

# Create a Counter
$Counter = 1
# Foreach Application we create an incremented variable
$ApplicationList | ForEach-Object {

# Define the Variable Name
$Variable = "$BaseVariableName{0:00}" -f $Counter

# Create the Task Sequence Variable
$TaskSequenceEnvironment.value("$Variable") = "$_"

# Increment the counter
[void]$Counter++


}