locked
Trying to create a Batch file to run daily for Exchange Contacts RRS feed

  • Question

  • We have contacts that need the box checked daily (Automatically update e-mail addresses based on e-mail policy)on our exchange server but because of our enviroment we are running it needs to be done through a powershell command. It works fine if I run the command from the exchange powershell and does what we need it to do, but I like to setup a batch file to run nightly like I have some other batch files that do. This is the command I am using to launch powershell and also then update the contacts to put a check in Automatically update e-mail addresses based on e-mail policy.

    $contacts = Get-MailContact -OrganizationalUnit "Domain.com/Users Email/User Contacts" -ResultSize Unlimited | Set-MailContact -EmailAddressPolicyEnabled $true

    If I run this one from the batch file I get an error and this is the error and not sure why it is throwing that error? Any help would be appreciated.

    C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -command ". 'D:\Program Files\Microsoft\Exchange Server\bin\Exchange.ps1';Add-PSSnapin Microsoft.Exchange.Management.PowerShell.Admin; $contacts = Get-MailContact -OrganizationalUnit "Domain.com/Users Email/User Contacts" -ResultSize Unlimited | Set-MailContact -EmailAddressPolicyEnabled $true
    pause

    Here is the error I receive from the bat file about.

    Get-MailContact : A parameter cannot be found that matches parameter name 'Contacts'.
    At line:1 char:153
    + . 'D:\Program Files\Microsoft\Exchange Server\bin\Exchange.ps1';Add-PSSnapin Microsoft.Exchange.Management.PowerShell.Admin; $contacts = Get-MailCo
    ntact  <<<< -OrganizationalUnit Domain.com/User Email/User Contacts -ResultSize Unlimited | Set-MailContact -EmailAddressPolicyEnabled $tru



    • Edited by CJ1862 Thursday, August 23, 2012 9:35 PM
    Thursday, August 23, 2012 9:34 PM

Answers

  • Do like this, create ps file contacts.ps1 and add below.

    $contacts = Get-MailContact -OrganizationalUnit "Domain.com/Users Email/User Contacts" -ResultSize Unlimited | Set-MailContact -EmailAddressPolicyEnabled $true

    Then create bat file and include like below.

    C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -command ". 'C:\Program Files\Microsoft\Exchange Server\V14\bin\RemoteExchange.ps1'; Connect-ExchangeServer -auto; c:\Scripts\contacts.ps1"


    James Chong MCITP | EA | EMA; MCSE | M+, S+ Security+, Project+, ITIL msexchangetips.blogspot.com

    • Marked as answer by CJ1862 Thursday, August 23, 2012 9:52 PM
    Thursday, August 23, 2012 9:41 PM

All replies

  • Do like this, create ps file contacts.ps1 and add below.

    $contacts = Get-MailContact -OrganizationalUnit "Domain.com/Users Email/User Contacts" -ResultSize Unlimited | Set-MailContact -EmailAddressPolicyEnabled $true

    Then create bat file and include like below.

    C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -command ". 'C:\Program Files\Microsoft\Exchange Server\V14\bin\RemoteExchange.ps1'; Connect-ExchangeServer -auto; c:\Scripts\contacts.ps1"


    James Chong MCITP | EA | EMA; MCSE | M+, S+ Security+, Project+, ITIL msexchangetips.blogspot.com

    • Marked as answer by CJ1862 Thursday, August 23, 2012 9:52 PM
    Thursday, August 23, 2012 9:41 PM
  • That worked perfectly I only had to change this for it to work right but you pointed me in the right direction.....Thank you so much you rock.

    C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -command ". 'D:\Program Files\Microsoft\Exchange Server\bin\Exchange.ps1';Add-PSSnapin Microsoft.Exchange.Management.PowerShell.Admin;

    Thursday, August 23, 2012 9:51 PM