如何解决作为脚本的一部分执行时,启动作业不起作用
我有一个PowerShell脚本,旨在在一定时间内连续运行Test-NetConnection。
该脚本在通过PSE执行时或在PowerShell中手动运行作业时效果很好,但是当我通过批处理文件执行命令时,或者如果我关闭PowerShell窗口,则该作业仍将继续进行,脚本结束了。 .. 难道我做错了什么?如果某件事情没有在后台运行,那么将其作为后台作业有什么意义呢?
编辑:我尝试将InvokeCommand与-computername和-asjob标志一起使用,但是它仍然无法正常工作,需要我启用psremoting。我想通过执行引用该批处理文件的批处理文件来运行以下脚本,然后能够让PS窗口关闭,这样作业就不会因注销或其他用户可能关闭的情况而中断
$TimetoRun = Read-Host -Prompt 'Input how long you want the script to run in hours'
$TimeStart = Get-Date
$TimeEnd = $timeStart.AddSeconds($TimetoRun)
$Destination = Read-Host -Prompt 'Input the Hostname or IP you want to test'
$Port = Read-Host -Prompt 'Input the port you want the script to test'
$Interval = Read-Host -Prompt 'Input the interval you want to test connection in seconds'
$scriptDir = Split-Path -Path $MyInvocation.MyCommand.Definition -Parent
Start-Job -ScriptBlock {
param($scriptDir,$TimeStart,$TimeEnd,$Destination,$Port,$Interval)
Start-Transcript -Path "C:\Users\npayne\Desktop\TestNetConnection\Connectivity.log"
Write-Host "Start Time: $TimeStart"
write-host "End Time: $TimeEnd"
Write-Host "Destination: $Destination"
Write-Host "Port: $Port"
Write-Host "Interval = $Interval seconds"
Do {
$TimeNow = Get-Date
if ($TimeNow -ge $TimeEnd) {
Write-host "Net Connection Test Completed"
} else {
$Test = "$(Get-Date) - $(Test-NetConnection -Port $Port $Destination -InformationLevel Quiet)"
Write-Host $Test
}
Start-Sleep -Seconds $Interval
}
Until ($TimeNow -ge $TimeEnd)
Stop-Transcript
} -ArgumentList $scriptDir,$Interval
解决方法
使用Invoke-*(远程计算机上的运行代码),PSremoting是设计上的要求(作为远程主机上的管理员),除了将脚本设置为登录外,没有其他解决方法脚本或远程目标上的计划任务。
在PSE中,PowerShell将根据需要自动按顺序自动加载/解析/处理代码。在控制台主机中,必须确保添加了这些资源,然后再使用它们,即,如果有正在使用的表单代码,则为.Net表单名称空间。
只有少数几个命令在未启用PowerShell Remoting的情况下可以运行。 Invoke- *不属于这些。
参考:
•提示:使用Windows PowerShell远程工作,而无需使用Remoting或 WinRM https://technet.microsoft.com/en-us/library/ff699046.aspx
Some cmdlets have a –ComputerName parameter that lets you work with a remote computer without using Windows PowerShell remoting. This means you can use the cmdlet on any computer that is running Windows PowerShell,even if the computer is not configured for Windows PowerShell remoting. These cmdlets include the following: • Get-WinEvent • Get-Counter • Get-EventLog • Clear-EventLog • Write-EventLog • Limit-EventLog • Show-EventLog • New-EventLog • Remove-EventLog • Get-WmiObject • Get-Process • Get-Service • Set-Service • Get-HotFix • Restart-Computer • Stop-Computer • Add-Computer • Remove-Computer • Rename-Computer • Reset-ComputerMachinePassword
对于PowerShell作业。您需要为您的用例使用正确的方法。有几种类型(这不会消除启用PSRemoting以便在远程主机上运行代码的需要):
TABLE
Job Type Description
-------- -----------
BackgroundJob Started by using the Start-Job cmdlet.
RemoteJob Started by using the AsJob parameter of the Invoke-Command cmdlet.
PSWorkflowJob Started by using the AsJob parameter of a workflow.
PSScheduledJob An instance of a scheduled job started by a job trigger.
CIMJob Started by using the AsJob parameter of a cmdlet from a CDXML module.
WMIJob Started by using the AsJob parameter of a cmdlet from a WMI module.
PSEventJob Created by running Register-ObjectEvent and specifying an action with the Action parameter.
查看其他详细信息
about_Jobs - PowerShell | Microsoft Docs
about_Job_Details - PowerShell | Microsoft Docs
about_Scheduled_Jobs - PowerShell | Microsoft Docs
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。