DESCRIPTION
Execute an Exchange cmdlet with parameters passed by virtual attributes in script policy. The .Net Framework, PowerShell and Exchange Management Tools are required to be installed on the computer where ActiveRoles Server Administrative Service is running.
Please, replace the edsvaArgument1/edsvaArgument2 names with the name of your virtual attributes, change "Some-ExchangeCommand" to the real Exchange cmdlet name (for example, "Enable-Mailbox"), and change "-Argument1"/"-Argument2" to real cmdlet parameter names.
CAUTION! Exchange cmdlet execution is marked as non-interactive, but it still can ask user input anyway. To avoid this, please select the cmdlet and its arguments with care.
Note This code may use functions from the Active Roles Script Policy Best Practices. Please, follow the link to obtain instructions and code for those functions.
SCRIPT
'*********************************************************************************
' THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
' EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
' WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
'
' IF YOU WANT THIS FUNCTIONALITY TO BE CONDITIONALLY SUPPORTED,
' PLEASE CONTACT ONE IDENTITY PROFESSIONAL SERVICES.
'*********************************************************************************
Option Explicit
'***********************************************************************************
'** CUSTOMIZABLE SETTINGS
'***********************************************************************************
Const strPowerShellFileName = "C:\WINDOWS\system32\WindowsPowerShell\v1.0\PowerShell.exe"
Const strExchangeSnapinFileName = "C:\Program Files\Microsoft\Exchange Server\bin\exshell.psc1"
'***********************************************************************************
'** SUBROUTINES
'***********************************************************************************
Function GetParameter(ByVal strName, ByRef Request)
On Error Resume Next
GetParameter = Request.Parameter(strName)
On Error GoTo 0
End Function ' GetParameter
'***********************************************************************************
Function ExecutePowerShellCmdlet(ByVal strShellCommand)
Dim objShell, strFullShellCommand, numReturnCode
'--- needed objects
Set objShell = CreateObject("WScript.Shell")
'--- construct a full command for shell
strFullShellCommand = """" & strPowerShellFileName & """" & _
" -PSConsoleFile """ & strExchangeSnapinFileName & """" & _
" -NoLogo -NonInteractive" & _
" -Command """ & strShellCommand & """"
'--- execute a command and get a return code
numReturnCode = objShell.Run(strFullShellCommand, 0, True)
'--- numReturnCode = 0 means that the command was executed successfully
'--- otherwise, it was failed
ExecutePowerShellCmdlet = numReturnCode
End Function
'***********************************************************************************
'** EVENT HANDLERS
'***********************************************************************************
Sub onPostCreate(Request)
'--- handle user object only ---
If (IsObjectClassRequested("user", Request)) Then Exit Sub
Dim strArgument1, strArgument2
'--- get values from virtual attributes ---
strArgument1 = GetAttribute("edsvaArgument1", Request)
strArgument2 = GetAttribute("edsvaArgument2", Request)
Dim objOctetString
Dim strObjectGUID, strDomainControllerFQDN
Dim strShellCommand, numReturnCode
'--- helping objects ---
Set objOctetString = CreateObject("AelitaEDM.EDMOctetString")
'--- get object GUID as string value ---
Call objOctetString.Set(DirObj.GUID)
strObjectGUID = objOctetString.GetGuidString()
'--- get domain controller fully-qualified domain name ---
strDomainControllerFQDN = GetParameter("LdapServer", Request)
'--- Excahnge 2007 commandlet name ---
strShellCommand = "Some-ExchangeCommand"
'--- add the first argument ---
If (Not IsEmpty(strArgument1)) Then
strShellCommand = strShellCommand & " -Argument1 " & strArgument1
End If
'--- add the second argument ---
If (Not IsEmpty(strArgument2)) Then
strShellCommand = strShellCommand & " -Argument2 " & strArgument2
End If
'--- add the rest arguments ---
strShellCommand = strShellCommand & _
" -Identity " & strObjectGUID & _
" -DomainController " & strDomainControllerFQDN
'--- execute a command and get a return code ---
numReturnCode = ExecutePowerShellCmdlet(strShellCommand)
'--- numReturnCode = 0 means that the command was executed successfully ---
'--- otherwise, it was failed ---
End Sub
'***** END OF CODE ***************************************************************