上停止或啓動服務我創建了一個腳本,將啓動或停止基於它的顯示名稱的服務。我的腳本在本地機器上工作,但我想確保它可以在遠程機器和本地機器上完成。我不知道如何讓它在遠程機器上工作。遠程機器
任何幫助,將不勝感激。
$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方法在本地計算機上工作。 –