Search code examples
windowspowershellgoogle-workspace

Executing an EXE file using a PowerShell script with arguments for GCDS


I am new to windows and PowerShell on the admin level. I have experience in Linux and prefer using Python but I have a difficult time understanding windows environments. In bash and Linux I am used to running shell scripting with cronjobs but in Windows, I'm having an issue running this command in the Task Scheduler. I need to be able to run Google Cloud Directory Sync so that our AD is in sync with Gsuite. I wrote a batch file that works but I feel its a bit dated to use a bat file

cd C:\Program Files\Google Apps Directory Sync\
sync-cmd.exe -a -o -c config.xml

my best guess is this needs to run as a PowerShell script through task scheduler, but I don't know where to start. I found this so far but I get an error that I don't know how to interpret.

Start-Process sync-cmd.exe -ArguementList "-a -o -c C:\Somepath\config.xml"

sorry for being a noob thanks in advance! Also for an additional resource here's the GCDS command page.

https://support.google.com/a/answer/6152425?hl=en


Solution

  • Option 1 - Schedule your EXE directly through Task Scheduler

    No need for powershell. You can just provide the full path to the EXE and the arguments using the Windows Task Scheduler User Interface. You can specify a working folder using the Start in option.

    Option 2 - Schedule a PowerShell script using Task Scheduler

    I find using the -File option of PowerShell.exe very useful when scheduling a PowerShell script using Task Scheduler. In this case, I would use the cmdlet Start-Process and I would encapsulate the arguments inside the PowerShell script.

    Example

    "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -File "c:\MyScript.ps1"
    

    MSDN

    https://learn.microsoft.com/en-us/powershell/scripting/components/console/powershell.exe-command-line-help?view=powershell-6

    Syntax

    PowerShell[.exe]
           [-Command { - | <script-block> [-args <arg-array>]
                         | <string> [<CommandParameters>] } ]
           [-EncodedCommand <Base64EncodedCommand>]
           [-ExecutionPolicy <ExecutionPolicy>]
           [-File <FilePath> [<Args>]]
           [-InputFormat {Text | XML}]
           [-Mta]
           [-NoExit]
           [-NoLogo]
           [-NonInteractive]
           [-NoProfile]
           [-OutputFormat {Text | XML}]
           [-PSConsoleFile <FilePath> | -Version <PowerShell version>]
           [-Sta]
           [-WindowStyle <style>]
    
    PowerShell[.exe] -Help | -? | /?
    

    Example from my laptop

    enter image description here

    Passing arguments through Start-Process

    If you are using Start-Process then you can pass an array of arguments through a comma delimited string.

    PS C:\> Start-Process -FilePath "$env:comspec" -ArgumentList "/c dir `"%systemdrive%\program files`""
    

    https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/start-process?view=powershell-6