Powershell 脚本无法 运行 正确使用任务计划程序

Powershell Script Can't Run Properly With Task Scheduler

我有一个奇怪的问题。我有一个脚本在手动 运行 时可以正常工作。我在 windows 和 运行 中自动创建了一个计划作业。该脚本在脚本的最后阶段之前工作正常。

$deploymentfiles_mdm = Get-ChildItem 'D:\DeploymentTriggerApp\*'
Write-Host $deploymentfiles_mdm
$timestamp_app = Get-Date -Format o | ForEach-Object {$_ -replace ":", "."}
Write-Host $timestamp_app
$server = Get-Content 'C:\Users\Administrator\Desktop\Scripts\AutoDeployment\ProdMDMapps.txt'
$User = 'domain\user'
$SecurePassword = Get-Content C:\Users\Administrator\Desktop\Scripts\Password.txt | ConvertTo-SecureString
$UserCred = New-Object System.Management.Automation.PSCredential ($User, $SecurePassword)

if (Test-Path -Path $deploymentfiles_mdm)
{

        do{
            try
            {
                $ServerSessions = New-PSSession -ComputerName $server -Credential $UserCred -ErrorAction Stop
                Write-Host ("$ServerSessions")
            }
            catch [Exception]
            {
                Write-Host("Credential is incorrect or password is expired. Either change credential and run CredentialEncryption.ps1 or communicate with dc admin to open expired password!")
            }

        }while(!$ServerSessions)

        Copy-Item "D:\Deployment_Files\*.zip" -ToSession $ServerSessions -Destination "D:\Deployment_Files\" -ErrorAction SilentlyContinue

        try{
        
            Invoke-Command -Session $ServerSessions -ScriptBlock {
                param($timestampApp)
                $appPath = Get-ChildItem 'D:\MDM\live\bin\'
                Expand-Archive -Path 'D:\Deployment_Files\*.zip' -DestinationPath 'D:\Deployment_Files\' -Force
                Remove-Item -Path 'D:\Deployment_Files\*.zip'

                $nodeProcess = Get-Process | Where-Object { $_.Name -eq "node"}
                if($nodeProcess -Or $appPath)
                {
                    Get-Process | Where-Object { $_.Name -eq "node"} | Select-Object -First 1 | Stop-Process -Force
                    
                    New-Item -Path 'D:\Backups\' -Name $timestampApp -ItemType 'directory'
                    Get-ChildItem -Path "D:\MDM\live\bin\" -Recurse | Move-Item -Destination "D:\Backups$timestampApp\"
                }
                
                Copy-Item "D:\Deployment_Files\bin" -Destination "D:\MDM\live\" -Recurse -Force -ErrorAction SilentlyContinue

                Remove-Item "D:\Deployment_Files\*" -Recurse -Force

                Start-Job -ScriptBlock{ node D:\MDM\live\bin\main.js}
        } -ArgumentList $timestamp_app
        }
        catch
        {
            $_.Exception.Message
        }   
    
}

Remove-Item D:\DeploymentTriggerApp\*

Start-Job -ScriptBlock{ node D:\MDM\live\bin\main.js} 部分,脚本无法启动节点进程。当我手动 运行 它时,它 运行 没有任何问题。

有什么建议吗? (节点进程需要在后台作业。如果有任何替代命令,我也可以尝试该解决方案)

下面一行解决了我的问题。

Start-Job -ScriptBlock{& 'C:\Program Files\nodejs\node.exe' D:\MDM\live\bin\main.js}