Netbox secrets with PowerShell - powershell

I am trying to access secrets in Netbox like in this example
https://netbox.readthedocs.io/en/latest/api/working-with-secrets/
but using Powershell but I seem to have hit a brick wall.
Here is my code:
Add-Type -AssemblyName System.Web
$Hdrs = #{}
$Hdrs.Add("Authorization","Token fafcaea339cf926c0d79tokenf916aeec2d18bdd")
$Hdrs.Add("Accept","application/json; indent=4")
$body = Get-Content 'c:\key\private.txt'
$body = [System.Web.HttpUtility]::UrlEncode($body)
Invoke-RestMethod -Method post -Uri "http://netbox.et/api/secrets/get-session-key/" -Headers $Hdrs -body $body
I just get the error
Invoke-RestMethod : Private key was not provided.
I'm pretty sure the issue is with how I am forming the request, however, I don't know enough about curl to know what '--data' do and how that would look in Powershell. Any help would be most appreciated.

OK, there were multiple issues. The first that I need to pass the private key as an object with two clear fields like I am the header and the other was I had URL encoded it by defining it in the header and then used a function to URL encode it again, which in turn made the private key invalid.
This is the solution
Add-Type -AssemblyName System.Web
$Hdrs = #{}
$body = #{}
$Hdrs.Add("Authorization","Token fafcaea339cf926c0d7977e3ff916aeec2d18bdd")
$Hdrs.Add("Accept","application/json; indent=4")
$Hdrs.Add("Content-Type","application/x-www-form-urlencoded")
$Pkey = Get-Content -raw 'c:\key\private.txt'
$body.add("private_key",$PKey)
Invoke-RestMethod -Method post -Uri "http://netbox.net/api/secrets/get-session-key/" -Headers $Hdrs -Body $body

Related

Invoke-RestMethod a boolean value

Using Powershell 5.1
I'm trying to post my body that includes a Boolean
$body= #{
"file"= $filePath
"name"= $fileName.name
"booleanValue" = $true
}
$response = Invoke-RestMethod -Uri $uri -Headers $headers -Method POST -body $body
The issue i'm having is the booleanValue not translating correctly, I have it working in my Postman test but I unable to get it working in my powershell.
I have tried 0 or 1 for it, 'true' and assigning a variable to [bool] but unable to handle it correctly in my typescript service.
Thanks for the help in advance,

Invoke-Webrequest with UseBasicParsing fails to pass SecureString password

I am trying to upload a file [path] to [uri] with Invoke-WebRequest with the following:
$Uri = [uri]
$Body = #{
username = $Credentials.UserName
password = $Credentials.Password
file = Get-Item -Path [path]
}
Invoke-WebRequest -Uri $Uri -Method 'POST' -Body $Body -UseBasicParsing
(As I'm stuck with Powershell 5.1 I include -UseBasicParsing).
This leads to a failed login response, and inspecting the server response, I see that it interprets the password string as "System.Security.SecureString" instead of the actual password.
I assume that this happens because -UseBasicParsing changes how SecureString objects are passed? I am a complete novice in Powershell and HTTP and so would greatly appreciate any suggestions for workarounds that don't compromise on security, and/or any explanations for why this happens.
You can try decoding the password inline, like this:
$Uri = [uri]
$Body = #{
username = $Credentials.UserName
password = ($Credentials.Password.GetNetworkCredential().Password)
file = Get-Item -Path [path]
}
Invoke-WebRequest -Uri $Uri -Method 'POST' -Body $Body -UseBasicParsing
and here's the result:

IBM Urbancode AddVersionStatus API call from Powershell How?

