远程机器
上停止或启动服务我创建了一个脚本,将启动或停止基于它的显示名称的服务。我的脚本在本地机器上工作,但我想确保它可以在远程机器和本地机器上完成。我不知道如何让它在远程机器上工作。远程机器
任何帮助,将不胜感激。
$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"
}
假设你没有禁用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
不带参数在本地执行,或者包括远程服务器的名称来执行服务器上的行动。
Start-Service
和Stop-Service
对远程计算机不起作用。您将需要执行PowerShell远程处理,或者使用WMI。在我的环境中,PowerShell远程处理默认被阻止,但我们使用WMI;通过Get-WMIObject
检索服务对象已称为Start-Service()
其可以检索到的服务对象调用一方法:
(Get-WmiObject -ComputerName $ComputerName -Class Win32_Service -Filter "Name='$ServiceName'").StartService()
停止使用WMI的工作方式相同的远程计算机上的服务;您可以调用服务对象的方法。
我建议你阅读Get-Help Get-WMIObject
和the MSDN reference on the Win32_Service class的信息。
ETA:应该指出的是,通过省略-ComputerName
参数,WMI会在本地计算机上正常工作。
您可以使用Start-Service
和Stop-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"
}
这是能够执行服务启动/远程服务器上停止功能的一个很好的建议,但它确实没有回答如何使它发生在本地和远程原来的问题。还是很好的信息! WMI功能非常强大,并且使用不足的IMO。 – TheMadTechnician
可以使用'启动Service'和'别-Service'针对远程计算机,你只需要一个服务对象从传递给他们'GET-Service' –
@TheMadTechnician - 请参阅我的其他编辑;通过简单地将'-ComputerName'参数省略到'Get-WMIObject',WMI方法在本地计算机上工作。 –