Hello Friends,
If I have 200 machines MAC Addresses and each MAC address having 2-3 machines in the SCCM console and you want to delete each machine from the console. you will be irritated doing this job manually. the reason behind it, its time-consuming activity and repetitive task.
why you are not doing smart work? how?
Let's say we have a scenario like above and you have one PowerShell script in which you added all the MAC addresses into it and just run the script and it deleted all the desired machines from the console while you finish a cup of tea 😉
Yes, We can create the PowerShell script like this using ConfigurationManager.psd1 SCCM PS module.
Import the PowerShell module first and then connect to the Primary Site Server. To get the MAC Addresses from a file, we have to create one input file named "macaddressinpput.txt" in the script root directory where we have created and kept the PowerShell script. Once you create the input text file paste all the MAC Addresses into the file and runs the below script with SCCM admin privileges.
While you run the script, This script will take MAC address input from the input file and search the hostname against each MAC address on the SCCM console and return the valid hostname of each MAC Address and write the same in the "hostnameinput.txt" text file on the script root directory.
Next, Once hostname collections are finished, the script will get the content from the "hostnameinput.txt" file and delete each hostname from the SCCM console.
While script execution, you can find the logs on the same path(Script Root path) with the name "clientremoval.log"
If you want to delete the machine based on the hostname of the machine, please use the script written here.
We always welcome your thoughts and valuable feedback.
<#
.SYNOPSIS
Provide MAC Addresses and delete Machines from the SCCM console.
.DESCRIPTION
This script will delete the hostname from SCCM based on the MAC Address you provided as the input file.
.NOTES
=============================================================================================================================================
Created with: Windows PowerShell ISE
Created on: August 18, 2018 12:35:04 PM
Created by: Dashrath Chate
Organization: Organization Name
=============================================================================================================================================
#>
Set-ExecutionPolicy -ExecutionPolicy Bypass
$ScriptDir = Split-Path $script:MyInvocation.MyCommand.Path
$log
= "$ScriptDir\clientremoval.log"
$hostnameout="$ScriptDir\hostnameinput.txt"
$date
= Get-Date -Format "dd-MM-yyyy
hh:mm:ss"
"--------------------- Script executed on $date (dd-MM-yyyy hh:mm:ss)
---------------------" + "`r`n" | Out-File $log -append
Import-Module (Join-Path $(Split-Path $env:SMS_ADMIN_UI_PATH) ConfigurationManager.psd1)
$SiteCode = Get-PSDrive -PSProvider CMSITE
$SMSProvider=$sitecode.SiteServer
Set-Location "$($SiteCode.Name):\"
$MacAddress=Get-Content $ScriptDir"\macaddressinpput.txt"
ForEach ($hostname in $MacAddress)
{
(Get-WMIObject -ComputerName $SMSProvider -Namespace root\sms\site_$SiteCode -Class SMS_R_System | Where-Object { $_.MacAddresses[0] -eq $hostname}).name
|Out-File $hostnameout -Append
}
$hostnamein=Get-Content $ScriptDir"\hostnameinput.txt"
ForEach ($client in $hostnamein)
{
$CN=Get-CMDevice -Name $client
$name=$CN.Name
}
if ($name)
{
try {
Write-Progress -Activity "Fount 5 wait"
"$date [INFO]`t $name found in SCCM " | Out-File $log -append
#Remove-CMDevice
-name $client -force
"$date [INFO]`t $name removed from SCCM " | Out-File $log -append
Start-Sleep -Seconds 6
}
catch
{"$date [INFO]`t $name found in SCCM but unable
to delete record.Check further " | Out-File $log -append
}
}
else
{Write-Progress -Activity "Fount 60 wait"
"$date [INFO]`t $hostname not found in SCCM " | Out-File $log -append
Start-Sleep -Seconds 60
}
No comments:
Post a Comment
Leave your valuable words here for improve better.