• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 979
  • Last Modified:

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

0
teamfox201
Asked:
teamfox201
  • 2
  • 2
1 Solution
 
merowingerCommented:
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

0
 
teamfox201Author Commented:
So now I am getting:

Error when trying to delete the Computer Object. Error:424 - Object required.
0
 
merowingerCommented:
ok you've a complicated code to delete a computer object.

Could you try this one?
strAdminUser = "" 'If needed
strAdminpasswd = "" 'If needed
strServername = "Servername"
strSitecode = "XYZ"
strNameSpace = "root/sms/site_" & strSitecode

Set objWBEMLocator = CreateObject("WbemScripting.SWbemLocator")
Set objSWbemServices = objWBEMLocator.ConnectServer(strServername, strNameSpace, strAdminUser, strAdminpasswd)

Set colResourceIDs = objSWbemServices.ExecQuery("SELECT * FROM SMS_R_System where NetBiosName  = '" & InputBox("Please enter the computer name") & "'")
For Each objResID In colResourceIDs
	objResID.Delete_()
Next

Open in new window

0
 
teamfox201Author Commented:
That worked! Thank you sooooooo much!
0

Featured Post

Get quick recovery of individual SharePoint items

Free tool – Veeam Explorer for Microsoft SharePoint, enables fast, easy restores of SharePoint sites, documents, libraries and lists — all with no agents to manage and no additional licenses to buy.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now