作为脚本的一部分执行时,启动作业不起作用

如何解决作为脚本的一部分执行时,启动作业不起作用

我有一个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

about_Remote_Disconnected_Sessions - PowerShell ...

Using Windows PowerShell Jobs | Scripting Blog

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 <select id="xxx"> SELECT di.id, di.name, di.work_type, di.updated... <where> <if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 <property name="dynamic.classpath" value="tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams['font.sans-serif'] = ['SimHei'] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -> systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping("/hires") public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate<String
使用vite构建项目报错 C:\Users\ychen\work>npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-