Script Repository


Delete computer from SCCM server

March 18, 2016
1652

The script deletes a computer from a System Center Configuration Manager (SCCM) Server. You can add it to your Business Rules, Custom Commands and Scheduled Tasks with the help of the Run a program or PowerShell script action.

Parameters:

  • $serverName - specifies the SCCM Server name;
  • $siteCode - specifies the SCCM Site Code;
  • $computerName - specifies the computer name. You can use value references (e.g. %cn%) to insert the computer name. When the script is executed, the value references will be replaced with property values of the AD object on which the script is executed.
Edit Remove
PowerShell
$serverName = "SCCMServer.domain.com" # TODO: modify me
$siteCode = "MyCode" # TODO: modify me
$computerName = "%cn%" # TODO: modify me

# Get Resource ID
$computer = Get-WmiObject -ComputerName $serverName -Namespace "Root\SMS\Site_$($siteCode)" -Class 'SMS_R_SYSTEM' -Filter "Name='$computerName'"

if ($computer.ResourceId -eq $NULL)
{ 
    $Context.LogMessage("The computer '$computername' has not been found in SCCM.", "Warning") # TODO: modify me
    return
}

# Delete computer account
try
{
    $computer.PSBase.Delete()
    $Context.LogMessage("Computer '$computerName' successfully deleted", "Information") # TODO: modify me
}
catch
{
    $Context.LogMessage("Could not delete '$computerName'. Error: " + $_.Exception.Message, "Error") # TODO: modify me
}

Comments ( 0 )
No results found.
Leave a comment