powershell 示例(探测端口并重启程序)

###restartAPP.ps1###

$log="D:\xxxx\xxxx\xxxx.log.txt"
$execFile="C:\Program Files (x86)\xxxx\xxxx\xxxx.exe"
$Ipaddress='10.x.x.x'
$Port=80

# 探测端口
$t = New-Object Net.Sockets.TcpClient
try
{
$t.Connect($Ipaddress,$Port)
}
catch{}

# 如果可以连接端口
if($t.Connected)
{
add-content "$log" "$(Get-Date): Port $Port is operational."
}
# 否则
else
{
# 连接断开,需要重连.
add-content "$log" "$(Get-Date): Port $Port is closed."
# 注销除管理员外的登录用户
$SERVER="$(hostname)"
try {
query user /server:$SERVER 2>&1 | select -skip 1 | Select-String "administrator" -notMatch | foreach {
logoff ($_ -split "\s+")[-6] /server:$SERVER
}
}
catch {}
# 重启程序
stop-process -name xxxx -force
Start-Sleep -Seconds 10
Start-Process -FilePath "$execFile"
add-content "$log" "$(Get-Date): xxxx has been restarted."
}

 

运行:powershell -ExecutionPolicy Bypass -File restartAPP.ps1

 

设置计划任务:如果提示“已有一个实例在运行”,可以考虑修改运行方式为“允许多实例运行”。

运行时指定要运行的程序是powershell,参数是-ExecutionPolicy Bypass -File restartAPP.ps1

 

转载于:https://www.cnblogs.com/memoryredbook/p/7063358.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值