CAlling a function with parameters is failing

Welcome Forums General PowerShell Q&A CAlling a function with parameters is failing

This topic contains 2 replies, has 3 voices, and was last updated by

 
Participant
1 year ago.

  • Author
    Posts
  • #83887

    Participant
    Points: 0
    Rank: Member

    I have a simple function which takes 2 arguments.

    When I call the function like this, it throw the error. I am sure I am doing a mistake the way I am passing parameters.

    PS D:\scripts> .\Validate-QRM-HPC.ps1 AWDWQRMHEAD01 D:\FHLBScripts\log\Log-QRM-HPC-Validation.txt
    Validate-HPC : Cannot bind argument to parameter 'aQRMHeadNodeName' because it is an empty string.
    At D:\scripts\Validate-QRM-HPC.ps1:87 char:15
    + Validate-HPC $aQRMHeadNodeName $logfile
    + ~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidData: (:) [Validate-HPC], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorEmptyStringNotAllowed,Validate-HPC
    Add-PSSnapIn Microsoft.HPC
    Import-Module AWSPowerShell

    # Write-Log Function makes it easy to write messages to a log file that is parseable and based on a standard log format
    Function Write-Log {
    [CmdletBinding()]
    Param(
    [Parameter(Mandatory=$True)]
    [string]
    $Message,

    [Parameter(Mandatory=$False)]
    [ValidateSet("INFO","WARN","ERROR","FATAL","DEBUG")]
    [String]
    $Level = "INFO",

    [Parameter(Mandatory=$False)]
    [string]
    $logfile
    )

    $Stamp = (Get-Date).toString("yyyy/MM/dd HH:mm:ss:fff")
    $Line = $Stamp + "," + $Level + "," + '"' + $Message + '"'
    If($logfile) {
    Add-Content $logfile -Value $Line
    }
    Else {
    Write-Output $Line # used when a logfile name is not passed to Write-Log, Write to the Standard Output, the console.
    }
    }

    Function Validate-HPC {

    [CmdletBinding()]
    Param(
    [Parameter(Mandatory=$True)]
    [string] $aQRMHeadNodeName,

    [Parameter(Mandatory=$True)]
    [string]
    $logfile

    )

    try{

    $logMsg = "Validating HPC Cluster " + $aQRMHeadNodeName

    Write-Log $logMsg "INFO" $logfile

    $aclusterOutput = Get-HpcClusterOverview -Scheduler $aQRMHeadNodeName -Verbose

    $logMsg = -join ( "HPC Cluster Validation Completed ", "ClusterName: ", $aclusterOutput.ClusterName, " Total Node Count: ", $aclusterOutput.TotalNodeCount , " Total Ready Nodes: ", $aclusterOutput.ReadyNodeCount)

    Write-Log $logMsg "INFO" $logfile

    }
    Catch{

    $ErrorMessage = $_.Exception.Message

    $FailedItem = $_.Exception.ItemName

    $logMsg = "Exception Message: HPC Host name supplied is : " +$aQRMHeadNodeName + $_.Exception + " with Failed Item: " + $FailedItem

    Write-Log $logMsg "FATAL" $logfile

    }

    }

    Validate-HPC $aQRMHeadNodeName $logfile

  • #83888

    Participant
    Points: 0
    Rank: Member

    In your call to the function: name the parameters you are passing values to. The second parameter needs to be one of the enumerated in ValidateSet.

  • #83896

    Participant
    Points: 56
    Rank: Member

    Hi Jason,

    Just from whats on offer here, it looks like you are trying to call the script and not the function. Should it not be

    
    Validate-HPC -aQRMHeadNodeName "AWDWQRMHEAD01" -logfile "D:\FHLBScripts\log\Log-QRM-HPC-Validation.txt"
    
    

The topic ‘CAlling a function with parameters is failing’ is closed to new replies.