powershell - 从批处理文件运行 Powershell 脚本中的参数

标签 powershell batch-file command-line-arguments

我正在尝试从批处理文件中的 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”

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

最佳答案

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

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

现在应该可以工作了。

关于powershell - 从批处理文件运行 Powershell 脚本中的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26782078/

相关文章:

Linux:将txt文件中列出的文件从目录(带有子目录)复制到指定文件夹

batch-file - 在 var 中读取批处理文件并写回

windows - 如何从命令输出中查找和提取子字符串?

python - 使用 psutil Python 根据进程运行时间来终止进程

powershell - 使用PowerShell在cmd.exe中运行命令

python - 作为 .py 运行时出现随机模块错误

linux - 统计替换重复值后的记录数

c# - 检查命令行参数的输入值

Powershell DSC : Composite Resources with parameters not working

optimization - 优化 PowerShell 模块导入