PowerShell Add Variables to Machines from CSV SCCM ConfigMgr

So it used to be a bit trickier to add a custom variable or multiple variables to a machine object in SCCM/ConfigMgr. But since the addition of the awesome New-CMDeviceVariable Cmdlet it’s a breeze!

Basically I’m in the midst of automating a clients Server Builds. As they have a lovely spreadsheet with all details of each server I thought I’d nock this up as a starter this evening.

So the script imports the CSV specified and from there creates the variables supplied in the CSV to the machine that is named in the CSV (that’s a lot of CSV). If the script cant find the Machine object (say you are doing hundreds or thousands!) that is named in the CSV file it will log that name to a text file called “NotFound.txt” in the script root folder.

As always comments throughout the script explain every step.

Anyway here is the script and CSV example:

#########################################################################################################
#Script Author: SCCMOG - Richie Schuster 24/05/2017 WWW.SCCMOG.COM                                      #
#Modded By:     Your Name Here                                                                          #
#Script Name:   Add Variables to Machines from CSV                                                      #
#########################################################################################################
#Script Usage: Add-VariablesMachines.ps1 -SiteCode S0G -CSVParam MyCSV.csv                              #
#########################################################################################################

#Script Parameters
Param
(
[parameter(mandatory=$true,HelpMessage="Please, provide your SiteCode.")][ValidateNotNullOrEmpty()][String]$SiteCode,
[parameter(mandatory=$true,HelpMessage="Please, provide a CSV to import. It must be in the same folder as the script e.g. YourCSV.CSV")][ValidateNotNullOrEmpty()][String]$CSVParam
)

# Check for elevation
Write-Host "Checking for elevation"
If (-NOT ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole(`
    [Security.Principal.WindowsBuiltInRole] "Administrator"))
{
    Write-Warning "Please run from elevated PowerShell prompt!"
    Write-Warning "Aborting script..."
    Break
}

#Double check CSV param
If ($CSVParam -ne ""){

    #Create path to CSV (back compat PS 2.0)
    $path     = Split-Path -parent $MyInvocation.MyCommand.Definition 
    $newpath  = $path + "\$CSVParam"
    #Check location of CSV is valid.
    If (Test-Path $newpath){
        $MachineCSV = Import-Csv -Path $newpath
        #Import ConfigMgr Module
        Import-Module "$($ENV:SMS_ADMIN_UI_PATH)\..\ConfigurationManager.psd1"
        #Set the current location to be the site code.
        Set-Location "$SiteCode`:"
            #Loop through each machine in CSV
            foreach ($Machine in $MachineCSV){
                #Check If Machine exists
                $Exists = Get-CMDevice -Name $($Machine.Name)
                #If no machine found log to txt file and report
                if ($Exists -eq $null){
                    Write-Warning "$($Machine.Name) not Found Logged to Notfound.txt"
                    Add-Content "$path\NotFound.txt" "$($Machine.Name)" -Force
                    }
                #If machine is found Create the Variables
                Else {
                    New-CMDeviceVariable -DeviceName $($Machine.Name) -VariableName "IPAddress" -VariableValue $($Machine.IPAddress) -IsMask 0
                    New-CMDeviceVariable -DeviceName $($Machine.Name) -VariableName "SubMask" -VariableValue $($Machine.SubMask) -IsMask 0
                    New-CMDeviceVariable -DeviceName $($Machine.Name) -VariableName "Gateway" -VariableValue $($Machine.Gateway) -IsMask 0
                    New-CMDeviceVariable -DeviceName $($Machine.Name) -VariableName "DNS1" -VariableValue $($Machine.DNS1) -IsMask 0
                    New-CMDeviceVariable -DeviceName $($Machine.Name) -VariableName "DNS2" -VariableValue $($Machine.DNS2) -IsMask 0
                }
            }
        #Set location back to script root
        Set-Location $path
        }
    Else{
    Write-Host "Please enter VALID CSV Name. Make sure it is in the same folder as this script." -ForegroundColor Cyan
    }
}
Else{
    Write-Host "Please enter CSV Name. Make sure it is in the same folder as this script." -ForegroundColor Cyan
}
###############################################################################################################

CSV:

Name,IPAddress,SubMask,GateWay,DNS1,DNS2
Proliant,192.168.1.88,255.255.255.0,192.168.8.254,192.168.1.1,192.168.1.2
SCCMOG-CM-01,192.168.1.90,255.255.255.0,192.168.8.254,192.168.1.1,192.168.1.2
NotHere,192.168.1.89,255.255.255.0,192.168.8.254,192.168.1.1,192.168.1.2
NotHere2,192.168.1.89,255.255.255.0,192.168.8.254,192.168.1.1,192.168.1.2
NotHere3,192.168.1.89,255.255.255.0,192.168.8.254,192.168.1.1,192.168.1.2

Leave a Reply

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

Copyright 2016 SCCMOG | All Rights Reserved

css.php