Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在任务计划程序中传递Powershell参数_Powershell_Scheduled Tasks - Fatal编程技术网

在任务计划程序中传递Powershell参数

在任务计划程序中传递Powershell参数,powershell,scheduled-tasks,Powershell,Scheduled Tasks,我需要从任务计划程序中传入以下参数 -错误日志 -服务 -最大尺寸 我目前的任务计划程序设置如下 程序/脚本:C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe 添加参数(可选):-Command“&\ServerName\C$\Users*****\Documents\Scripts\Scheduled-serviceretart.ps1-ErrorLog'ServerName\C$\Users*****\Documents\lo

我需要从任务计划程序中传入以下参数
-错误日志
-服务
-最大尺寸

我目前的任务计划程序设置如下
程序/脚本:C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe

添加参数(可选):
-Command“&\ServerName\C$\Users*****\Documents\Scripts\Scheduled-serviceretart.ps1
-ErrorLog'ServerName\C$\Users*****\Documents\log\ScriptErrors.txt'
服务'foo1','foo2'
-MaxSize'5MB'


当我运行计划的任务时,什么都没有发生,可能是出了什么问题。

我建议将任务计划为使用
-File
参数,而不是
-Command
。例如:

程序/脚本:
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe

添加参数(可选):
-NoProfile-ExecutionPolicy Bypass-File“Scheduled Services restart.ps1”-ErrorLog“ScriptErrors.txt”-Services“foo1”,“foo2”-MaxSize 5MB

开始位置(可选):
C:\Users\\Documents\Scripts


您可以在任务的“起始位置”属性中指定脚本的起始目录,并避免脚本和日志文件的冗长路径名。(请注意,我假设您正在本地计算机上运行脚本副本,而不是在网络上运行,这会增加潜在的复杂性和失败的可能性。)

需要首先导入该功能。我建议将函数保存为模块,并将其放置在system32或program files中的modules文件夹中。这样,当启动powershell时,它将自动导入您的函数

完成此操作后,任务调度程序非常简单

程序/脚本

function ServiceRestart
{
    Param
    (
        $ErrorLog,
        $Services,
        $MaxSize    
    )

    $Time = Get-Date -Format 'yyyy:MM:dd HH:mm:ss'
    $Result = (Get-Item $ErrorLog).length 


    if($Result -gt $MaxSize)
    {
        Clear-Content $ErrorLog
    }

    Try 
    {
        Foreach($Service in $Services)
        {
            Restart-Service -DisplayName $Service -ErrorAction Stop
        }
    } Catch 
      {
        "ERROR: $Service could not be restarted $Time" | Add-Content $ErrorLog 
      }
}

ServiceRestart -ErrorLog -Services -MaxSize
添加参数(可选):


就问题的字面回答而言:可能需要在脚本位置周围添加单引号,否则它会试图将特殊字符/反斜杠解释为转义符

例:

您还可以在powershell脚本中添加一些基本日志记录,以确保它实际启动


我在生产Powershell计划任务中使用“-Command&”样式&只要字符串格式正确,它们就可以正常工作。

显然,在使用计划任务时,引号的选择会产生巨大的差异。为您的论点尝试以下方法:

-Command "& '\ServerName\C$\Users*****\Documents\Scripts\Scheduled-ServiceRestart.ps1' ...

使用-NoProfile-ExecutionPolicy Unrestricted-File'Scheduled serviceretart.ps1'-ErrorLog'\C$\Users\\Documents\log\ScriptErrors.txt'-Services'Service1','Service2'-MaxSize'500MB'“我得到错误(0xFFFD0000)。我正在远程服务器上运行此脚本,但脚本只需要在本地执行。@Bill\u Stewart将脚本复制到本地服务器。我无法使用此处显示的示例,因为参数值周围有单引号,如
“Scheduled-ServiceRestart.ps1”
。相反,我将所有单引号替换为双引号,并且我已经解决了所有问题。如果在处理Windows命令行时发现,您应该使用双引号,因为单引号仅在极少数情况下使用。
-Command &{ServiceRestart -ErrorLog 'ServerName\C$\Users*****\Documents\log\ScriptErrors.txt' -Services 'foo1','foo2' -MaxSize '5MB'}
-Command "& '\ServerName\C$\Users*****\Documents\Scripts\Scheduled-ServiceRestart.ps1' ...
-Command "& '\\ServerName\C$\Users\...\Documents\Scripts\Scheduled-ServiceRestart.ps1' -ErrorLog '\\ServerName\C$\Users\...\Documents\log\ScriptErrors.txt' -Services 'foo1','foo2' -MaxSize '5MB'"