我正在尝试验证服务器名称的.txt列表中是否存在特定服务,然后输出到文件中。在这种情况下,我还需要添加凭据,因此我需要使用Invoke命令。我在这里做错了什么?
clear
start-transcript -path .Log.txt
$servers = Get-Content .ResourcesLab.txt
$cred = get-credential domainUsername
$name = Read-Host -Prompt 'Input your service name'
function Getinfo() {foreach($server in $servers)
{
Get-Service | Where-Object { $_.Name -eq $name}-and {$_.Status -eq "Running"}| Format-Table -AutoSize
}
}
Invoke-Command -credential $cred -ComputerName $servers -ScriptBlock ${function:Getinfo}
Stop-Transcript
我想你是这个意思吧?
$servers = Get-Content .ResourcesLab.txt
$cred = Get-Credential domainUsername
$name = Read-Host -Prompt 'Input your service name'
Invoke-Command -Credential $cred -ComputerName $servers -ScriptBlock {
param($name)
Get-Service | Where-Object { $_.Name -eq $name -and $_.Status -eq "Running"}
} -ArgumentList $name | Format-Table -AutoSize