下面是PowerShell脚本,第一个if语句是让脚本以管理员权限运行,剩下的三句话分别是停止程序、重启服务、启动程序,完全按照楼主的需求。把下面脚本另存为一个.ps1文件,然后右键点击选择“Run with PowerShell(以PowerShell运行)”即可。
[PowerShell] 纯文本查看 复制代码 if($script:MyInvocation.MyCommand.Path -ne $null) {
$current_dir = Split-Path -Path $script:MyInvocation.MyCommand.Path -Parent;
$current_script = Split-Path -Path $script:MyInvocation.MyCommand.Path -Leaf;
if(!([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) {
# NoExit makes keeps the window from closing. If you don't need it you can remove it.
Start-Process powershell.exe -Verb RunAs -ArgumentList "-NoProfile", "-NoExit", "-ExecutionPolicy", "Bypass", "-Command", "cd `"'$current_dir'`"; &`"'.\$current_script'`""
exit
}
}
ps | ?{$_.path -eq "D:\Expressway\Projectsgb\Expway.exe"} | kill -force
Get-WmiObject win32_service | ?{$_.PathName -like "*D:\MySQL\MySQL Server 5.5\bin\MySQLd"} | select Name | Restart-Service -Force
&"D:\Expressway\Projectsgb\Expway.exe" |