【问题标题】:PowerShell Invoke-Command Returns Blank Data?PowerShell 调用命令返回空白数据?
【发布时间】:2016-07-02 19:06:00
【问题描述】:

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

我有以下脚本:

$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* :

如果我在循环/调用命令之外运行该函数,结果绝对完美。我的远程循环有什么问题?

我尝试将变量放入函数中,我尝试在没有参数列表的情况下运行调用命令等。

更新:2016 年 3 月 17 日

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

这些函数在不通过远程/调用命令运行时完美运行。

这到底是什么...invoke-command 严重搞砸了我的东西!

关于如何在一组服务器上从一个文本文件运行我的本地脚本(100% 有效)而不会出现类似这样的奇怪问题,任何人有任何想法/提示吗?调用命令是唯一的方法吗?

【问题讨论】:

  • 调用代码后$EndJobs 的值是多少?
  • 如果我在运行代码后输出$EndJobs,则该值是正确的,@PetSerAl。正如我在下面所说,我有一堆使用相同的调用命令方法调用的其他函数,它们运行良好......唯一没有的是这个。我不明白为什么这个不会像所有其他人一样输出结果。
  • 实际上,@PetSerAl,我的立场是正确的。如果我按原样运行我的实际脚本,$EndJobs 的结果很奇怪,因为它在一个表中输出所有服务,然后在另一个表中输出三个 IIS 服务。这可以解释为什么当我运行我的调用命令(stopIIS)脚本时,我必须重新启动整个服务器,因为它关闭了所有服务。这些功能在不通过远程/调用命令运行时完美运行。到底是什么...invoke-command 严重搞砸了我的东西!

标签: powershell invoke-command


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

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

    $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
    

    【讨论】:

    • 这行得通,@Anthony Stringer。你能解释一下为什么吗?基本上,我在脚本中的不同时间调用这个函数(我的脚本实际上非常庞大,这是它的 1% sn-p)......我的其他函数,用简单的调用命令 $(function :name) 命令运行没有问题...只是这个特定的 IISStatus 一个...
    • 我希望可以,但我不熟悉${function:IISServiceStatus}。也许其他人可以解释这个问题。
    • 仍然很感激...最坏的情况我会使用它,尽管它不一定是一个干净的解决方案。感谢您抽出宝贵时间! @Anthony Stringer。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-15
    • 2016-09-09
    • 2020-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多