VMware Cloud Community
Ranger_rkm
Contributor
Contributor

Run function from commandline

Hello,

I'm trying to run this PowerShell function via the command line.  I know that it need a host to run against.

function Get-VMHostLunLatency {

  param([parameter(Mandatory=$true, ValueFromPipeline=$true)] $VMHost)

  process {

    $luns = Get-ScsiLun -VmHost $VMHost

    $DiskStats = $VMHost |

      Get-Stat -stat disk.totalreadlatency.average,disk.totalwritelatency.average -maxsamples 1 -realtime

    foreach ($lun in $luns) {

      $Stats = $DiskStats  |

        Where { $_.Instance -eq $Lun.CanonicalName }

      if ($stats.length -ne $null) {

        $obj = New-Object PSObject

        $obj | Add-Member -MemberType NoteProperty -Name VMHost -Value $VMHost.Name

        $obj | Add-Member -MemberType NoteProperty -Name Lun -Value $lun.CanonicalName

        $obj | Add-Member -MemberType NoteProperty -Name ReadLatency -Value ($stats[0].Value)

        $obj | Add-Member -MemberType NoteProperty -Name WriteLatency -Value ($stats[1].Value)

        Write-Output $obj

      }

    }

  }

}

So, what I'm trying is this:

c:\scripts> .\EsxLunLatency.ps1 Get-VMHostLunLatency "Host1.test.local"

Which is not returning any data.

The file name is EsxLunLatency.ps1 and the function name is Get-VMHostLunLatency, so what I'm I doing wrong.

Thanks,

Reply
0 Kudos
1 Reply
LucD
Leadership
Leadership

You are defining the function inside the .ps1 file, you first need to dot-source the .ps1 file to get the function "known" to the PS engine.

Then you can call the function.

Like this

. .\EsxLunLatency.ps1

Get-VMHostLunLatency "Host1.test.local"

Note that there are 2 dots with a blank in between on the first line


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos