如何使用PowerShell启动/停止远程服务器上的服务-Windows 2008提示输入凭据(&A)? [英] How to start/stop a service on a remote server using PowerShell - Windows 2008 & prompt for credentials?
本文介绍了如何使用PowerShell启动/停止远程服务器上的服务-Windows 2008提示输入凭据(&A)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试创建一个PowerShell脚本,该脚本将启动/停止远程计算机上的服务,但会提示用户输入所有值。我知道要使用的帐户;我只需要提示用户输入密码。
这是针对Tomcat个实例的。问题是Tomcat服务在不同服务器(tomcat6、tomcat7)上的名称并不总是相同。我需要能够存储加密的密码并提示停止或开始。这就是我到目前为止所拥有的。有什么想法吗?
我不确定是否将-AsSecureString
放在正确的位置。
# Prompt for user credentials
$credential=get-credential -AsSecureString -credential Domainusername
# Prompt for server name
$server = READ-HOST "Enter Server Name"
# Prompt for service name
$Service = READ-HOST "Enter Service Name"
gwmi win32_service -computername $server -filter "name='$service'" -Credential'
$cred.stop-service
推荐答案
您应该可以开始使用它,它使用可选的凭据和服务名称参数,如果您省略了凭据,它将提示您输入凭据。如果省略服务名称,它将默认为tomcat*,这将返回与该筛选器匹配的所有服务。然后根据需要将搜索结果传送到停止或启动。
当计算机名接受管道输入时,您可以传入一组计算机,或者如果它们存在于文件管道中,则将该文件的内容传递到脚本中。
例如
Get-Content computers.txt | <scriptname.ps1> -Control Stop
希望能有所帮助...
[cmdletBinding(SupportsShouldProcess=$true,ConfirmImpact="High")]
param
(
[parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]
[string]$ComputerName,
[parameter(Mandatory=$false)]
[string]$ServiceName = "tomcat*",
[parameter(Mandatory=$false)]
[System.Management.Automation.PSCredential]$Credential,
[parameter(Mandatory=$false)]
[ValidateSet("Start", "Stop")]
[string]$Control = "Start"
)
begin
{
if (!($Credential))
{
#prompt for user credential
$Credential = get-credential -credential Domainusername
}
}
process
{
$scriptblock = {
param ( $ServiceName, $Control )
$Services = Get-Service -Name $ServiceName
if ($Services)
{
switch ($Control) {
"Start" { $Services | Start-Service }
"Stop" { $Services | Stop-Service }
}
}
else
{
write-error "No service found!"
}
}
Invoke-Command -ComputerName $computerName -Credential $credential -ScriptBlock $scriptBlock -ArgumentList $ServiceName, $Control
}
这篇关于如何使用PowerShell启动/停止远程服务器上的服务-Windows 2008提示输入凭据(&A)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文