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