【问题标题】:Run parameters in a Powershell script from a batch file从批处理文件运行 Powershell 脚本中的参数
【发布时间】:2015-01-03 02:43:26
【问题描述】:

我正在尝试从批处理文件中指定 powershell 脚本中的参数。

脚本本身如下所示:

    Param(
    [Parameter(Mandatory=$true,HelpMessage="Application switch")]
    [string[]]$Apps,
    [ValidateRange(3,9999)]
    [int]$SwitchDelay = 30

$AppPids = @()
$Windows = Get-Process | ? { $_.MainWindowTitle -ne "" }
$Wsh = New-Object -COM Wscript.Shell

foreach ($App in $Apps) {
    foreach ($Window in $Windows) {
        if ($Window.MainWindowTitle -like $App) {
            Write-Verbose "Vindusfilter ""$App"" found hit on ""$($Window.MainWindowTitle)"" med PID ""$($Window.Id)"""
           $AppPids += $Window.Id
        }
    }
}

do {
    foreach ($ID in $AppPIDS) {
        # Hides text...
        $Wsh.AppActivate($ID) | Out-Null
        Start-Sleep -Seconds $SwitchDelay
        Write-Verbose "Changed window to PID ""$ID"""
    }
} while ($true)

而我想要做的是在批处理文件中定义类似于:

%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Bypass -File "C:\AppRotate.ps1" -Apps "*Chrome*", "Spotify*" -Switchdelay 5
pause

(这里应该显示错误信息,首先需要更多的声望...) 错误:“... PositionalParameterNotFound.Approtate.ps1”

我基本上是脚本新手,有什么想法吗?

【问题讨论】:

  • Param 的右括号在哪里(
  • 它在复制/粘贴过程中丢失了:)

标签: powershell batch-file command-line-arguments


【解决方案1】:
%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Bypass -File   "C:\AppRotate.ps1" -Apps "*Chrome*","Spotify*" -Switchdelay 5

问题在于参数 -Apps 的第一个和第二个变量之间的空格。

现在应该可以工作了。

【讨论】:

  • 哇,真不敢相信我错过了。谢谢!
猜你喜欢
  • 2012-10-20
  • 2013-10-20
  • 2013-02-18
  • 2015-07-06
  • 2020-03-22
  • 1970-01-01
  • 1970-01-01
  • 2011-09-15
相关资源
最近更新 更多