Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
PSBlog006
Function Login-Acumatica
{
#This OutputType allows you to strongly type the return object from this function
#In this case we are going to encapsulate the WebRequestSession object initialization and return it
#to the calling process as to be used in other functions. This is needed as to pass the authentication
#State to other functions that need it. Don’t forget to use the Logout-Acumatica function else you will incur
#unnecessary User Logins if they are left open.
[OutputType([Microsoft.PowerShell.Commands.WebRequestSession])]
param(
$User = $Null,
$Password = $Null,
$Tenant,
$EndPoint
)
if($User -eq $Null)
{
#This is a great way to *not* store sensitinve information into a plain text
#file such as this script. This keeps IT Managers very happy.
#Dont be the person that exposes sensitive authentication information
#That in turn gets your system hacked. If you need to automate a script in a
#scheduler look into using a PowerShell secure-string
#To save your credentials to disk in a secure way.
#That would in turn allow for a fully autonomous execution of a script without
#storing sensitive information in an insecure manner
$credentials = Get-Credential
$user = $credentials.UserName
$Password = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto([System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($credentials.Password))
}
$WebSession = New-Object Microsoft.PowerShell.Commands.WebRequestSession
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Content-Type", "application/json")
$body = "{`n `"name`":`"$User`",`n `"password`":`"$Password`",`n `"tenant`":`"$Tenant`"`n}"
$Uri = "$EndPoint/entity/auth/login"
$response = Invoke-RestMethod $Uri -Method 'POST' -Headers $headers -Body $body -WebSession $WebSession
return $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.