使用带有 GCDS 参数的 PowerShell 脚本执行 EXE 文件

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

我是管理员级别的 windows 和 PowerShell 的新手。我在 Linux 方面有经验并且更喜欢使用 Python 但我很难理解 windows 环境。在 bash 和 Linux 中,我习惯 运行ning shell 使用 cronjobs 编写脚本,但在 Windows 中,我遇到了一个问题 运行ning任务计划程序中的此命令。我需要能够 运行 Google 云目录同步,以便我们的广告与 Gsuite 同步。我写了一个可以工作的批处理文件,但我觉得使用 bat 文件有点过时

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

我最好的猜测是这需要通过任务计划程序 运行 作为 PowerShell 脚本,但我不知道从哪里开始。到目前为止我发现了这个,但我收到了一个我不知道如何解释的错误。

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

抱歉我是个菜鸟提前致谢! 此外,这里还有 GCDS 命令页面,可作为额外资源使用。

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

选项 1 - 通过任务计划程序直接安排您的 EXE

不需要powershell。您可以使用 Windows 任务计划程序用户界面提供 EXE 和参数的完整路径。您可以使用 Start in 选项指定工作文件夹。

选项 2 - 使用任务计划程序安排 PowerShell 脚本

我发现使用 PowerShell.exe-File 选项在使用任务计划程序计划 PowerShell 脚本时非常有用。在这种情况下,我会使用 cmdlet Start-Process 并将参数封装在 PowerShell 脚本中。

例子

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

MSDN

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

语法

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 | -? | /?

来自我的笔记本电脑的例子

通过 Start-Process 传递参数

如果您使用的是 Start-Process,则可以通过逗号分隔的字符串传递参数数组。

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

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

我发现 PowerShell 与带有变量的 python 和 bash 非常相似。这作为脚本运行,然后我附加到任务计划程序。

$msbuild = "C:\Program Files\Google Apps Directory Sync\sync-cmd.exe"
$arguments = "-a -o -c config.xml"
start-Process -FilePath $msbuild $arguments

您的错误表明 Start-Process 没有名为 ArguementList 的参数。您可以使用 Get-Help 获取可用参数列表。

Get-Help Start-Process -Parameter * | Select-Object Name

确实ArguementList不可用,但ArgumentList可用。您的命令中只是有一个拼写错误。

以下应该可以正常工作:

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