m 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")