OCS PowerShell Functions

####################################################################
#     ----Name : OCS 2007 Account Manipulation tools
#     ---Author: Stephen Owen, 10/3/2014
#     ----Ver 1: Release Version, all basic features provided but unfortunately advanced features like true whatif and true pipeline support won't work due to PowerShell limitations
#     Function : A set of tools based on this blog post to speed up Enabling and Disabling OCS accounts en masse - http://blog.insideocs.com/2009/01/16/189/
#     ---Usage : Use Get-OCSUser to obtain a list of all user accounts in OCS, filter this using Where-Object commands or comparisons and pipe into Disable or Enable-OCSuser Cmdlettes
##########################################


Function Get-OcsUser{

#.Synopsis
#   A simpler wrapper for Get-WMIObject on the Microsoft SIPEUser Class
#.DESCRIPTION
#   Long description
#.EXAMPLE
#   To obtain a list of all OCS Users
#
#   Get-OCSUser -Fi;;
#.EXAMPLE
#   To obtain only the users from OCS which match those found in a text file
#
#   Get-OCSUser | Where { (Get-Content C:\temp\DisableOCS.txt) -contains $_.DisplayName}

    param([switch]$Full)
    if ($Full){
        Get-WmiObject -class MSFT_SIPESUserSetting 
    }
    else{
        Get-WmiObject -class MSFT_SIPESUserSetting -Property DisplayName,Enabled,EnabledForEnhancedPresence,EnabledForFederation,UserCategory,PrimaryURI,InstanceID | Select DisplayName,Enabled,EnabledForEnhancedPresence,EnabledForFederation,UserCategory,PrimaryURI,InstanceID 
    }
}


Function Enable-OcsUser{
##
#.Synopsis
#   A wrapper for cretain WMI calls using Get-WMIObject on the Microsoft SIPEUser Class to enable users
##.DESCRIPTION
##Use this wrapper to ease the enablement of OCS Users using PowerShell.  Be sure to see the Examples for usage notes, as #PowerShell v1 limitations force some peculiar usage steps.
#.EXAMPLE
##   Step 1 Obtain a list of all OCS Users and store in $OCS or a similar string
##
##   $OCS = (Get-OcsUser)
##
##   Step 2 Verify user to enable
##
##   $OCS | Where-Object {$_.PrimaryURI -eq "sip:Testy.McDelete@DomainOLD.Domain.com"} 
##
##   Step 3 ENABLE
##
   ##$OCS | Where-Object {$_.PrimaryURI -eq "sip:Testy.McDelete@DomainOLD.Domain.com"} | ForEach-Object {Enable-OCSUser -PrimaryURI $_.DisplayName} 
#.EXAMPLE
##   To obtain only the users from OCS which match those found in a text file, then enable them.  NOTE the usage of -WhatIf. 
##
##   Get-OCSUser | Where { (Get-Content C:\temp\DisableOCS.txt) -contains $_.DisplayName} | ForEach-Object {Enable-OCSUser -PrimaryURI $_.DisplayName -WhatIf} 
##
Param($PrimaryURI,[switch]$WhatIf)
        ForEach ($SIP in $PrimaryURI) {
            #get-wmiobject -class MSFT_SIPESUserSetting | where-object { $_.PrimaryURI -eq “sip:userid@SIPDomain” } | % { $_.Enabled = $True; $_.put() | out-null }
        if ($WhatIf){
            "Safety enablied, Would be enabling $PrimaryURI"
        }
        ELSE{
            "Safety released, actually enabling $SIP"
             Get-WmiObject -class MSFT_SIPESUserSetting | Where-Object { $_.DisplayName -eq $SIP } | ForEach-Object { $_.Enabled = $True; $_.put()
             Get-OcsUser | Where-Object {$_.DisplayName -eq $SIP}}
        }

        
        
        }
}


Function Disable-OcsUser{
##
#.Synopsis
#   A wrapper for cretain WMI calls using Get-WMIObject on the Microsoft SIPEUser Class to disable users
##.DESCRIPTION
##Use this wrapper to ease the deletion of OCS Users using PowerShell.  Be sure to see the Examples for usage notes, as #PowerShell v1 limitations force some peculiar usage steps.
#.EXAMPLE 
##   Step 1 Obtain a list of all OCS Users and store in $OCS or a similar string, really have to do this to not overload on WMI quota
##
##   $OCS = (Get-OcsUser)
##
##   Step 2 Verify user to disable
##
##   $OCS | Where-Object {$_.PrimaryURI -eq "sip:Testy.McDelete@DomainOLD.Domain.com"} 
##
##   Step 3 DELETE
##
##   $OCS | Where-Object {$_.PrimaryURI -eq "sip:Testy.McDelete@DomainOLD.Domain.com"} | ForEach-Object {Disable-OCSUser -PrimaryURI $_.DisplayName} 
#.EXAMPLE
##   To obtain only the users from OCS which match those found in a text file, then disable them.  NOTE the usage of -WhatIf. 
##
##   Get-OCSUser | Where { (Get-Content C:\temp\DisableOCS.txt) -contains $_.DisplayName} | ForEach-Object {Disable-OCSUser -PrimaryURI $_.DisplayName -WhatIf} 
##>
Param($PrimaryURI,[switch]$WhatIf)
        "Process started at:"
        Get-date
        ForEach ($SIP in $PrimaryURI) {
            if ($WhatIf){
                "Would be Disabling $PrimaryURI"
            }
            ELSE{
                "Safety released, disabling $SIP"
               # Get-WmiObject -query "select * from MSFT_SIPESUserSetting where DisplayName = '$SIP'"
                #Get-WmiObject -Class MSFT_SIPESUserSetting | Where-Object { $_.DisplayName -eq $SIP } | ForEach-Object { $_.Enabled = $False; $_.put()              
                    #Get-OcsUser | Where-Object {$_.DisplayName -eq $SIP}
                Get-WmiObject -query "select * from MSFT_SIPESUserSetting where DisplayName = '$SIP'" | ForEach-Object { $_.Enabled = $False; $_.put() }
                    
            }

        }
}
Advertisements

One thought on “OCS PowerShell Functions

Have a code issue? Share your code by going to Gist.github.com and pasting your code there, then post the link here!

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s