
[英]issue with scheduling a powershell script in task scheduler windows 2008 server
[英]Issue with start an application from the windows task scheduler using Powershell script in a Windows 2016 server
我必须使用不同的服务帐户用户凭据启动一个应用程序,这项工作是通过在 Windows 任务调度程序上创建的作业完成的,该作业调用 powershell 脚本以作为服务帐户用户打开应用程序。 问题是当这个脚本从任务调度程序执行时,应用程序永远不会启动; 当我单独运行它时,相同的脚本可以完美运行。
下面的代码将任务设置到任务调度程序中。
$scriptPath = "C:\temp\SetupTask.ps1"
$action= New-ScheduledTaskAction -Execute "PowerShell.exe" -Argument "PowerShell.exe -executionpolicy bypass -noprofile -file $scriptPath 2>&1 > c:\\temp\\Setuptask.txt"
$principal = New-ScheduledTaskPrincipal -UserID "NT AUTHORITY\SYSTEM" -LogonType ServiceAccount -RunLevel Highest
$trigger = New-JobTrigger -AtStartup -RandomDelay 00:00:15
$settings = New-ScheduledTaskSettingsSet -MultipleInstances Queue
Register-ScheduledTask -TaskName "RunSetup" -Principal $principal -TaskPath "\test\setup" -Trigger $trigger -Action $action -Force -Settings $settings
下面是任务调度程序执行的代码 - SetupTask.ps1。
$timestamp = Get-Date
Write-Host "Setup Script Start at $timestamp"
Write-Host "Running Xapp"
& "C:\temp\RunXAppbyTask.ps1"
Unregister-ScheduledTask -TaskPath "test\setup" -TaskName "RunSetup" -Confirm:$false
$timestamp = Get-Date
Write-Host "Setup Script End at $timestamp"
下面是 runXAppbyTask.ps1 的代码
$username = "XXX-DDD" + "\service_account_user"
$password = "YYYYYY"
$credentialsObject = New-Object System.Management.Automation.PSCredential -ArgumentList @($username, (ConvertTo-SecureString -String $password -AsPlainText -Force))
$XAPPExeName = "XAPP.Client.DesktopUI"
$XAPPPath = "C:\Program Files\XAPP\XAPPClient\" + $XAPPExeName + ".exe"
try {
$timestamp = Get-Date
Write-Host "Starting XAPP Application as $username at $timestamp"
Start-Process -FilePath $XAPPPath -Credential ($credentialsObject) #The aplication doesn't start up, this same script works fine when ran separately.
Start-Sleep -s 60
$timestamp = Get-Date
Write-Host "Started XAPP Application as $username at $timestamp"
try {
$Process = Get-Process $XAPPExeName # Always fail to get the process since the process don't get started when called from the task scheduler
Write-Host $Process.Id "-" $Process.Name
if (!($Process.HasExited)) {
Stop-Process -Name $Process.Name -Force
}
}
catch {
write-host "Error Closing the XAPP Application."
}
}
catch {
write-host "Error Starting the XAPP Application."
}
我确实浏览了以下链接,这些链接在我看来提出了类似的问题,但我找不到解决此问题的方法。 请注意没有错误抛出脚本只是成功运行但应用程序永远不会启动。 这里的任何帮助将不胜感激。 提前致谢。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.