How to remove a computer from sccm during a task sequence

Im trying to write a powershell script that will remove the computer running it from sccm during a task sequence. I have the proper server and site name in but it always returns a null resID during the task sequence but when i change it to be used in a normal powershell script in the ise it works just fine. I have the computer name saved as a task sequence variable OSDComputerName earlier in the task sequence. Can anyone help me out?

$SCCMServer = 'SERVER' 
$sitename = 'SITE'
$tsenv = New-Object -COMObject Microsoft.SMS.TSEnvironment 
$computername=$Tsenv.value("OSDComputerName")
$resID = Get-WmiObject  -computername $SCCMServer -credential $credential -query "select resourceID from sms_r_system where Name like `'$computername`'" -Namespace "root\sms\site_$sitename"
if ($resID.ResourceId -eq $null)
{$msgboxValue = "The PC does not exist in SCCM"}
else
{
    $comp = [wmi]"\\$SCCMServer\root\sms\site_$($sitename):sms_r_system.resourceID=$($resID.ResourceId)"
    $comp.psbase.delete()
    #// deletion successfull?
    if($?)
    {$msgboxValue = "Successfully deleted $computername"}
    else
    {$msgboxValue = "Could not delete $computername, error: $($error[0])"}
}

[void][System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic') 

[Microsoft.VisualBasic.Interaction]::msgbox($msgboxValue, 0, "Delete from SCCM Status")

May 29th, 2015 10:12am

What are you trying to achieve? It looks like you want to remove the complete object and not just the collection membership. If that's true then I would simply use the SMSTSPostAction task sequence variable to trigger the removal of the ConfigMgr client via something like ccmsetup.exe /uninstall as the value.
Free Windows Admin Tool Kit Click here and download it now
May 29th, 2015 2:32pm

Currently we have our task sequences setup so it checks to find if the computer is in sccm currently before it allows you to run certain task sequences, we want this task sequence to be available so the tech's in our helpdesk department would be able to run this instead of calling the sccm manager to remove it manually. The client can be left on the machine because after running this task sequence the computer should be scrubbed and re-imaged for a different use. Will this ccmsetup.exe/uninstall remove it from the sccm directory? 
  • Edited by speclou 18 hours 23 minutes ago
June 1st, 2015 9:25am

"Will this ccmsetup.exe/uninstall remove it from the sccm directory? "

No, this does not happen.

Why not just deploy the TS to existing systems as available?

Free Windows Admin Tool Kit Click here and download it now
June 1st, 2015 9:40am

to avoid multiple entries of the same computer and to keep techs from making mistakes with task sequences. The way it is currently setup it requires the tech to backup and remove from sccm before the computer can be scrubbed so we dont lose the data.
June 1st, 2015 10:13am

"to avoid multiple entries of the same computer"

Not sure what you mean here but if you are referring to multiple resources within ConfigMgr corresponding to the same client, that won't happen as ConfigMgr intelligently matches up resources by hardware id and AD SID.

Free Windows Admin Tool Kit Click here and download it now
June 1st, 2015 10:17am

Also a reason for this is to automate the decommissioning process, which will include the removal from the sccm directory.
June 1st, 2015 11:01am

Currently we have our task sequences setup so it checks to find if the computer is in sccm currently before it allows you to run certain task sequences, we want this task sequence to be available so the tech's in our helpdesk department would be able to run this instead of calling the sccm manager to remove it manually. The client can be left on the machine because after running this task sequence the computer should be scrubbed and re-imaged for a different use. Will this ccmsetup.exe/uninstall remove it from the sccm directory? 
  • Edited by speclou Monday, June 01, 2015 1:25 PM
Free Windows Admin Tool Kit Click here and download it now
June 1st, 2015 1:24pm

Currently we have our task sequences setup so it checks to find if the computer is in sccm currently before it allows you to run certain task sequences, we want this task sequence to be available so the tech's in our helpdesk department would be able to run this instead of calling the sccm manager to remove it manually. The client can be left on the machine because after running this task sequence the computer should be scrubbed and re-imaged for a different use. Will this ccmsetup.exe/uninstall remove it from the sccm directory? 
  • Edited by speclou Monday, June 01, 2015 1:25 PM
June 1st, 2015 1:24pm

Yes, I know this is an old post, but Im trying to clean them up. Did you solve this problem, if so what was the solution?

Free Windows Admin Tool Kit Click here and download it now
August 22nd, 2015 11:49am

Since no one has answer this post, I recommend opening  a support case with Microsoft Customer Support Services (CSS) as they can work with you to solve this problem.

August 29th, 2015 1:28pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics