Reputation: 449
I have downloaded PowerShell script from below link.
https://gallery.technet.microsoft.com/scriptcenter/PowerShell-Perfmon-0f013da8
I am able to run this script successfully on local machine but facing issue on remote machine. I updated below code
(Get-Counter -ComputerName $ComputerName -Counter (Convert-HString -HString $Counter) -SampleInterval 2 -MaxSamples 10).counterSamples
to following.
(Invoke-Command -ComputerName $ComputerName -ScriptBlock {Get-Counter -Counter (Convert-HString -HString $Counter) -SampleInterval 2 -MaxSamples 10}).counterSamples
Now I am getting below error.
The term 'Convert-HString' is not recognized as the name of a cmdlet, function, script file, or operable program.
Upvotes: 0
Views: 550
Reputation: 1076
The function doesn't exist on the remote computer you are trying to run it in. You need to paste the full function in your scriptblock prior to calling it so it has it loaded when it attempts to run it. With Invoke-Command / anything involving a PSSession on another machine, you're running in the context of that machine. If you load a function/module/variable on your local machine, it only exists on your local machine.
Edit: Updated to allow $Counter
to be set on the local machine, then passed into the -ScriptBlock
by using Invoke-Command
's -ArgumentList
param and parameterizing the scriptblock
Example:
(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
Upvotes: 1