从Active Directory和SCCM 2012 - Powershell中删除计算机资产

时间:2014-12-01 12:58:10

标签: powershell sccm

我正在尝试修改以下脚本以允许我: -

  1. 从csv
  2. 中的计算机列表中读取
  3. 从Activedirectory&中删除每台计算机。 SCCM 2012
  4. 我的PowerShell经验非常有限,所以任何帮助都会非常感激:)

    感谢Adi

    # Environment setup 
    # Import the ActiveDirectory module to enable the Get-ADComputer CmdLet 
    Import-Module ActiveDirectory
    
    $sccmServer = SCCMServer
    $sccmSite = Site
    $computerName = Import-CSV -Path Assets.csv
    
    # find and delete the computer from AD
    ForEach ($pc in $computers) {
    $dom = [System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain()
    $root = $dom.GetDirectoryEntry()
    $search = [System.DirectoryServices.DirectorySearcher]$root
    $search.filter = "(&(objectclass=computer)(name=$computerName))"
    $search.findall() | %{$_.GetDirectoryEntry() } | %{$_.DeleteObject(0)}
    
    # find and delete from SCCM
    $comp = get-wmiobject -query "select * from SMS_R_SYSTEM WHERE Name='$computerName'" -computername $sccmServer -namespace "ROOT\SMS\site_$sccmSite"
    $comp.psbase.delete()
     }
    # spit out results
    Write-Host "Deleted $computerName from AD. Removed $computerName from SCCM server $sccmServer, site $sccmSite"
    

0 个答案:

没有答案