我有这个脚本,旨在每次用户打开计算机并登录时自动以管理员模式运行 exe 文件。
# Get the directory of the script
$scriptPath = Split-Path -Parent -Path $MyInvocation.MyCommand.Definition
# Define the path to the exe
$exePath = Join-Path -Path $scriptPath -ChildPath "MyExeFile.exe"
# Check if the exe exists
if (-not (Test-Path $exePath)) {
Write-Error "MyExeFile.exe not found in the script directory."
exit 1
}
# Name for the scheduled task
$taskName = "Run Exe File on Login"
# Create a new scheduled task action
$action = New-ScheduledTaskAction -Execute $exePath
# Create trigger to run at system startup
$trigger = New-ScheduledTaskTrigger -AtStartup
# Set up principal to run with highest privileges
$principal = New-ScheduledTaskPrincipal -UserId "SYSTEM" -LogonType ServiceAccount -RunLevel Highest
# Create the scheduled task
$task = New-ScheduledTask -Action $action -Trigger $trigger -Principal $principal
# Register the scheduled task
Register-ScheduledTask -TaskName $taskName -InputObject $task -Force
# Run the task immediately
Start-ScheduledTask -TaskName $taskName
当我以管理员身份运行脚本时,它运行并且没有出现问题,但是当我重新启动计算机然后登录时,exe文件根本不运行。
这是我第一次使用powershell编写脚本,如果有专家能看一下并指出问题或给我一些找到问题的建议,我将非常感激。
检查了计划任务 Powershell 脚本 - 作为用户帐户运行正常,但不能作为 SYSTEM 运行。我发现:脚本确实运行了,但是在
Task Scheduler Library
中它设置为Queue
,所以我需要让它在Task Scheduler Library
中手动运行。
但即便如此,当我尝试重新启动计算机时,exe文件仍然没有被执行。
当我检查该操作时,这似乎是正确的,因为我自己在 cmd 中运行了该操作,并且 exe 文件按预期运行。
动作如下:
在历史记录中,也有记录表明,该任务确实在启动的计算机上运行起来:
所以我现在怀疑可能是脚本错误地读取了操作,因此无法运行exe文件?
所以我已经解决了这个问题,以防万一有人在未来几年遇到同样的事情。脚本确实运行,但任务管理器中仅运行 exe 文件,但程序未打开。原因是因为我在
$principal = New-ScheduledTaskPrincipal -UserId "SYSTEM" -LogonType ServiceAccount -RunLevel Highest
行中将其作为 SYSTEM 运行,在将 "SYSTEM"
更改为 $principal = New-ScheduledTaskPrincipal -UserId $currentUser -LogonType Interactive -RunLevel Highest
后,脚本按我想要的方式运行。
此外,我的脚本中的任务将被设置为任务库中的队列,我已经对其进行了调整,因此它将在运行时立即运行:
# Get the directory of the script
$scriptPath = Split-Path -Parent -Path $MyInvocation.MyCommand.Definition
# Define the path to the exe
$exePath = Join-Path -Path $scriptPath -ChildPath "Ingeteam.FX.ServiceMonitor.exe"
# Check if the exe exists
if (-not (Test-Path $exePath)) {
Write-Error "Ingeteam.FX.ServiceMonitor.exe not found in the script directory."
exit 1
}
# Create a batch file to run the exe
$batchPath = Join-Path -Path $scriptPath -ChildPath "RunServiceMonitor.bat"
@"
@echo off
cd /d "%~dp0"
start "" "$exePath"
"@ | Out-File -FilePath $batchPath -Encoding ASCII
# Name for the scheduled task
$taskName = "Run Ingeteam Service Monitor"
# Create a new scheduled task action
$action = New-ScheduledTaskAction -Execute $batchPath
# Create trigger to run at user logon
$trigger = New-ScheduledTaskTrigger -AtLogOn
# Set up principal to run as the current user with highest privileges
$currentUser = [System.Security.Principal.WindowsIdentity]::GetCurrent().Name
$principal = New-ScheduledTaskPrincipal -UserId $currentUser -LogonType Interactive -RunLevel Highest
# Create the scheduled task
$task = New-ScheduledTask -Action $action -Trigger $trigger -Principal $principal
# Register the scheduled task
try {
Register-ScheduledTask -TaskName $taskName -InputObject $task -Force
Write-Host "Scheduled task '$taskName' registered successfully."
}
catch {
Write-Error "Failed to register scheduled task: $_"
exit 1
}
# Run the task immediately
try {
Start-ScheduledTask -TaskName $taskName
Write-Host "Scheduled task '$taskName' started successfully."
}
catch {
Write-Error "Failed to start scheduled task: $_"
}
# Wait for a moment to allow the task to start
Start-Sleep -Seconds 10
# Check if the process is running
$process = Get-Process -Name "Ingeteam.FX.ServiceMonitor" -ErrorAction SilentlyContinue
if ($process) {
Write-Host "Ingeteam.FX.ServiceMonitor is now running with PID: $($process.Id)"
}
else {
Write-Warning "Ingeteam.FX.ServiceMonitor does not appear to be running. Please check the Task Scheduler for more information."
}
# Display the task status
$taskInfo = Get-ScheduledTask -TaskName $taskName
Write-Host "Current task status: $($taskInfo.State)"
# If the task is still queued, try to run it again
if ($taskInfo.State -eq "Queued") {
Write-Host "Task is queued. Attempting to run it again..."
Start-ScheduledTask -TaskName $taskName
Start-Sleep -Seconds 10
$taskInfo = Get-ScheduledTask -TaskName $taskName
Write-Host "Updated task status: $($taskInfo.State)"
}