2012-04-05 89 views
2

我編寫了一個腳本來獲取機器的服務,並且我想將StartMode鏡像到另一臺機器。我不能想到如何實現後者:在遠程機器上設置服務。下面有一個腳本,香港專業教育學院迄今寫的:powershell:在Windows 7機器上更改服務的啓動類型

#List of Issue of services 
$NamesOfIssueServices = "Browser", "Dhcp", "Dnscache", "dwmrcs", "iphlpsvc", "LanmanServer", "LanmanWorkstation", "MMCSS", "MpsSvc", "Netlogon", "Netman", "netprofm", "NlaSvc", "nsi", "p2pimsvc","PNRPsvc","PolicyAgent", "SessionEnv", "stisvc", "W32Time", "WinHttpAutoProxySvc", "WinRM" 

#get all services 
$W32Services = Get-WmiObject Win32_Service 

#filter wanted services 
$IssueServices = $W32Services | Where-Object {$NamesOfIssueServices -contains $_.name} 

#display wanted services 
$IssueServices | Sort-Object name | ft Name, DisplayName, State, StartMode, StartName 

回答

4

如果您使用的PowerShell 2.0,你可以輕鬆地做到這一點通過Set-Service的cmdlet:

foreach ($service in $issueServices) { 
    $startMode = $service.StartMode 

    if ($service.StartMode -eq "Auto") { 
     $startMode = "Automatic" 
    } 

    Set-Service -ComputerName TheRemoteMachine -Name $service.Name -StartupType $startMode 
} 

條件語句是由於StartMode屬性獲得的值"Auto"與參數-StartupType預期的參數"Automatic"之間的不兼容性

+0

+1因爲在win32_service和AUTOMATIC中看到了與set-service不同的地方! – 2012-04-05 11:53:15

+0

謝謝你的工作 – resolver101 2012-04-05 13:42:28

相關問題