PowerShell for Loop Ideas

This topic contains 1 reply, has 2 voices, and was last updated by Profile photo of Ed Grant Ed Grant 3 months, 3 weeks ago.

  • Author
    Posts
  • #62955
    Profile photo of ahmer khan
    ahmer khan
    Participant

    Hello,

    I have recently came across a need for a powershell script for executing multiple curl commands via Api .

    I am executing a Curl Post to trigger a work flow, and using a curl get to tap into the rss feed. I cant wrap my head around how to execute multiple Curl posts in a loop only to continue if the workflow was successful. The Rss feed will show running, until it either fails or succeeds. So Im thinking

    for Loop
    — Execute Curl Post
    –> execute Curl Get and Watch
    —-> This is where Im confused, so how do I handle the three phases running/Failed/Success
    Running I need to continue to watch, or do i do some kind of until but can I do it with multiple conditions Like
    Until Curl Command If = Failed Break else if = success Continue?

    I know its a newbie question, but any help would be appreciated as I am on a time crunch.

  • #63018
    Profile photo of Ed Grant
    Ed Grant
    Participant

    I would suggest using a switch statement. You would want to capture the output of the Curl get and run different code depending on the result. I'm not familiar with Curl, but here is a switch statement that should get the concept across.

    $Bitness = Get-WmiObject win32_operatingsystem
    
    Switch ($Bitness.OSArchitecture)
        {    
        "64-bit"
            {
              #Code here
            }
    
        "32-bit"
            {
                #Code here
            }
        }
    
    

    I imagine with Curl, you would probably only get a few different results. You want to put all possible results (also known as a case) in quotes, with a corresponding script block right afterwards. For my example, there will only ever be two cases, 32-bit and 64-bit, so those are the only ones necessary. You can also set a default case if there are no matches. You can do that by doing something like this:

    
    $Bitness = Get-WmiObject win32_operatingsystem
    
    Switch ($Bitness.OSArchitecture)
        {
    
        default 
            {
             #Default code here
            }
          
        "64-bit"
            {
              #Code here
            }
    
        "32-bit"
            {
                #Code here
            }
        }
    
    

You must be logged in to reply to this topic.