Need some help regarding authentication issues in CRM while using powershell commands. When I use the following commands through powershell I get a 401 "Unauthorized".
$CRMCredentials=get-credential (username="someuser";password="somepassword")
$systemid='GSC-160802-836043'
$APIURL="https://crm.family.com/family/api/data/v8.2/gen_systems?`$filter=gen_serialnumber eq '$systemid'"
$headers = @{ Accept = 'application/json'}
$a=Invoke-RestMethod -Method Get -Uri $APIURL -Credential $CRMCredentials -Headers $headers
However, if I copy the url and paste it in the browser I get a login prompt where I enter the same credentials which I am using with powershell. After that I am able to reach the API endpoint"https://crm.family.com/family/api/data/v8.2/gen_systems?$filter=gen_serialnumber eq 'GSC-160802-836043'"
.
Using developer tools of chrome and while inspecting the http request I noticed that there are 4 cookies generated .
Using the information of the cookies I wrote a test code and was able to login.
$CRMCredentials=get-credential (username="someuser";password="somepassword")
$systemid='GSC-160802-836043'
$APIURL="https://crm.family.com/family/api/data/v8.2/gen_systems?`$filter=gen_serialnumber eq '$systemid'"
$headers = @{ Accept = 'application/json'}
$expiryforfirstcookie='2070-07-13T17:32:26.556Z'
$finaltime=Get-Date -Date ([datetime]::Parse($expiryforfirstcookie)) -Format G
$ContentType = "application/json; odata.metadata=minimal"
$cookie=new-object system.net.cookie
$cookie.name = "ReqClientId"
$cookie.path = "/"
$cookie.value = "9230....."
$cookie.domain = "crm.family.com"
$cookie.Secure="true"
$cookie.expires = "$finaltime"
$cookie.HttpOnly="true"
$session = New-Object Microsoft.PowerShell.Commands.WebRequestSession
$session.cookies.add($cookie)
$cookie2=new-object system.net.cookie
$cookie2.name = "NSC_wt_gvmmttm_dsn003_gf"
$cookie2.path = "/"
$cookie2.value = "ffffffff096c...."
$cookie2.domain = "crm.family.com"
$cookie2.Secure="true"
#$cookie2.expires = "Session"
$cookie2.HttpOnly="true"
$session.cookies.add($cookie2)
$cookie3=new-object system.net.cookie
$cookie3.name = "MSISAuth"
$cookie3.path = "/"
$cookie3.value = "77u/PD94bW....."
$cookie3.domain = "crm.family.com"
$cookie3.Secure="true"
#$cookie2.expires = "Session"
$cookie3.HttpOnly="true"
$session.cookies.add($cookie3)
$cookie4=new-object system.net.cookie
$cookie4.name = "MSISAuth1"
$cookie4.path = "/"
$cookie4.value = "UW9BREU0OHB4Y2c....."
$cookie4.domain = "crm.family.com"
$cookie4.Secure="true"
#$cookie2.expires = "Session"
$cookie4.HttpOnly="true"
$session.cookies.add($cookie4)
$a=Invoke-RestMethod -Method Get -Uri $APIURL -Credential $CRMCredentials -WebSession $session -Headers $headers -ContentType $ContentType
My question to you guys is that I think that the cookies information is changing therefore it would be a good idea to extract and include in my "Invoke-RestMethod" command . How I go about doing this? I have been searching couldn't find a valid resource. Thanks in advance!