https://www.ibm.com/support/knowledgecenter/en/SS4GSP_6.2.7/com.ibm.udeploy.api.doc/topics/udclient_addversionstatus.html
How to make this PUT call using Powershell please?. I am using Powershell 5.
I came across this post while trying to do the same thing. The problem for me was in knowing what exactly the correct URL was (see Adam Parsons' answer):
$URL = "url-goes-here"
After a lot of searching (IBM's documentation was not worth much in this effort), I was able to identify the correct URL by way of watching traffic in Chrome developer tools (thanks to Darrell Schrag's blog post: https://drschrag.wordpress.com/2013/10/03/the-udeploy-rest-api).
For those searching for this, my PowerShell REST call sequence now looks like this (and executes successfully):
$tokenEncoded = [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes( "PasswordIsAuthToken:{"token":"$authToken"}" ))
$headers = #{Authorization = "Basic "+$tokenEncoded}
# 1. Get component version ID
$uri = "$uDeployServer:8443/cli/version/getVersionId`?component=$componentName&version=$versionName"
$versionId=Invoke-RestMethod -Uri $uri -Method GET -Headers $headers
# 2. Add component version status
$uri = "$uDeployServer:8443/rest/deploy/version/$versionId/status/$versionStatus"
Invoke-RestMethod -Uri $uri -Method PUT -Headers $headers
Probably something like this...
$Hash = #{
Component="StringValue"
Version="StringValue"
Status="StringValue"
}
$Json = $Hash | ConvertTo-Json
$URL = "url-goes-here"
$Cred = Get-Credential
Invoke-RestMethod -Method "POST" -Uri $url -Credential $Cred -Body $Json

Powershell: Uploading file to Docparser API using Invoke-RestMethod

$header = #{'Authorization'='Basic <auth code value>'}
$ping = Invoke-RestMethod -Uri "https://api.docparser.com/v1/ping" -Headers $header
ping works fine...returns "pong". I then make a request for the Parser ID which is needed for uploading documents. I am able to retrieve this value successfully.
$parser = Invoke-RestMethod -Uri "https://api.docparser.com/v1/parsers" -Headers $header
$parserID = $parser.id
Now here is where I try to upload a pdf, which fails.
$fileToParse = "C:\test.pdf"
$body = #{'file'=$fileToParse}
$uploadDoc = Invoke-RestMethod -Uri "https://api.docparser.com/v1/document/upload/$parserID" -Method Post -Headers $header -ContentType 'multipart/form-data' -Body $body
API response keeps saying "Error: input empty"
This is the documentation from Docparser on how to upload pdfs:
Any thoughts on what I'm doing wrong here? Thanks in advance,
Eric
The problem is that your body currently just contains the path to your local file. Docparser expects however the content of the file as multipart/form-data.
I never worked with PowerShell, but I think something like this should work for you:
$body = #{
"file" = Get-Content($fileToParse) -Raw
}
I got the code from this answer: How to send multipart/form-data with PowerShell Invoke-RestMethod

Invoke-RestMethod and Fiddler, can't get 200 code in return

I am trying to replicate a POST call that can be send from GUI using Invoke-RestMethod. I would like to automate it and have been trying to use powershell to do it.
It alwasy returns 202 code, have been trying it for a few hours now but can't progress. This is really the first time I am playing with invoke-restmedod and Rest so please be detailed what's wrong. Any help is highly appreciated.
So the successful call captured by Fiddler is this:
The powershell code is:
$WfManDirUserPass = "Password"
$secpasswd = ConvertTo-SecureString $WfManDirUserPass -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential ("admin", $secpasswd)
$active = #{
ipaddress="192.168.100.116"
Port="62805"
status="0"
}
$json = $active | ConvertTo-Json
try{
$response = invoke-restmethod -uri https://myhost/MAM/wfservice/workers/?ip="&"port="&"newStatus=Deactivating -Method POST -Body $json -Credential $cred -ContentType 'application/json'
} catch {
write-host("Sorry, it does not work")
}
This powershell code in Fiddler returns:
I can see that the JSON is not exactly the same on the attached images. However I stuck now and would really appreciate some help now.
This is a reply from 1RedOne (Reddit) user that helped me out:
For one, let's wrap your whole -URI in single quotes and remove the double quotes. Your URL is probably messed up, which isn't helping things.
$uri = 'https://myhost/MAM/wfservice/workers/?ip=&port=&newStatus=Deactivating'
$response = invoke-restmethod -uri $uri-Method POST -Body $json -Credential $cred -ContentType 'application/json'
2.
Furthermore, your call from fiddler uses basic authentication, and is probably incompatible with using a -Credential object. Try replacing your credentials with this format.
$user = "yourusername"
$pass = 'yourPassWord'
# Build auth header
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user, $pass)))
# Set proper headers
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add('Authorization',('Basic {0}' -f $base64AuthInfo))
Then, reference the $header object within your Invoke-RestMethod, like so.
$response = invoke-restmethod -uri $uri- Method POST `
-Body $json -Header $headers -ContentType 'application/json'
That's it. It worked like a charm!