2017-04-14 117 views
0

上停止或啓動服務我創建了一個腳本,將啓動或停止基於它的顯示名稱的服務。我的腳本在本地機器上工作,但我想確保它可以在遠程機器和本地機器上完成。我不知道如何讓它在遠程機器上工作。遠程機器

任何幫助,將不勝感激。

$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"    
} 

回答

1

假設你沒有禁用PowerShell遠程處理,這樣做是ComputerName將其包裝在一個函數作爲可選參數的最簡單的方法,然後用Invoke-Command和圖示PSBoundParameters

Function Toggle-Service{ 
[cmdletbinding()] 
Param([string[]]$ComputerName) 
$serviceName = Read-Host -Prompt 'Please enter service name: ' 

# Check that service name exists 
If (Invoke-Command -ScriptBlock {Get-Service $serviceName -ErrorAction SilentlyContinue} @PSBoundParameters) 
{ 

# 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') { 

Invoke-Command -ScriptBlock {Start-Service -displayname $serviceName} @PSBoundParameters 

Write-Host $serviceName "Starting..." -ForegroundColor Green 
} 

#Stop service 
If ($Choice -eq 'stop') { 

    Invoke-Command -ScriptBlock {Stop-Service -displayname $serviceName} @PSBoundParameters 

    Write-Host $serviceName "Stopping..." -ForegroundColor Green 
} 
} 
    } 
else {    
    Write-Host "Service name does not exist"    
} 
} 

然後就可以調用Toggle-Service不帶參數在本地執行,或者包括遠程服務器的名稱來執行服務器上的行動。

0

Start-ServiceStop-Service對遠程計算機不起作用。您將需要執行PowerShell遠程處理,或者使用WMI。在我的環境中,PowerShell遠程處理默認被阻止,但我們使用WMI;通過Get-WMIObject檢索服務對象已稱爲Start-Service()其可以檢索到的服務對象調用一方法:

(Get-WmiObject -ComputerName $ComputerName -Class Win32_Service -Filter "Name='$ServiceName'").StartService() 

停止使用WMI的工作方式相同的遠程計算機上的服務;您可以調用服務對象的​​方法。

我建議你閱讀Get-Help Get-WMIObjectthe MSDN reference on the Win32_Service class的信息。

ETA:應該指出的是,通過省略-ComputerName參數,WMI會在本地計算機上正常工作。

+0

這是能夠執行服務啓動/遠程服務器上停止功能的一個很好的建議,但它確實沒有回答如何使它發生在本地和遠程原來的問題。還是很好的信息! WMI功能非常強大,並且使用不足的IMO。 – TheMadTechnician

+0

可以使用'啓動Service'和'別-Service'針對遠程計算機,你只需要一個服務對象從傳遞給他們'GET-Service' –

+0

@TheMadTechnician - 請參閱我的其他編輯;通過簡單地將'-ComputerName'參數省略到'Get-WMIObject',WMI方法在本地計算機上工作。 –

2

您可以使用Start-ServiceStop-Service遠程計算機,你就必須從Get-Service使用ComputerName參數這樣的服務對象傳遞給他們...可以針對遠程計算機上運行:

Get-Service $ServiceName -ComputerName $ComputerName | Start-Service 

我覺得這是比使用PowerShell遠程或WMI命令容易得多。

您可以輕鬆地更新你的代碼中加入一行以此來提示輸入遠程計算機的名稱,然後更新(只有三個行的新代碼)開始/停止服務的命令了。

$serviceName = Read-Host -Prompt 'Please enter service name: ' 

#get computername or use localhost for local computer 
if(($ComputerName = Read-Host "Enter Computer Name, leave blank for local computer") -eq ''){$ComputerName = "localhost"} 

# Check that service name exists 
If (Get-Service -DisplayName $serviceName -ComputerName $ComputerName -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') { 
      Get-Service -DisplayName $serviceName -ComputerName $ComputerName | Start-Service 
      Write-Host $serviceName "Starting..." -ForegroundColor Green 
     } 

     #Stop service 
     If ($Choice -eq 'stop') { 
      Get-Service -DisplayName $serviceName -ComputerName $ComputerName | Stop-Service 
      Write-Host $serviceName "Stopping..." -ForegroundColor Green 
     } 
    } 
} 
else {    
    Write-Host "Service name does not exist"    
}