Using ps script to invoke rest post using variables

I want to make a PowerShell script that can take my Postman code and use the same json file with variables to execute as many times needed. Using postman I was able to replace the parts of code with variables. Then using the runner, and selecting the json file. It successfully uploaded three files correctly. To make it easier on the end user, I want to just give them a PowerShell script that will execute in the same way.

json

{
    "annotation": "22.04",
    "storage_container_name": "default-container-41106426025512",
    "url": "http://192.168.254.80/tscImages/ubuntu-22.04.4-live-server-amd64.iso",
    "image_type": "ISO_IMAGE",
    "name": "Ubuntu Server"

},
{  
    "annotation": "22.04",
    "storage_container_name": "default-container-41106426025512", 
    "url": "http://192.168.254.80/tscImages/ubuntu-22.04.2-desktop-amd64.iso",   
    "image_type": "ISO_IMAGE",
    "name": "Ubuntu Desktop"
   
},
{  
    "annotation": "DISK",
    "storage_container_name": "default-container-41106426025512", 
    "url": "http://192.168.254.80/tscImages/webserver.qcow2",   
    "image_type": "DISK_IMAGE",
    "name": "WebServer"
    
}

postman code

{
“annotation”: “{{annotation}}”,
“image_import_spec”: {
“storage_container_name”: “{{storage_container_name}}”,
“url”: “{{url}}”
},
“image_type”: “{{image_type}}”,
“name”: “{{name}}”
}

ps snippet

$body = @"
{
"annotation": "22.04",
"image_import_spec": {
"storage_container_name": "default-container-41106426025512",
"url": "http://192.168.254.80/tscImages/ubuntu-22.04.4-live-server-amd64.iso"
},
"image_type": "ISO_IMAGE",
"name": "Ubuntu Server"
}
"@

$response = Invoke-RestMethod ‘https://192.168.254.100:9440/PrismGateway/services/rest/v2.0/images’ -Method ‘POST’ -Headers $headers -Body $body
$response | ConvertTo-Json

Juan,
Welcome to the forum. :wave:t3:

OK, that’s a nice plan. :+1:t3: And what is your question? :thinking:

And BTW: When you post code, sample data, console output or error messages please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.

Thanks in advance

How to format code in PowerShell.org 1 <---- Click :point_up_2:t4: :wink:

( !! Sometimes the preformatted text button hides behind the settings gear symbol. :wink: )

You may go back, edit your question once again and fix the formatting of your code.

So what are you looking for help with exactly? What have you tried? Does your PS you wrote work or are you running into an error? If an error, what’s the error? Is your issue that you don’t know how to store code or what?

Help us help you, it’s not clear what you are needing help with.

oops sorry Olaf didn’t see you had already asked the same thing, think I posted right after you heh.

Sorry for confusion, I was looking for help or guidance on what I would need to do to make the script in ps. Did manage to get what I needed already.

Tried to clean up the format but only the first code I copied would stay formatted. I saw other posts where you asked for that in other posts.

Great. I’m glad to hear that. :+1:t3:

An image of code is not helpful at all!!

I even posted a link with the explanation how to format code. :point_up:t3: :man_shrugging:t3:

Invoke-RestMethod (Microsoft.PowerShell.Utility) - PowerShell | Microsoft Learn

Use PS objects. You don’t need to use JSON here for like… your body ($payload variable). Let PS do the work for you, and it will make your code cleaner. You’ll see from the docs body happily accepts an object.

One reason we went with json file for the different inputs, is that we are handing this over to someone else to use. Rather than having them modify the script. It’s safer for them to update the json file.
Moreover, I’m more of a contributor to what was needed than the actual author, taking time to learn as I help. So while I trust you know what you’re saying it’s still Greek to me. Thank you for the input tho, I appreciate it.

In your code, you literally have JSON. It’s not a separate file. Don’t confuse that with the JSON file you are importing. JSON’s merely a format for data in a file:

{
"FirstName" : "John",
"LastName" : "Cena"
}

I was pretty clear in my previous statement. Your $payload variable is JSON. I’d suggest doing an Object instead, it’ll make it easier to read. You can just do a hashtable really. It has nothing to do with the JSON file your using in the other part of your code. As a quick example (partial you could do something like this (replacing the hardcoded values with your variables of course)

$Payload = @{
  Annotation        = "Test"
  image_import_spec = @{
    storage_container_name = "StorageContainerName"
    url                    = "https://fakeurl.com"
  }
}

You could even do something like this:

$ImageImportSpec = @{
  storage_container_name = "StorageContainerName"
  url                    = "https://fakeurl.com"
}

$Payload = @{
  Annotation        = "Test"
  image_import_spec = $ImageImportSpec
}

As you can hopefully see, that’s much more readable. In any case it’s up to you, or i guess the author (which you might ask for help on it since they are the responsible party).