Hey! We all know things that we want to do in MEMCM (SCCM) but we cant, because UI its not enough, yes ive been there, we all been there i think.
One thing is working with Variables on CMDevices, Its not much we can do, but with powershell we can do it all, almost 😉
So here is an scenario, We have a bunch of clients in our Collection. Let say we have a Collection for all the computers in our company. Lets call that Collection “Poppes Company”. In that collection we have a lot of Clients.
The only thing we have on each device is a variable, that says which city they are from. And let say now we want to Add a variable for all the ones who are in Stockholm. But we cant separate them from the others, because they are all in the same collection. So now its getting a little bit tricky.
We want to two variables “Name=SpecificLocation, Value=Kista ” and “Name=HouseName, Value= House101” on just devices that have variable “Stockholm”
So gonna break down a script that i made, but of course you can download it here
AddVariablesToComputerInACollection
( i have all in a script that you can download here:)
#Variables $CollectionName = "Poppes Company" #The variable value that you know $City = "Stockholm" #The variables you want to add $SpecificLocation = "Kista" $HouseName= "House201"
Also we need to save data from that collection to a .csv file, that we gonna work with later on
#CSV Path $Path = "C:\Script\$Collectionname.csv" $ResultPath = "C:\Script\result+$Collectionname.csv" #Gathering all the computers inside a collection to a variable $computer = Get-CmCollectionMember -CollectionName $CollectionName | Select Name $Computers = $Computer."name"
Now we gonna take all the computers in that collection “Poppes company” and gather all the variables
#Gathering computers to a csv file remove-item $path -ErrorAction SilentlyContinue $i=1 $Computers | ForEach-Object { Set-Location "$($SiteCode):\" @initParams $Computer = $_ $Variable = Get-CMDeviceVariable -DeviceName $_ | select name, value | where {$_.name -eq "City"} set-location "C:\" $hash = @{ "Computername" = $Computer "Variable" = $Variable } $newRow = New-object Psobject -Property $Hash Export-Csv $Path -InputObject $newrow -Append -Force write-host -ForegroundColor Cyan "Added $_ to CSV file, Computer number $i" $i++ start-sleep -Milliseconds 100 }
Now if you look in that .csv file
You see all the PC-names, and the variables for both Stockholm and other locations.
Now we gonna make a list of all the machines who have the Variable Stockholm
#Gathering data with all who have a specific Country to a List Set-location "C:\" $Data = Import-csv $Path $List = @() $Data | ForEach-Object{ if ($_.Variable -eq "@{name=City; value=$City}") { $List += $_.Computername } } $List
Now we getting a list with just the computers that have Stockholm in the variable, Its just 3 computers. And in the .csv file its just three computers there. so its correct!
Next up is to add variables to all the machines, We add SpecificLocation & HouseName to all the “Stockholm computers”
# Add variables to Device-object with that country code $k=1 Set-Location "$($SiteCode):\" @initParams $List | ForEach-Object { New-CMDeviceVariable -Devicename $_ -VariableName "SpecificLocation" -VariableValue $SpecificLocation New-CMDeviceVariable -Devicename $_ -VariableName "HouseName" -VariableValue $HouseName write-host -ForegroundColor yellow "Adding Variables to computer $_, Computer number $k" $k++ start-sleep -Milliseconds 100 }
Done, now it should look like this.
And we are done, good luck with working with variables
/Pontus