Options

Scheduling Powershell script

mikedisd2mikedisd2 Member Posts: 1,096 ■■■■■□□□□□
I can't get the syntax right on this one. I need to execute an Exchange Powershell script from the command line;

PowerShell.exe -PSConsoleFile "C:\Program Files\Microsoft\Exchange Server\Bin\ExShell.psc1" & 'c:\scripts\Report.ps1'

Running this in DOS returns the following:

Windows PowerShell
Copyright (C) 2006 Microsoft Corporation. All rights reserved.

PS C:\>


My PS book hasn't arrived yet. What am I doing wrong?

Comments

  • Options
    PashPash Member Posts: 1,600 ■■■■■□□□□□
    What is your Execution policy set to?

    From shell:-

    Get-Executionpolicy
    DevOps Engineer and Security Champion. https://blog.pash.by - I am trying to find my writing style, so please bear with me.
  • Options
    mikedisd2mikedisd2 Member Posts: 1,096 ■■■■■□□□□□
    The ExecutionPolicy was set to RemoteSigned; I've changed it to unrestricted but no change.

    After running the command from DOS and then exiting the PS C:\> prompt, it states "The system cannot find the path specified." It's not recognising the path I'm providing even though it's vaild.

    Running & 'c:\scripts\Report.ps1' or & '.\scripts\Report.ps1' from the PS prompt executes ok but not from the single DOS command.
  • Options
    ClaymooreClaymoore Member Posts: 1,637
    Instead of using the -PSConsoleFile switch, try adding the Exchange Snap-in inside your PowerShell script:

    Add-PSSnapin Microsoft.Exchange.Management.PowerShell.Admin

    Wait, when copying your code I just realized you aren't using the -command switch when specifying the PowerShell script.

    Try:
    PowerShell.exe -PSConsoleFile "C:\Program Files\Microsoft\Exchange Server\Bin\ExShell.psc1" -command & 'c:\scripts\Report.ps1'

    Or:
    PowerShell.exe -command & 'c:\scripts\Report.ps1'
    If you include the Exchange Snapin inside the PowerShell script
  • Options
    mikedisd2mikedisd2 Member Posts: 1,096 ■■■■■□□□□□
    Got it sorted, it's a case of quotation marks after the -command. The working executable is:

    PowerShell.exe -PSConsoleFile "C:\Program Files\Microsoft\Exchange Server\Bin\ExShell.psc1" -command "& 'c:\scripts\MailboxSizeReport.ps1'"

    Thanks for the help; Add-PSSnapIn is a also new one for me to know and love.
Sign In or Register to comment.