计划任务的PowerShell脚本与字符串数组参数 [英] Scheduled Task for PowerShell Script with String Array Parameter

查看:159
本文介绍了计划任务的PowerShell脚本与字符串数组参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个从命令行管理程序运行完全PowerShell脚本。我试图把它安装在Windows Server 2008 R2的计划任务工作,我不能确定如何传递参数给我字符串数组参数。

I've created a PowerShell script that runs perfectly from the Management Shell. I'm trying to get it setup to work in a scheduled task in Windows Server 2008 R2 and am unsure how to pass the parameters for my string array parameter.

下面是我的脚本的相关部分:

Here is the relevant portion of my script:

[CmdletBinding()]
param(
    [Parameter(Mandatory=$true)]
    [String]
        $BaseDirectory,
    [String]
        $BackupMethod = "Full",
    [Int]
        $RemoveOlderThanDays = 0,
    [String]
        $LogDirectory,
    [Int]
        $LogKeepDays = 7,
    [String[]]
        $AdditionalDirectories
)

if ($AdditionalDirectories -and $AdditionalDirectories.Count -gt 0) {
    Write-Host "  Additional Directories to be included:" -ForegroundColor Green
    $AdditionalDirectories | ForEach-Object {
        Write-Host "     $_" -foregroundcolor green
    }
}

给予麻烦参数是最后一个, $ AdditionalDirectories

如果我运行在命令行管理程序这样的脚本,它完美的作品:

If I run the script from the Management Shell like this, it works perfectly:

.\FarmBackup.ps1 \\SomeServer\Backups Full 0 D:\Logs\Backups 0 "D:\Documents\PowerShell Scripts","D:\SomeFolder"

结果:

   Additional Directories to be included:
      D:/Documents/PowerShell Scripts
      D:/SomeFolder

从计划任务:

动作: 启动程序

程序/脚本: PowerShell.exe

参数: - 文件D:\\文档\\ PowerShell脚本进行\\ FarmBackup.ps1\\\\ SomeServer \\备份全0 D:\\日志\\备份0D:\\文档\\ PowerShell的脚本,D:\\ SomeFolder

结果:(从日志文件)

  Additional Directories to be included:
     D:\Documents\PowerShell Scripts,D:\SomeFolder


我试过几个这些参数不同的方法,但我似乎无法让他们被视为字符串数组中的2个独立的字符串。我硬编码它们现在,但似乎必须有一种方法,使这项工作,因为从shell中运行时,它是完全有效的。


I've tried a couple of different methods for those parameters but I can't seem to get them to be seen as 2 separate strings in the string array. I'm hardcoding them for now, but it seems like there must be a way to make this work since it's totally valid when run from the shell.

推荐答案

尝试使用 - 命令开关代替 - 文件开关,然后使用调用操作符'和;。这里是与预定任务这样的一个例子的链接:

Try using the -Command switch instead of the -File switch, and then use the invocation operator '&'. Here is a link to an example of doing this with scheduled tasks:

<一个href=\"http://blogs.technet.com/b/heyscriptingguy/archive/2011/01/12/schedule-powershell-scripts-that-require-input-values.aspx\">http://blogs.technet.com/b/heyscriptingguy/archive/2011/01/12/schedule-powershell-scripts-that-require-input-values.aspx

是这样的:

-Command "& 'D:\Documents\PowerShell Scripts\FarmBackup.ps1' '\\SomeServer\Backups' 'Full' 0 'D:\Logs\Backups' 0 'D:\Documents\PowerShell Scripts','D:\SomeFolder'"

我用的内容创建一个脚本测试此解决方案:

I tested this solution by creating a script with the contents:

param([string[]] $x)
Write-Host $x.Count

然后把它称为有以下两种方式:

Then called it in the following two ways:

powershell -File ".\TestScript.ps1" "what1,what2"

与结果:1

powershell -Command "& .\TestScript.ps1 what1,what2"

与结果:2

这篇关于计划任务的PowerShell脚本与字符串数组参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