Tuesday, February 19, 2013

Arvutite kustutamine SCCM 2007'st powershelliga

 Oli nimekiri arvutitest ja vajadus need SCCM 2007 serverist ära kustutada. Käsitsi üsna tüütu neid otsida ja ükshaaval kustutada. Internetist leidsin skripti, mis otsis AD'st vanu arvuteid, tõstis need teise OU'sse ning kustutas AD'st ja SCCM'ist lõpuks arvutite kontod ära. Mul oli vaja aga ainult SCCM kustutamise osa ja kirjutasin asja ümber funktsiooniks endale.


Function Remove-WorkstationsFromSCCM {
<#
.SYNOPSIS
This Cmdlet deletes specified computers from specified SCCM server
.DESCRIPTION
This Cmdlet deletes specified computers from specified SCCM server. Computer names are specified as comma separated list of names.
You can use Powershell Common Parameters, see help about_commonparameters.
To use function dot source it - . .\Remove-WorkstationsFromSCCM.ps1
If you don't want to use it anymore, delete it - del function:\Remove-WorkstationsFromSCCM
.EXAMPLE
Remove-WorkstationsFromSCCM -Computers PC1 -SCCMServer SCCMSrv
Does not actually delete computer PC1 from SCCM server named SCCMsrv. Just dry run.
.EXAMPLE
Remove-WorkstationsFromSCCM -Computers PC1 -SCCMServer SCCMSrv -Remove
Deletes computer PC1 from SCCM server named SCCMsrv
.EXAMPLE
Remove-WorkstationsFromSCCM -Computers PC1,PC2 -SCCMServer SCCMSrv -Verbose -Remove
Deletes computers PC1 and PC2 from SCCM server named SCCMsrv and prints additional information.
.EXAMPLE
Get-Content .\PCsToDeleteFromSCCM.txt | Remove-WorkstationsFromSCCM -SCCMServer SCCMSrv -Verbose -Remove
Takes names of the computers from PCsToDeleteFromSCCM.txt and pipes it to cmdlet.
.LINK
Adapted from http://trevorsullivan.net/2010/08/17/powershell-ad-sccm-workstation-cleanup-script-version-3-0/
#>

[CmdletBinding(
# don't need whatif right now
# SupportsShouldProcess = $True
)]
Param(
    # computernames to remove from SCCM
    [parameter(Position = 0, Mandatory = $True, ValueFromPipeline = $True, HelpMessage = "Computername(s) to delete from SCCM server")]
        [String[]]$Computers,
    # The server on which your SMS Provider component is installed
    [parameter(Mandatory = $True, HelpMessage = "SCCM server name")]
        [String]$SCCMServer,
    # If set to $true, the script WILL REMOVE computers from SCCM! [switch] means, that with no -Remove switch the $Remove is $false
    [switch]$Remove
)

    begin {
        # Dynamically obtain SMS provider location based only on server name
        $tSiteCode = (Get-WmiObject -ComputerName $SCCMServer -Class SMS_ProviderLocation -Namespace root\sms).NamespacePath
        # Return only the last 3 characters of the NamespacePath property, which indicates the site code
        $SCCMSiteCode = $tSiteCode.SubString($tSiteCode.Length - 3).ToLower()
        Write-Verbose "Site code is: $SccmSiteCode"
        Write-Verbose "SiteServer is: $SCCMServer"
    }
    process {
        # Now we delete resources from the Configuration Manager database
        foreach ($Computer in $Computers) {
            $tSysQuery = "select * from SMS_R_System where Name = '$Computer'"
            $tWmiNs = "root\sms\site_" + $SccmSiteCode
            $Resources = Get-WmiObject -ComputerName $SCCMServer -Namespace $tWmiNs -Query $tSysQuery
            if ($Resources -ne $null) {
                Write-Verbose $tSysQuery
                Write-Verbose "tWmiNs is: $tWmiNs"
                foreach ($Resource in $Resources)
                {
                    $AgentTime = $($Resource.AgentTime | Sort-Object | Select-Object -Last 1)
                    $UserName = $Resource.LastLogonUserDomain + '' + $Resource.LastLogonUserName
                    Write-Verbose $Resource.ResourceID
                    Write-Verbose $Resource.Name
                    Write-Verbose $AgentTime
                    Write-Verbose $UserName
                    # This line deletes records from the ConfigMgr database
                    if ($Remove)
                    {
                        # Delete the resource from the ConfigMgr site server
                        "Deleting resource " + [string]$Resource.Name
                        $resource.Delete()
                    }
                }
            } else {
                "There was not $Computer in $SCCMServer server."
            }
        }
    }
    end {}
}

No comments:

Post a Comment