Provide Hostname and delete Machines from SCCM console - PowerShell automation

Hello Friends,

Suppose, if I have 200 machines hostname and each hostname 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 machines hostname 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 hostname of the machine from a file, we have to create one input file named "hostnameinput.txt" in the same directory where we have created and kept the PowerShell script. Once you create the input text file paste all the machines hostname into the file and runs the below script with SCCM admin privileges.

While you run the script, Log file will be created on the same path(Script Root path) with the name "clientremoval.log"

If you want to delete the machine based on the MAC Address, please use the script written here.

I am waiting for your valuable feedback on this.

<#

    .SYNOPSIS

    Provide Hostname and delete Machines from the SCCM console.

    .DESCRIPTION

    This script will delete the hostname from SCCM based on the hostname you provided as the input file.

    .NOTES

 =============================================================================================================================================

    Created with:     Windows PowerShell ISE

    Created on:       August 17, 2018 2:05:44 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):\"

$hostnamein=Get-Content $ScriptDir"\hostnameinput.txt"

 ForEach ($client in $hostnamein)

    {

    $CN=Get-CMDevice -Name $client

   $name=$CN.Name

   if ($name)

   {

       try {

       "$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

           }

       catch

         {"$date [INFO]`t $name found in SCCM but unable to delete record.Check further " | Out-File $log -append

      }

         }

   else

    { "$date [INFO]`t $client not found in SCCM " | Out-File $log -append}

   }

No comments:

Post a Comment

Leave your valuable words here for improve better.