1

I am trying to incorporate this function to my script but it shows me errors If I add any other code before this function as seen below. I enclosed the parameter in {} and the error no longer shows but it messes up my output since the parameters are no longer being defined. I have other code that needs to run and before this part of the script executes. You can see adding any other code before this function causes errors.

So I don't know how to include this part of the script with my other code and have it work. Any Help?

enter image description here

3
  • Please avoid using pictures for code. It makes it impossible to copy it. Commented May 28, 2020 at 20:18
  • What you need to do is just place that code in a function. Commented May 28, 2020 at 20:19
  • Take the Get-TraceRoute code and convert it into a function. Commented May 28, 2020 at 20:21

2 Answers 2

7

Place the script into a function. You just surround the code in a function.

Function Get-Traceroute{
    [CmdletBinding()]
    param(
        [Parameter(Mandatory=$True,ValueFromPipeline=$True)]
        [String]$Target,

        [Parameter(ValueFromPipeline)]
        [Alias("c")]
        [ValidateRange(5,100)]
        [int]$PingCycles = 10, #Default to 10 pings per hop; minimum of 5, maximum of 100

        [Parameter(ValueFromPipeline)]
        [Alias("b")]
        [ValidateRange(32,1000)]
        [int]$BufLen = 32, #Default to 32 bytes of data in the ICMP packet, maximum of 1000 bytes

        [Parameter(ValueFromPipeline)]
        [Alias("s")]
        [IPAddress]$DNSServer = $Null,

        [Parameter(ValueFromPipeline)]
        [Alias("f")]
        [String]$Filename = "Traceroute_$Target"

    )
    Function script:Set-Variables {
    ...more code here...
}

Then you can just call the function Get-TraceRoute -Target 8.8.8.8

Sign up to request clarification or add additional context in comments.

2 Comments

i will test everything and confirm. 5 mins please :)
Great minds think exactly alike at the exact same time :)
5

Decorators and parameters need to be placed at the top of a script.

So, your parameter declaration (the [CmdletBinding] to then end of the param() block) should be at the top of the script. Then you can include that other function in it's entirety. For instance, it would look like...

 [Parameter(ValueFromPipeline)]
    [Alias("s")]
    [IPAddress]$DNSServer = $null,

    [Parameter(ValueFromPipeline)]
    [Alias("f")]
    [String]$Filename = "Traceroute_$Target"

    )#end of your original param block

Function Get-TraceRoute{
    param(
    [Parameter(Mandatory=$True,ValueFromPipeline=$True)]
    [String]$Target,
#. omitting 230 lines.#

$PerTraceArr | Format-Table -Autosize
}#end of Get-TraceRoute

You would then end the script like so:

}#end of Get-TraceRoute

Get-TraceRoute -Target $ip #or whichever params you want to pass in to your `.ps1` file.

2 Comments

you and me posted the exact same thing lol. At the exact same time
yeah putting it at the top seems to be working.. I will test everything and confirm in 2 mins

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.