Quantcast
Channel: TechCenter
Viewing all articles
Browse latest Browse all 2350

Execute an Exchange cmdlet in script policy

$
0
0
Revision 1 posted to Active Roles Script Center by DELL-Tatiana G on 2/24/2014 6:49:03 AM

Back to PowerShell Policies

Back to Exchange management

DESCRIPTION

Execute an Exchange cmdlet with parameters passed by virtual attributes in script policy. The .Net Framework 2.0, PowerShell and Exchange 2007 Management Tools are required to be installed on computer where ActiveRoles Server Administrative Service is running.

Please, replace the edsvaArgument1/edsvaArgument2 names with name of your virtual attributes, change "Some-ExchangeCommand" to real Exchange commandlet name (for example, "Enable-Mailbox"), and change "-Argument1"/"-Argument2" to real commandlet parameter names.

CAUTION! Exchange commandlet execution is marked as non-interactive, but it still can ask user input anyway. To avoid this, please select commandlet and its arguments with care.

Note This code may use functions from the ARS 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 MERCHANTBILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.

'

' IF YOU WANT THIS FUNCTIONALITY TO BE CONDITIONALLY SUPPORTED,

' PLEASE CONTACT QUEST PROFESSIONAL SERVICES.

'*********************************************************************************

'

' This code is published on the ActiveRoles Script Center:

' http://communities.quest.com/docs/DOC-9991

'

' This code may use functions from the ARS Script Policy Best Practices:

' http://wiki.activeroles.inside.quest.com/index.php/ARS_Script_Policy_Best_Practiceshttp://communities.quest.com/docs/DOC-10016

'

' Please, follow the link to obtain instructions and code for those functions.

'*********************************************************************************

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 ***************************************************************

 

COMPATIBILITY

Script compatible with the following version(s): <Not specified>

Back to PowerShell Policies

Back to Exchange management

 


Viewing all articles
Browse latest Browse all 2350

Trending Articles