I am trying to get Sitecore users' StartUrl (configured in Profile tab) in powershell and then supply it to Invoke-WebRequest in powershell to get the response status code for the StartUrl (to know if StartUrl is valid url or not).

To access StartUrl, a user has to be authenticated in Sitecore application. We also have authentication enabled in IIS. So in order to access StartUrl in browser, a user has to authenticate to IIS first and then authenticate in Sitecore (double authentication). How can we achieve this in powershell? I am using the below script and cant figure out where to supply Sitecore credentials in this

$IISuser = 'User'
$IISpass = 'Password!'
$pair = "$($IISuser):$($IISpass)"
$encodedCreds = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($pair))
$basicAuthValue = "Basic $encodedCreds"
$Headers = @{
    Authorization = $basicAuthValue
$url = "https://mySitecoreInstance/?sc_mode=edit&sc_itemid=%7b50F6C3EC-CE89-4987-B0EF-5005938FE61D%7d&sc_site=website&sc_lang=en-US"
(Invoke-WebRequest $url -Headers $Headers).StatusCode

Error -

Invoke-WebRequest : Server Error
401 - Unauthorized: Access is denied due to invalid credentials.
You do not have permission to view this directory or page using the credentials that you supplied.
At line:11 char:2
+ (Invoke-WebRequest $url -Headers $Headers).StatusCode
+  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand

The same script works fine if $url is Sitecore login url which doesnt need Sitecore authentication to access it ie $url = https://mySitecoreInstance/Sitecore/login

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.