Using WMI Delete method

Welcome Forums General PowerShell Q&A Using WMI Delete method

This topic contains 3 replies, has 3 voices, and was last updated by

 
Participant
3 years, 3 months ago.

  • Author
    Posts
  • #34948

    Participant
    Topics: 6
    Replies: 11
    Points: 0
    Rank: Member

    Hello,

    I have a PowerShell script that uses the WMI COM object to connect to the SCCM server to delete the device from the SCCM server before I add the device to the SCCM server.

    When I used WMI in VBScript to connect to the SCCM server and delete the device, it works fine. However, when I translated the VBScript code to PowerShell, it generated the output below, and the device is not deleted:

    OverloadDefinitions
    ——————-
    void Delete_ (int, IDispatch)

    The VBScript code is from this link at: http://eskonr.com/2012/04/vb-script-delete-computer-from-sccm-database/, which I translated into the PowerShell code as shown below:

    $sccmServer = 'MySCCMServer'
    $siteCode = 'MySCCMSite'
    $computer = 'PC1'
    $username = 'Domain\user'
    $password = 'secrete!'
    
    $objSWBemlocator = New-Object -ComObject WbemScripting.SWbemLocator;
    $objWMIService = $objSWBemlocator.ConnectServer($sccmServer,"root\SMS\site_"+$siteCode,$userName,$password);
    $strQry = "SELECT ResourceID FROM SMS_R_System WHERE Name='$computer'";
    $objEnumerator = $objWMIService.ExecQuery($strQry);
    foreach ($objInstance in $objEnumerator)
    {
    	foreach ($objItem in $objInstance.Properties_)
    	{
    		$resourceId = $objItem.Value;
    	}
    }
    $objDevice = $objWMIService.Get("SMS_R_System='$resourceId'")
    $objDevice.Delete_
    
    I also try:
    
    $objDevice = $objWMIService.Get("SMS_R_System='$resourceId'") | % {$_.Delete_}
    
    and the result is the same.
    
    

    Any help or comment will greatly be appreciated.

    Thanks in advance.

  • #34950

    Member
    Topics: 9
    Replies: 2322
    Points: 0
    Rank: Member

    When you call methods on objects in PowerShell, you have to include parentheses (even if you're not passing in any arguments.)

    $objDevice.Delete_()
    
  • #34951

    Participant
    Topics: 6
    Replies: 11
    Points: 0
    Rank: Member

    Dave,

    That did it.

    Thank you for the help!

  • #34966

    Participant
    Topics: 0
    Replies: 669
    Points: 0
    Rank: Member

    You really should look at using the WMI cmdlets instead of using the COM objects like this

The topic ‘Using WMI Delete method’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort