Link to home
Start Free TrialLog in
Avatar of teamfox201
teamfox201Flag for United States of America

asked on

Delete computer from SCCM in WinPE

Hello ,

I am running the script below which works fine if I run it from my desktop, but when I add it to a task sequence I get the following error when it runs from WinPE and I am not sure how to fix it.

Line: 21
Char: 1
Error: Permission denied: 'GetObject'
Code: 800A0046



SiteServer = "*******"
provSiteCode = "*******"
GetProviderAccount = "*******"
GetProviderPassword = "*******"


strComputer=InputBox("Please enter the computer name")

Set objLocator = CreateObject("WbemScripting.SWbemLocator")
objLocator.Security_.AuthenticationLevel = 6
Set objSMS = objLocator.ConnectServer(SiteServer , "root/sms/site_" & provSiteCode, GetProviderAccount, GetProviderPassword)

'get the resource ID of the computer
intResourceID = GetResourceID(strComputer)

'Remove ResourceID
Set objResource = GetObject( "WinMgmts:\\" & SiteServer & "\root\SMS\site_" & provSiteCode & ":SMS_R_System.ResourceID=" & cint (intResourceID))
objResource.Delete_
'wscript.echo "Deleted " & strComputer & "(" & intResourceID & ")"

Function GetResourceID(strComputerName)
    Set colResourceIDs = objSMS.ExecQuery _
        ("select ResourceID from SMS_R_System where Name = '" & strComputer & "'")
    for each objResID in colResourceIDs
        GetResourceID = objResID.ResourceID
    next
End Function

Open in new window

Avatar of merowinger
merowinger
Flag of Germany image

i would debug the script by echo out the variables
SiteServer = "*******"
provSiteCode = "*******"
GetProviderAccount = "*******"
GetProviderPassword = "*******"


strComputer=InputBox("Please enter the computer name")

On Error Resume Next
Set objLocator = CreateObject("WbemScripting.SWbemLocator")
objLocator.Security_.AuthenticationLevel = 6
Set objSMS = objLocator.ConnectServer(SiteServer , "root/sms/site_" & provSiteCode, GetProviderAccount, GetProviderPassword)
If Err.Number <> 0 Then 
	MsgBox "Error when trying to connect. Error:" &Err.Number &" - " & Err.Description
	WScript.quit
End If
On Error GoTo 0

'get the resource ID of the computer
intResourceID = GetResourceID(strComputer)

'Remove ResourceID
On Error Resume Next
Set objResource = GetObject( "WinMgmts:\\" & SiteServer & "\root\SMS\site_" & provSiteCode & ":SMS_R_System.ResourceID=" & cint (intResourceID))
objResource.Delete_
If Err.Number <> 0 Then 
	MsgBox "Error when trying to delete the Computer Object. Error:" &Err.Number &" - " & Err.Description
	WScript.quit
End If
On Error GoTo 0
'wscript.echo "Deleted " & strComputer & "(" & intResourceID & ")"

Function GetResourceID(strComputerName)
	On Error Resume Next
    Set colResourceIDs = objSMS.ExecQuery _
        ("select ResourceID from SMS_R_System where Name = '" & strComputer & "'")
    
    If colResourceIDs.Count <= 0 Then 
    	MsgBox "No Computer Object found"
    	WScript.quit
    End if
    
    For Each objResID In colResourceIDs
    	If objResID.ResourceID <> "" Then
        	GetResourceID = objResID.ResourceID
        Else
        	MsgBox "No Resource ID found"
        End If
    next
End Function

Open in new window

Avatar of teamfox201

ASKER

So now I am getting:

Error when trying to delete the Computer Object. Error:424 - Object required.
ASKER CERTIFIED SOLUTION
Avatar of merowinger
merowinger
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
That worked! Thank you sooooooo much!