#script to ping host with given parameters

6 messages · Page 1 of 1 (latest)

high patrol
#

i'm brand new to powershell and trying to learn for my server scripting class. i have an assignment with the following requirements:

Use at least one function that accepts parameters.
Store and retrieve values using environment variables.
Include default values if no parameters or environment variables are provided.
Display the full ping output to the console.
DO NOT include the summary information displayed by the normal PING command.
Implement proper variable naming, looping, and conditional logic.```

here's what i have so far:
```powershell
<# TODO: documentation goes here dont forget moron
 #>
function Ping-Host {

    param(
    [Parameter(Mandatory=$false)]
    [string] $PING_HOST,

    [Parameter(Mandatory=$false)]
    [int] $PING_COUNT
    )

    if([string]::IsNullOrEmpty($PING_HOST)) {$PING_HOST = "8.8.8.8"}
    
    if($PING_COUNT) {$PING_COUNT} else {$PING_COUNT = 2}

    Write-Output "$PING_COUNT"
    Write-Output "$PING_HOST"
    Test-Connection -TargetName $PING_HOST -Count $PING_COUNT

}

Ping-Host```

right now im trying to figure out why the parameters i give it in the command line don't get used and instead always fallback to default values. **i'm not asking for someone to do it for me i just want some guidance, i really want to properly learn**
#

right now i'm trying to get this to even work before i start implementing environment variables

#

trying another method notice the output at the bottom

strong moth
#

When you invoke a PowerShell script as a file you provide arguments to the file and not the function inside of it. You have Ping-Host in there but no arguments are supplied. If you want the file to be the function itself and it be invoked like ./script.ps1 -PING_HOST ... -PING_COUNT ... you need to put the contents of the script as what is in the function, e.g. the script would be this exactly

param(
    [Parameter(Mandatory=$false)]
    [string] $PING_HOST,

    [Parameter(Mandatory=$false)]
    [int] $PING_COUNT
)

if([string]::IsNullOrEmpty($PING_HOST)) {$PING_HOST = "8.8.8.8"}
    
if($PING_COUNT) {$PING_COUNT} else {$PING_COUNT = 2}

Write-Output "$PING_COUNT"
Write-Output "$PING_HOST"
Test-Connection -TargetName $PING_HOST -Count $PING_COUNT
#

If you wanted a script to just define the function but without running it you can have the script with just your function definition then when it comes to invoking it you would first dot source the file so that the contents are loaded in the current scope then invoke the function, e.g.

# Dot source the script to load it in the same scope
# You need to ensure you've removed the Ping-Host at
# the end of the script and just have the ps1 contain
# only the function definition
. ./script.ps1

Ping-Host -PING_HOST ... -PING_COUNT ...
#

If you wanted feedback on the actual PowerShell code I would suggest a few things

  • Define the defaults on the actual parameters
    This way you can just define the defaults easily and PowerShell will automatically override those vars if they are specified by the caller
param (
    [Parameter()]
    [string]
    $PING_HOST = '8.8.8.8',

    [Paramter()]
    [int]
    $PING_COUNT = 0
)
  • Use Write-Host for console output or Write-Verbose for logging/debugging purposes
    When you use Write-Output the string you have there is emitted to the output pipeline. You typically only want this for actual objects you want the caller to capture like objects or results. If you are using it to just debug the function and see what the value is then consider using Write-Host so it writes to the console directly or Write-Verbose where it can selectively display it on the console when the function is called with -Verbose

  • Use PascalCase for parameters
    I would highly recommend sticking with the common convention of -PascalCase for parameters and not -SNAKE_CASE. It aligns with how pwsh functions typically are specified. You don't have to it's just common convention