这应该可以帮助您入门,它使用可选参数作为凭据和服务名称,如果您省略凭据,它将提示输入它们。如果您省略服务名称,它将默认为 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 Domain\username
}
}
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
}