【问题标题】:PowerShell Remoting Collecting Performance CountersPowerShell 远程处理收集性能计数器
【发布时间】:2017-05-18 13:41:00
【问题描述】:

我已经从下面的链接下载了 PowerShell 脚本。

https://gallery.technet.microsoft.com/scriptcenter/PowerShell-Perfmon-0f013da8

我能够在本地机器上成功运行此脚本,但在远程机器上遇到问题。我更新了下面的代码

(Get-Counter -ComputerName $ComputerName -Counter (Convert-HString -HString $Counter) -SampleInterval 2 -MaxSamples 10).counterSamples

跟随。

(Invoke-Command -ComputerName $ComputerName -ScriptBlock {Get-Counter -Counter (Convert-HString -HString $Counter) -SampleInterval 2 -MaxSamples 10}).counterSamples

现在我遇到了错误。

The term 'Convert-HString' is not recognized as the name of a cmdlet, function, script file, or operable program.

【问题讨论】:

    标签: powershell remoting


    【解决方案1】:

    该函数在您尝试运行它的远程计算机上不存在。您需要在调用它之前将完整的函数粘贴到您的脚本块中,以便在尝试运行它时加载它。使用 Invoke-Command / 任何涉及另一台机器上的 PSSession 的东西,您都在该机器的上下文中运行。如果你在本地机器上加载一个函数/模块/变量,它只存在于你的本地机器上。

    编辑:已更新以允许在本地计算机上设置 $Counter,然后通过使用 Invoke-Command-ArgumentList 参数和参数化脚本块将其传递到 -ScriptBlock

    例子:

    (Invoke-Command -ComputerName $ComputerName -ScriptBlock {
        Param
        (
        [parameter(Mandatory=$false,Position=0)]
        [String]
        $Counter
        )
        function Global:Convert-HString {      
            [CmdletBinding()]            
            Param             
            (
                [Parameter(Mandatory = $false,
                    ValueFromPipeline = $true,
                    ValueFromPipelineByPropertyName = $true)]
                [String]$HString
            )#End Param
    
            Begin {
                Write-Verbose "Converting Here-String to Array"
            }#Begin
            Process {
                $HString -split "`n" | ForEach-Object {
    
                    $ComputerName = $_.trim()
                    if ($ComputerName -notmatch "#") {
                        $ComputerName
                    }    
    
    
                }
            }#Process
            End {
                # Nothing to do here.
            }#End
    
        }#Convert-HString
        Get-Counter -Counter (Convert-HString -HString $Counter) -SampleInterval 2 -MaxSamples 10
    } -ArgumentList $Counter).counterSamples
    

    【讨论】:

    • 你能举个例子吗?
    • 从链接下载脚本,从中复制您尝试调用的函数并将其粘贴到您的脚本块中,然后您的脚本块尝试调用它
    • 我在使用上面的代码时遇到了错误。必需的参数丢失或不正确。 + CategoryInfo : InvalidResult: (:) [Get-Counter], 异常 + FullyQualifiedErrorId : CounterApiError,Microsoft.PowerShell.Commands.GetCounterCommand + PSComputerName : server1
    • 看起来您对 Get-Counter 的调用不正确。
    • 也就是说,Get-Counter cmdlet 支持 -ComputerName... 为什么需要通过 Invoke-Command 运行它?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-08
    • 1970-01-01
    相关资源
    最近更新 更多