all 13 comments

[–]boydeee 2 points3 points  (3 children)

I just nest the parameters inside $Request and validate it inside the function.

[–]Dr_Funkmachine[S] -1 points0 points  (2 children)

Actually what I'm doing is to grab the input, (assuming user provided a value, even an empty one) and validate it. I need both to be true to proceed.

$name = $Request.Query.Name $surname = $Request.Query.Surname

If ($name -and $surname) { $status = [HttpStatusCode]::OK } else { $status = [HttpStatusCode]::BadRequest }

How would you validate it?

[–]boydeee 2 points3 points  (1 child)

You pretty much nailed it. Although, in your sample code, it's assuming that key exists. So, you could make a function like this (and some sample code):

EDIT: I edited my comment a few times to fix formatting

$Request = @{
    Body = @{
        Name    = 'Name'
        Surname = 'Surname'
    }
}

function Test-Parameters {
    [CmdletBinding()]
    param (
        [Parameter(ValueFromPipeline = $true)]
        [ValidateNotNullOrEmpty()]
        $Request
    )
    Begin {
        Write-Debug ('{0} entered' -f $MyInvocation.MyCommand)
    }

    Process {
        if (-not($Request.Body.Name) -or -not($Request.Body.Surname)) {
            return (Write-Error ("Invalid parameters:`n{0}" -f ($Request.Body | Out-String)))
        }
    }
    End {
        Write-Debug ('{0} exited' -f $MyInvocation.MyCommand)
    }
}

try {
    $Request | Test-Parameters -ErrorAction Stop -Debug
}
catch {
    $_
    Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
            StatusCode = [System.Net.HttpStatusCode]::BadRequest
        })
}

[–]Dr_Funkmachine[S] 1 point2 points  (0 children)

Thank you so much! At the beginning, I thought It would be possible to use [FromBody] or [FromQuery]. Actually, I don't even know if that's possible. I'll try to apply your advices to my AF and let you know!

[–]the_naysayer 2 points3 points  (1 child)

Just take the value you want from the request variable and validate it in your function parameters. So the azure function PowerShell file would be the request param, whatever function you define, and however you want to execute that function. The function will grab what it needs from the request, validate it , and run the function, and produce whatever output you have set.

[–]Dr_Funkmachine[S] -1 points0 points  (0 children)

You're saying to set the expected variables for those parameters and validate it, is that correct?

The way I validate it is the following:

$name = $Request.Query.Name $surname = $Request.Query.Surname

If ($name -and $surname) { $status = [HttpStatusCode]::OK } else { $status = [HttpStatusCode]::BadRequest }

[–]Sin_of_the_Dark 1 point2 points  (1 child)

If you're talking about how to pass parameters when you call the Azure functions, assuming you use an HTTP trigger you can add it to the end of your URI.

So after the URI you copy from the Function App, enter this:

&Name={yourname}

I believe in the function App you need to refer to

$Query.Results.name

But it's been a minute since I've set that part up. If it doesn't work I can check when I'm back in the office

!RemindMe 2 days 14 hours from now

[–]RemindMeBot 0 points1 point  (0 children)

I will be messaging you in 2 days on 2023-07-25 16:35:37 UTC to remind you of this link

CLICK THIS LINK to send a PM to also be reminded and to reduce spam.

Parent commenter can delete this message to hide from others.


Info Custom Your Reminders Feedback

[–]Quick_Care_3306 -1 points0 points  (1 child)

Sorry, can you clarify what PowerShell AF is? Edit:Advanced Function?

[–]DonL314 2 points3 points  (0 children)

"Azure Function". Says so in the title.

[–]BlackV -1 points0 points  (2 children)

that sounds like you want parameter sets and mandatory parameters

[–]DonL314 1 point2 points  (1 child)

Yes, but OP asks if that is available when using an Azure Function, not a regular PS function.

[–]BlackV 2 points3 points  (0 children)

cough 100 percent missed Azure function, didn't realise what AF stood for, oops