PowerShell add Task to run PowerShell script with parameters

你离开我真会死。 提交于 2019-12-05 18:04:23

Solved it by using \" as the inner quotes. Had to swap ' with \\\`" in PowerShell script

$command = "PowerShell \`"& \\\`"C:\ProgramFiles (x86)\MyDir\MyScript.ps1\\\`" $myStringParam $myBooleanParam\'"" 

So Task Scheduler shows

PowerShell "& \"C:\Program Files (x86)\MyDir\MyScript.ps1\" Cat 0"
Lars Truijens

Try using the -File parameter of powershell.exe to specify the script to run and just add the parameters of the script at the end

powershell.exe -File "C:\Program Files (x86)\MyDir\MyScript.ps1" Cat 0

UPDATE

Boolean and Switch parameters seem to be a problem with -File. This will work:

powershell.exe "C:\Program Files (x86)\MyDir\MyScript.ps1" Cat 0

Use the -command parameter for powershell:

What you would execute in your powershell without task sceulder:

C:\Scripts\mypsscript.ps1 -parameter 'nice value'

What you give the task sceduler:

Programm to run: Powershell

Arguments:

-Command "& C:\Scripts\mypsscript.ps1 -parameter 'nice value'"
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!