批处理文件中的Powershell未按预期工作



让我从一些背景开始。我在一个学区工作,那里有大约300台使用win 10 pro的笔记本电脑。我们正在为使用锁定浏览器的年度标准化测试做准备。无论出于何种原因,该公司创建了一个新的锁定浏览器,该浏览器不会关闭后台运行的MSEdge。如果您一登录就启动锁定浏览器,MSEdge就不是问题。

软件公司提供的解决方案是更改测试的注册表项,然后在测试后将其更改回。这对我来说似乎工作量太大了。

我的第一个选择是使用售货亭模式,但它似乎不适用于win 10 pro。我现在正在研究的解决方案是创建一个专门用于测试的用户,并在登录时使用计划任务和在退出时注销的命令。基本概念似乎适用于这种情况,只要笔记本电脑插上电源。我似乎无法获得

新计划任务设置Set–AllowStartIfOnBatteries–DontStopIfGoingOnBatteres

生效。我怀疑这和一个遗漏的论点有关,但我在谷歌搜索中找不到任何线索。

我正在使用批处理文件调用PowerShell并运行.PS1文件

.bat文件

:: this Batch file which is run by double clicking in SMACS_IT account. calls powershell to 
start the task_kiosk.ps1. Which starts the proccess of creating the scheduled task for the 
user STAAR TEST to run TXSecureBrowser.

Powershell.exe -Command "& {Start-Process Powershell.exe -ArgumentList '-ExecutionPolicy 
Bypass -File C:1kiosktask_kiosk_ps1.ps1' -Verb RunAs}"

.PS1文件

$User = "STAAR TEST"
$Description = "A task created to launch the txsecurebrowser using the kiosk_bat.bat file when 
the user STAAR TEST logs in"
$taskName = "Launch TXSecureBrowser"
$taskExists = Get-ScheduledTask | Where-Object {$_.TaskName -like $taskName }
$action = New-ScheduledTaskAction -Execute 'C:1kioskkiosk_bat.bat'
$trigger = New-ScheduledTaskTrigger -AtLogOn -User "STAAR TEST"
$settings = New-ScheduledTaskSettingsSet –AllowStartIfOnBatteries –DontStopIfGoingOnBatteries
if($taskExists) {
Unregister-ScheduledTask -TaskName $taskName -Confirm:$false 
}
Register-ScheduledTask -Action $action -Trigger $trigger -TaskName $taskName -Description 
$Description -User $User

Set-ScheduledTask -TaskName $taskName -Settings $settings

使用此代码,我可以作为测试帐户登录,只要笔记本电脑插上电源,任务就会启动staar测试。如果笔记本电脑未插上电源则任务不起作用。但是,如果我在以管理员身份运行的PowerShell中手动输入命令,则如果笔记本电脑没有插上电源该任务就会起作用。这让我相信.bat文件需要另一个参数。

我使用任务调度器已经很多年了,所以我无法直接帮助您。但是,我仍然有我当时的工作剧本。我们需要每隔几分钟更新一次带有闸门计数器的网页,下面的脚本是我如何自动配置计划任务的。

去掉你不想要的所有东西,插入一些你想要的东西,你应该拥有它

.CMD文件

<# : BATCH Bootstrap for PowerShell
@ECHO OFF
SETLOCAL
PowerShell -executionpolicy remotesigned -Command "Invoke-Command -ScriptBlock ([scriptblock]::Create($([System.IO.File]::ReadAllText('%~f0')))) -ArgumentList ([string]'%*').split()"
ENDLOCAL
GOTO :EOF
#>
# https://blog.netnerds.net/2015/01/create-scheduled-task-or-scheduled-job-to-indefinitely-run-a-powershell-script-every-5-minutes/
# Change these three variables to whatever you want
$jobname = "Gate Count Web Updater"
$script =  "C:UsersPublicGateCountWebUpdater.cmd"
$repeat = (New-TimeSpan -Minutes 2)
# The script below will run as the specified user (you will be prompted for credentials)
# and is set to be elevated to use the highest privileges.
# In addition, the task will run every 5 minutes or however long specified in $repeat.
$action = New-ScheduledTaskAction –Execute $script
$duration = (New-TimeSpan -Days (365 * 20))
$trigger = New-ScheduledTaskTrigger -Once -At (Get-Date).Date -RepetitionInterval $repeat -RepetitionDuration $duration
$msg = "Enter the username and password that will run the task"; 
$credential = $Host.UI.PromptForCredential("Task username and password",$msg,"$env:userdomain$env:username",$env:userdomain)
$username = $credential.UserName
$password = $credential.GetNetworkCredential().Password
$settings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries -StartWhenAvailable -RunOnlyIfNetworkAvailable -DontStopOnIdleEnd
Register-ScheduledTask -TaskName $jobname -Action $action -Trigger $trigger -RunLevel Highest -User $username -Password $password -Settings $settings

最新更新