PowerShell Remoting收集性能计数器

时间:2017-05-18 13:41:00

标签: powershell remoting

我从下面的链接下载了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.

1 个答案:

答案 0 :(得分:1)

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

修改:已更新以允许在本地计算机上设置$Counter,然后使用-ScriptBlock' s 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