Return from Function

Welcome Forums General PowerShell Q&A Return from Function

Viewing 7 reply threads
  • Author
    Posts
    • #238142
      Participant
      Topics: 3
      Replies: 15
      Points: 60
      Rank: Member

      I am sure this is a question of me not having a programming background, so please be kind.

      I have a function, it goes off to an API and checks for a result.

      function is called, getticket

      in my code, i call that function by doing this

      $openTicket = getticket

      Then i use if($openTicket){} assuming it is true or false, depending on if getticket returns a ticketobject from the API.

      What i dont understand, or am doing wrong, is if i want to output information from getticket whilst it is in progress, that info is then seen as $openTicket having a value.

      I appreciate this might be difficult to follow.

      So lets say getticket does this

      function getticket{

      Write-Output “Talking to API”

      # api query

      $apiresults

      }

      it is taking ‘Talking to API’ as a value for $openTicket instead of just $apiResults.

      So, how would i do that? im sure it can be done but i dont really understand how.

       

    • #238166
      Participant
      Topics: 11
      Replies: 146
      Points: 55
      Helping Hand
      Rank: Member

      If I am reading it correctly from your “sudo code” the $openTicket and $apiresults would contain the same object. That being said your “IF” block should work unless the $openTicket variable contains nothing or is $Null. So to troubleshoot we would need to see a bit more code but maybe sanitize the URL you’re protecting (for good reason) with a fake URL.

      But from what you’ve shared it should work unless the variable did not get an object. But what is “IF” without “ELSE”?

      You could specifically try something overkill like. . .

      $This = $openTicket.Length
      if ($This -gt 0) {Do-SomeThing}

    • #238190
      Participant
      Topics: 3
      Replies: 15
      Points: 60
      Rank: Member

      Hey!

      $apiResults should return a json response from the api, or nothing.

      so what id like, is that I get to see the write-output from getticket, but that not to impact $openTicket, and have open ticket either be the json response or false/null.

       

      so if I then did if($openTicket){#skip}else{#do something } it would only do something if $openticket didn’t return the json object.

    • #238235
      Participant
      Topics: 0
      Replies: 30
      Points: 284
      Helping Hand
      Rank: Contributor

      Hello titlerequired,

      As a quick and dirty fix try changing your Write-output to write-warning and it should do the trick.

      More proper way would be to use Write-verbose but that require your function to be advanced one and during the function call you would use   -verbose switch.

      Hope that helps.

    • #238286
      Participant
      Topics: 3
      Replies: 15
      Points: 60
      Rank: Member

      Hey!

      thanks both I will try this out and let you know.

    • #238295
      Participant
      Topics: 3
      Replies: 15
      Points: 60
      Rank: Member

      Using write-warning seems to work perfectly.

      So in terms of using write-verbose do i just need to add a param for verbose and then just add write-verbose anywhere i want output?

    • #238298
      Participant
      Topics: 3
      Replies: 15
      Points: 60
      Rank: Member

      OK Cooooooool!

      I added a [switch] param for $verbose and changed the write-warnings to write-verbose, but, they didnt do anything until i added this,

      if($verbose) {
              $oldverbose = $VerbosePreference
              $VerbosePreference = “continue”
          }
      So this is very helpful, and many thanks to both of you for your assistance.
    • #238421
      Participant
      Topics: 0
      Replies: 30
      Points: 284
      Helping Hand
      Rank: Contributor

      Glad that you figured it out.

      I was referring to [cmdletbinding()] it is actually explained in the post you are referring to as “Easier way“.

      Reference for more details:

      https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_functions_cmdletbindingattribute?view=powershell-5.1

      Hope that helps.

Viewing 7 reply threads
  • You must be logged in to reply to this topic.