This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Library script for calling function to connect to Exchange 2016

I have inherited a number of PowerShell scripts running as scheduled tasks on various servers, that I am looking to consolidate into ARS.  The first one that I am working on creating is a scheduled task in ARS to run against a subset of users and to disable OWA for those mailboxes.  I have created a function for connecting to Exchange 2016 that works whenever I have the function declared within the scheduled task script.  My issue is that I would like to put my connectExchange() function into a Library Script to be able to re-use and manage the function easily. 

This is my connectExchange() function that I have saved as a Library Script

function connectExchange {
    param ()
    ## Connect to Exchange
    switch ($env:COMPUTERNAME.ToUpper()) {
        "ARServer01" {
            ## ARServer01 Specific SecureString Password for the ARS Service account in the Domain where Exchange is installed
            ## Only the ARS Service Account logged into the apropriate ARS server will be able to decrypt the password
            $EncryptedPassword =  '01000000d08c9ddf0115d1118c7a00c04fc297a7k20plc11as8vl2wvoaajsm49dfcb3f3843aa8b'
            $EncryptedPassword += 'e0000000002000000000003660000c000000010000000f457abe96d08912ff2c2d8a9c49f5e3800'
            $EncryptedPassword += '00000004800000a0000000100000006e02f77c2a810bfx0p1516efcfc1a2cf68000000ce3a97c62'
            $EncryptedPassword += '8d8cf7c69f4dcd4339  Not A Real Password  95e98f83f425ed0d65a2ceb641b27236fd6f17'
            $EncryptedPassword += 'a228d27593160ca9f1k569281lb8a8389g110a0g419190000d9cd243cd7094c179a082d58a96f2e'
            $EncryptedPassword += '811cc462cf' 
        }
        "ARServer02" {
            ## ARServer02 Specific SecureString Password for the ARS Service account in the Domain where Exchange is installed
            ## Only the ARS Service Account logged into the apropriate ARS server will be able to decrypt the password
            $EncryptedPassword =  '01000000d08c9ddf0115d1118c7a00c04fc297eb010000006347355992dc964b89c4881376a8cd4'
            $EncryptedPassword += '80000000002000000000003660000c0000000100000008ad6f86586723jhgbkh5g6976f8as7d6f0'
            $EncryptedPassword += '35b06ebeaf88asdf7634k917236498751897236vs9dgf96797675bn77a75868cf2e654d9a4c82db'
            $EncryptedPassword += '6ababeef73762784eb5a4a3e2f320a48570  Not A Real Password  cab377fe3f586571f1e04'
            $EncryptedPassword += 'e6efadsf765134hjg2634978dsg978345kjh654ku6y91746adfs876dfag876asd9f76377a0efe70'
            $EncryptedPassword += 'ca9bcd4072'
        }
    }
    $ServerFQDN = 'ExchangeServer.Domain.Name'
    ## Defined the Credential Object
    $Credential = New-Object System.Management.Automation.PSCredential -ArgumentList 'ARSServiceAccount@Domain.name', $($EncryptedPassword | ConvertTo-SecureString)
    ##  Establish the PowerShell Session with the Exchange server
    $Session = New-PSSession -ConfigurationName Microsoft.Exchange `
                             -ConnectionUri http://$ServerFQDN/Powershell/ `
                             -Authentication Kerberos `
                             -Credential $Credential
    ## Load the PowerShell Session
    Import-PSSession  -AllowClobber -Session $Session
}

This is my scheduled task for disabling OWA access for a group of employees.  When I put the connectExchange() function into the scheduled task Disable OWA script, OWA is disabled for the users as intended.  When I attempt to load the connectExchange() function from the Library Script "ConnectExchange", the error that I receive on the scheduled task 'Last Run Message' is "The term 'connectExchagne' is not recognized as the name of a cmdlet, function, script file or operable program.  ..."

function onInit($context) {
    $context.UseLibraryScript("ConnectExchange")
}
connectExchange($null)

## Search for Employees to disable OWA Access for

$LDAPFilter =  "(&"
$LDAPFilter +=   "(l=CityName)"
$LDAPFilter +=   "(st=StateName)"
$LDAPFilter +=   "(homemdb=*)"                                           #Mailbox Enabled
$LDAPFilter +=   "(!(useraccountcontrol:1.2.840.113556.1.4.803:=2))"     #User Account Enabled
$LDAPFilter +=   "(|"
$LDAPFilter +=     "(title=Service Associate)"
$LDAPFilter +=   ")"
$LDAPFilter += ")"


Get-QADUser -LdapFilter $LDAPFilter -Service Domain.Name -SizeLimit 0 | foreach {
    ## Disable OWA
    Set-CASMailbox -Identity $_.UserPrincipalName `
                   -ActiveSyncEnabled $False `
                   -OWAEnabled $False `
                   -OWAforDevicesEnabled $False `
                   -EWSEnabled $False 
}

I don't really understand the onInit() function or the $context object.  Am I doing something wrong there?  I am attempting to emulate what is within the ARS SDK help file for 'Understanding library scripts'. 

Thank you

Parents Reply Children
No Data