PowerShell脚本在多台远程计算机上停止服务

时间:2015-02-20 09:29:34

标签: powershell remote-access

我正在尝试禁用在250多台PC上运行的服务。我想有一个PowerShell脚本,我可以在网络中的随机PC上执行,并让它在我在txt文件中指定的每台PC上禁用服务。它始终是相同的服务。该脚本还应该询问它尝试连接的PC的凭据。

这是在computer.txt中的每台PC上设置DNS的脚本。它要求我提供每台PC的“管理​​员”密码。

function Set-DNSWINS {
#Get NICS via WMI
$remoteuser = get-credential $_\administrator
$NICs = Get-WmiObject  -Class Win32_NetworkAdapterConfiguration -Credential $remoteuser -ComputerName $_ -Filter "IPEnabled=TRUE"
Get-WmiObject  -Class Win32_NetworkAdapterConfiguration -Credential $remoteuser -ComputerName $_ -Filter "IPEnabled=TRUE"

foreach($NIC in $NICs) {
$DNSServers = "192.168.3.12","192.168.0.77"
$NIC.SetDNSServerSearchOrder($DNSServers)
$NIC.SetDynamicDNSRegistration("TRUE")
#$NIC.SetWINSServer("12.345.67.890", "12.345.67.891")
}
}

function Get-FileName {
$computer = Read-Host "Dateiname mit Computernamen"
return $computer
}

Get-Content computer.txt | ForEach-Object {Set-DNSWINS}

2 个答案:

答案 0 :(得分:0)

您可以使用

从命令行停止服务
net stop "servicename"

或在PowerShell中

Stop-Service "serviceName"

可能有更好的方法可以在多台机器上自动执行此操作。

答案 1 :(得分:0)

可以使用Set-Service来禁用服务,使用Invoke-Command来远程运行它。请注意,您需要在远程计算机上运行Enable-PSRemoting并配置WSMAN以允许连接到远程PC:

function MyFunction{
    $remoteuser = get-credential $_\administrator
    $service = "MyService"
    Invoke-Command -computer $_ -credential $remoteuser -scriptblock {
        Stop-Service $service
        Set-Service $service -startuptype Disabled
    }
}

function Get-FileName {
$computer = Read-Host "Dateiname mit Computernamen"
return $computer
}

Get-Content computer.txt | ForEach-Object {MyFunction}