これで開始できます。資格情報とサービス名にオプションのパラメーターを使用します。資格情報を省略すると、それらの入力を求めるプロンプトが表示されます。サービス名を省略すると、デフォルトで tomcat* になり、そのフィルターに一致するすべてのサービスが返されます。検索の結果は、必要に応じて stop または start にパイプされます。
computername はパイプライン入力を受け入れるため、コンピューターの配列を渡すことができます。または、それらがファイル パイプに存在する場合は、そのファイルの内容をスクリプトに渡します。
例えば
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
}