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



我有一个奇怪的问题。我有一个脚本,工作正常时手动运行它。我在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:UsersAdministratorDesktopScriptsAutoDeploymentProdMDMapps.txt'
$User = 'domainuser'
$SecurePassword = Get-Content C:UsersAdministratorDesktopScriptsPassword.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:MDMlivebin'
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:MDMlivebin" -Recurse | Move-Item -Destination "D:Backups$timestampApp"
}

Copy-Item "D:Deployment_Filesbin" -Destination "D:MDMlive" -Recurse -Force -ErrorAction SilentlyContinue
Remove-Item "D:Deployment_Files*" -Recurse -Force
Start-Job -ScriptBlock{ node D:MDMlivebinmain.js}
} -ArgumentList $timestamp_app
}
catch
{
$_.Exception.Message
}   

}
Remove-Item D:DeploymentTriggerApp*

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

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

下一行解决了我的问题。

Start-Job -ScriptBlock{& 'C:Program Filesnodejsnode.exe' D:MDMlivebinmain.js}

相关内容

  • 没有找到相关文章