sccm powershell to write variables to devices in collection

sccm powershell to write variables to devices in collection


Table of Contents

sccm powershell to write variables to devices in collection

Managing Configuration Manager (SCCM) environments often requires deploying settings and variables to specific devices. PowerShell provides a robust method for achieving this, especially when targeting devices within collections. This guide outlines how to use PowerShell to write variables to devices within an SCCM collection, covering best practices and troubleshooting tips.

Understanding the Approach

The core strategy involves leveraging SCCM's WMI (Windows Management Instrumentation) interface through PowerShell. We'll query the SCCM database to identify devices in a specific collection and then use remote PowerShell to execute commands on each target machine, writing the desired variables.

Identifying the Target Collection

First, we need to identify the collection containing the devices we want to target. This is done using a WMI query. You'll need the SMS_Collection WMI class and the name (or unique ID) of your collection.

$CollectionName = "YourCollectionName" # Replace with your collection name

$Collection = Get-WmiObject -Namespace root\sms -Class SMS_Collection | Where-Object {$_.Name -eq $CollectionName}

if ($Collection -eq $null) {
    Write-Error "Collection '$CollectionName' not found."
    exit 1
}

$CollectionID = $Collection.CollectionID

Remember to replace "YourCollectionName" with the actual name of your SCCM collection.

Retrieving Device Information

Next, we need to get a list of devices within the identified collection. This uses the SMS_Collection class and its related classes to retrieve the device information.

$Devices = Get-WmiObject -Namespace root\sms -Class SMS_Collection -Filter "CollectionID='$CollectionID'" | Get-WmiObject -ExpandProperty Resources | Where-Object {$_.ResourceType -eq 2} # ResourceType 2 represents computers

if ($Devices -eq $null) {
    Write-Error "No devices found in collection '$CollectionName'."
    exit 1
}

Writing Variables to Devices

Now, we iterate through the list of devices and use remote PowerShell to write the variables. We'll use the Invoke-Command cmdlet for this. This example writes variables to the environment:

$Variable1 = "Value1"
$Variable2 = "Value2"

foreach ($Device in $Devices) {
    try {
        Invoke-Command -ComputerName $Device.Name -ScriptBlock {
            $env:Variable1 = "Value1"
            $env:Variable2 = "Value2"
            Write-Host "Variables set on $($env:COMPUTERNAME)"
        }
    }
    catch {
        Write-Warning "Failed to set variables on $($Device.Name): $($_.Exception.Message)"
    }
}

This script sets the environment variables. You could adapt this to write to the registry, create files, or perform other actions as needed.

Alternative: Using a Script File

For more complex operations, it's cleaner to create a separate PowerShell script file and then execute it remotely:

$ScriptPath = "C:\path\to\your\script.ps1"  #Path to your script

foreach ($Device in $Devices) {
    try {
        Invoke-Command -ComputerName $Device.Name -FilePath $ScriptPath
    }
    catch {
        Write-Warning "Failed to execute script on $($Device.Name): $($_.Exception.Message)"
    }
}

This approach improves readability and maintainability.

Error Handling and Best Practices

  • Error Handling: The try-catch blocks are crucial for handling potential errors, such as network issues or permissions problems. Always include comprehensive error handling.
  • Credentials: If your script requires elevated permissions on the target devices, you'll need to specify credentials using the -Credential parameter of Invoke-Command.
  • Session Management: For large collections, managing multiple remote sessions efficiently is vital. Consider using techniques like PSSession to optimize performance.
  • Testing: Always test your script thoroughly on a small subset of devices before deploying to a large collection.
  • Logging: Implement comprehensive logging to track progress and identify errors.

Frequently Asked Questions (PAAs)

How do I write variables to the registry instead of environment variables?

To write variables to the registry, you'll need to use the Set-ItemProperty cmdlet within your Invoke-Command script block. For example:

Invoke-Command -ComputerName $Device.Name -ScriptBlock {
    Set-ItemProperty -Path "HKCU:\Software\MyApplication" -Name "Variable1" -Value "RegistryValue1"
}

Remember to adjust the registry path and values as needed. Ensure you have the necessary permissions to write to the registry on target machines.

Can I use this script to deploy files?

Yes, you can adapt this to deploy files using Copy-Item within the Invoke-Command script block. You would need to specify the source and destination paths. Consider using a network share for the source path for easier management.

What if a device is offline or unreachable?

The try-catch block handles failures. The script will log a warning and continue to the next device. You can enhance error handling to retry failed commands after a delay or implement more sophisticated retry mechanisms.

This comprehensive guide provides a solid foundation for using PowerShell to effectively manage and deploy variables to devices within SCCM collections. Remember to adapt the scripts to your specific requirements and always test thoroughly before deployment.