PowerShell 调用命令 Returns 空白数据?

PowerShell Invoke-Command Returns Blank Data?

一直在尝试解决这个问题,但似乎无法弄清楚。

我有以下脚本:

$Servers = Get-Content -Path "C:\Utilities_PowerShell\ServerList.txt"
$IISServiceName1 = 'W3SVC'
$IISServiceName2 = 'IISAdmin'
$IISServiceName3 = 'WAS'
$IISarrService = Get-Service -Name $IISServiceName1,$IISServiceName2,$IISServiceName3
$IISarrServiceCheck = Get-Service -Name $IISServiceName1,$IISServiceName2,$IISServiceName3 -ErrorAction SilentlyContinue -ErrorVariable NoService

function IISServiceStatus # Checks for status of IIS services
{
    param (
    $IISServiceName1,
    $IISServiceName2,
    $IISServiceName3,
    $IISarrService,
    $IISarrServiceCheck
    )

    if (Get-Service -Name $IISServiceName1,$IISServiceName2,$IISServiceName3)
    {
        Write-Host "Status of IIS service(s) on $env:ComputerName :"
        Get-Service -Name $IISServiceName1,$IISServiceName2,$IISServiceName3 | Select Name,DisplayName,Status | Format-Table -AutoSize
    }
    else
    {
        Write-Host "  No IIS service(s) were found..." -foreground "red"
    }
}

$Sessions = New-PSSession -ComputerName $Servers
$EndJobs = $Sessions | ForEach-Object {
    Invoke-Command -Session $_ -ScriptBlock ${function:IISServiceStatus} -AsJob -ArgumentList $IISServiceName1, $IISServiceName2, $IISServiceName3, $IISarrService, $IISarrServiceCheck | Wait-Job | Receive-Job
    Write-Host " "
}

每当我 运行 它时,我得到的只是输出:

  Status of IIS service(s) on *PC* :

如果我运行一个loop/invoke-command之外的函数,结果绝对完美。我的远程循环有什么问题?

我试过将变量放入函数中,我试过 运行没有参数列表的调用命令等

更新:2016 年 3 月 17 日

结果...如果我 运行 我的实际脚本原样,$EndJobs 的结果很奇怪,因为它在一个 table 中输出所有服务,然后是三个另一个 table 中的 IIS 服务。这可以解释为什么当我 运行 我的调用命令 (stopIIS) 脚本块时...我不得不重新启动整个服务器,因为它关闭了所有服务。

这些函数 运行 完美地通过 remote/invoke-command.运行。

这到底是怎么回事...invoke-command 真的把我的东西搞砸了!

任何人 ideas/tips 都知道我如何 运行 我的本地脚本(100% 有效)在一组服务器上从一个文本文件没有像这样的奇怪问题? invoke-command 是唯一的方法吗?

如果像这样将它们全部包装到脚本块中,您会遇到同样的问题吗?

$Servers = Get-Content 'C:\Utilities_PowerShell\ServerList.txt'

$Sessions = New-PSSession -ComputerName $Servers

$EndJobs = $Sessions | ForEach-Object {
    Invoke-Command -Session $_ -ScriptBlock {
        $IISServiceName1 = 'W3SVC'
        $IISServiceName2 = 'IISAdmin'
        $IISServiceName3 = 'WAS'
        $IISarrService = Get-Service -Name $IISServiceName1,$IISServiceName2,$IISServiceName3
        $IISarrServiceCheck = Get-Service -Name $IISServiceName1,$IISServiceName2,$IISServiceName3 -ErrorAction SilentlyContinue -ErrorVariable NoService

        function IISServiceStatus { # Checks for status of IIS services
            param (
                $IISServiceName1,
                $IISServiceName2,
                $IISServiceName3,
                $IISarrService,
                $IISarrServiceCheck
            )

            if (Get-Service -Name $IISServiceName1,$IISServiceName2,$IISServiceName3) {
                Write-Host "Status of IIS service(s) on $env:ComputerName :"
                Get-Service -Name $IISServiceName1,$IISServiceName2,$IISServiceName3 | Select Name,DisplayName,Status | Format-Table -AutoSize
            } else {
                Write-Host '  No IIS service(s) were found...' -ForegroundColor Red
            }
        }

        IISServiceStatus $IISServiceName1 $IISServiceName2 $IISServiceName3 $IISarrService $IISarrServiceCheck
    } -AsJob | Wait-Job | Receive-Job
    Write-Host ' '
}

$EndJobs

我遇到了类似的问题。我正在使用 credssp 来测试 2nd hop auth 的自动化,以干净地关闭生产环境。我的脚本有 3 个部分;会话设置、调用、会话拆卸。如果我 运行 每个部分分开,我得到输出。如果我 运行 整个脚本,我得到的空行与我分别 运行 时得到的输出量相匹配......我的调用没有什么特别的(反引号继续 - 我更喜欢 Python 的格式范式优于 Powershell/C#):

Invoke-Command                      `
    -Session        $workingSession `
    -ScriptBlock    {
        get-service *spool* -ComputerName server01
    }