Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
PSBlog002
#We will declare Variable that will change often up at the top
#This is a common practice for scripts.
#Later we will convert this to a function, and these will become parameters
$AcumaticaUser = "Admin"
$AcumaticaPassword = "123"
$AcumaticaTenant = "Company"
$AcumaticaEndPoint = "http://localhost/APS20_104_0012"
$ReferenceNbr = "AR008403"
$WebSession = New-Object Microsoft.PowerShell.Commands.WebRequestSession
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Content-Type", "application/json")
#All we must do to format a string with variables is add the variable to the string.
#the ` grave char escapes so the `n are carriage returns.
$body = "{`n `"name`":`"$AcumaticaUser`",`n `"password`":`"$AcumaticaPassword`",`n `"tenant`":`"$AcumaticaTenant`"`n}"
$Uri = "$AcumaticaEndPoint/entity/auth/login"
$response = Invoke-RestMethod -Uri $Uri -Method 'POST' -Headers $headers -Body $body -WebSession $WebSession
#if you need to get the $ char into the string you can use `$ which will get it into place as we did here
# We also need to use the `' to get the single quote into the string
$Uri = "$AcumaticaEndPoint/entity/default/18.200.001/Invoice?`$Filter=ReferenceNbr eq `'$ReferenceNbr`'"
#Note: Post man incorrectly sets the -Body $body parameter. It needs to be removed for this to work with a get
$response = Invoke-RestMethod -Uri $Uri -Method 'GET' -Headers $headers -WebSession $WebSession
#Piping the response to the ConvertTo-Json method will format the value and
#display to the output window
$response | ConvertTo-Json
$Uri = "$AcumaticaEndPoint/entity/auth/logout"
$response = Invoke-RestMethod -Uri $Uri -Method 'POST' -Headers $headers -Body $body -WebSession $WebSession
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.