上停止或启动服务我创建了一个脚本,将启动或停止基于它的显示名称的服务。我的脚本在本地机器上工作,但我想确保它可以在远程机器和本地机器上完成。我不知道如何让它在远程机器上工作。远程机器
任何帮助,将不胜感激。
$serviceName = Read-Host -Prompt 'Please enter service name: '
# Check that service name exists
If (Get-Service $serviceName -ErrorAction SilentlyContinue)
{
# Check that service name is not empty
if([string]::IsNullOrEmpty($serviceName))
{
Write-Host "Service name is NULL or EMPTY"
}
else
{
$Choice = Read-Host -Prompt 'Would you like to start or stop the service'
#Start service
If ($Choice -eq 'start') {
Start-Service -displayname $serviceName
Write-Host $serviceName "Starting..." -ForegroundColor Green
}
#Stop service
If ($Choice -eq 'stop') {
Stop-Service -displayname $serviceName
Write-Host $serviceName "Stopping..." -ForegroundColor Green
}
}
}
else {
Write-Host "Service name does not exist"
}
这是能够执行服务启动/远程服务器上停止功能的一个很好的建议,但它确实没有回答如何使它发生在本地和远程原来的问题。还是很好的信息! WMI功能非常强大,并且使用不足的IMO。 – TheMadTechnician
可以使用'启动Service'和'别-Service'针对远程计算机,你只需要一个服务对象从传递给他们'GET-Service' –
@TheMadTechnician - 请参阅我的其他编辑;通过简单地将'-ComputerName'参数省略到'Get-WMIObject',WMI方法在本地计算机上工作。 –