8

I tried both default credential and user/pass but keep getting 401 unauthorized error. What's the fix?

Here is the error I am getting ....

New-WebServiceProxy : The request failed with HTTP status 401: Unauthorized.
At C:\Scripts\GetItemID_UsingWebServices.ps1:1 char:26
+ $WS = New-WebServiceProxy <<<<  -uri http://server/sites/promo/_vti_bin/SiteData.asmx
    + CategoryInfo          : ObjectNotFound: (http://server...n/SiteData.asmx:Uri) [New-WebServiceProxy], WebExcept
   ion
    + FullyQualifiedErrorId : WebException,Microsoft.PowerShell.Commands.NewWebServiceProxy
Property 'Credentials' cannot be found on this object; make sure it exists and is settable.
At C:\Scripts\GetItemID_UsingWebServices.ps1:2 char:5
+ $WS. <<<< Credentials = [System.Net.CredentialCache]::DefaultCredentials
    + CategoryInfo          : InvalidOperation: (Credentials:String) [], RuntimeException
    + FullyQualifiedErrorId : PropertyNotFound

You cannot call a method on a null-valued expression.
At C:\Scripts\GetItemID_UsingWebServices.ps1:12 char:26
+ $GUS = $WS.GetURLSegments <<<< ($ItemURL, $webID, $bucketID, $strListID, $strItemID)
    + CategoryInfo          : InvalidOperation: (GetURLSegments:String) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

Code:

$WS = New-WebServiceProxy -uri http://server/_vti_bin/SiteData.asmx
$WS.Credentials = [System.Net.CredentialCache]::DefaultCredentials
#$WS.Credentials = [System.Net.NetworkCredential]::("user", (ConvertTo-SecureString "password" -AsPlainText -force), "domain")
$ItemURL = "http://server/sites/promo/sales/testLib/TestFile.txt"
$GUS = $WS.GetURLSegments($ItemURL, $webID, $bID, $strListID, $strItemID)
Write-Host $strItemID

Update with new code!

This code is working (meaning at least no more 401 error). However, I am still working on it since it does not return any values for webid or bid or listid or itemid.

$webID = $null
$bID = $null
$strListID = $null
$strItemID = $null
$GUS
$uri = "http://Server/sites/Promo/TestDocLibrary44/_vti_bin/SiteData.asmx"
$WS = New-WebServiceProxy -uri $uri -UseDefaultCredential
$ItemURL = "http://Server/sites/Promot/TestDocLibrary44/2011102100006b.pdf"
$GUS = $WS.GetURLSegments($ItemURL, [REF]$webID,  [REF]$bID,  [REF]$strListID,  [REF]$strItemID)
write-Host $strItemID.value # returning null or blank
write-Host $webID # returning null or blank
Write-Host "Test "
2
  • 1
    Where are all the SP Gurus ?????? Commented Oct 24, 2011 at 22:40
  • I went ahead and edited your question to include the information you gave in an answer below. You might also consider asking a new question since we have solved your 401 Unauthorized error.
    – Kit Menke
    Commented Oct 27, 2011 at 0:25

5 Answers 5

5

I don't really know powershell but I know webservices... and I think you need to use DefaultNetworkCredentials instead of DefaultCredentials:

$WS = New-WebServiceProxy -uri http://server/sites/promo/sales/_vti_bin/SiteData.asmx
$WS.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials
$ItemURL = "http://server/sites/promo/sales/testLib/TestFile.txt"
$GUS = $WS.GetURLSegments($ItemURL, $webID, $bID, $strListID, $strItemID)
Write-Host $strItemID

Also, make sure the account you are running this script as has access to the SharePoint site. Some webservices require higher permissions levels as well.

8
  • I copied and pasted your code and still 401 error... Commented Oct 26, 2011 at 14:40
  • Have you double checked the account's permissions?
    – Kit Menke
    Commented Oct 26, 2011 at 14:41
  • Yes, I am running this on WFE with SPAdmin who has full access to the site collection. Commented Oct 26, 2011 at 15:13
  • I just noticed that your web service proxy URL was pointing to the root site. I'm guessing http://server/sites/promo/sales/ is the site which has your doclib in it. Try the updated code.
    – Kit Menke
    Commented Oct 26, 2011 at 15:19
  • Yes sir. I have tried that combination as well. I tried $WS = New-WebServiceProxy -uri $uri -UseDefaultCredential then I get following error and dont receive 401 error. Argument: '2' should be a System.Management.Automation.PSReference. Use [ref]. At line:5 char:26 + $GUS = $WS.GetURLSegments <<<< ($ItemURL, $webID, $bID, $strListID, $strItemID) Commented Oct 26, 2011 at 15:43
3

If you're going to specify the password, I believe it has to be a secure string:

$WS.Credentials = [System.Net.NetworkCredential]::("USERNAME", (ConvertTo-SecureString "PASSWORD" -AsPlainText -force))

If you're specifying a domain, I believe it needs to be as a parameter, not as a fully qualified username:

$WS.Credentials = [System.Net.NetworkCredential]::("USERNAME", (ConvertTo-SecureString "PASSWORD" -AsPlainText -force), "DOMAIN")

See MSDN: http://msdn.microsoft.com/en-us/library/system.net.networkcredential.aspx

3
  • I updated the original post. I am still getting the same 401 unauthorized error. Is there something I need to check in IIS? From browser I can see all the methods for the _vti_bin/sitedata.asmx. I am logged in spadmin on the SP WFE. Commented Oct 25, 2011 at 19:15
  • What are you trying to do would be a good place to start I guess... there's no a lot to go on here.
    – webdes03
    Commented Oct 25, 2011 at 20:31
  • I am trying to get doc id for an item using GetURLSegments method via web services with Powershell. Commented Oct 26, 2011 at 13:05
2

The only solution I found to this problem is to use -UseDefaultCredential, but run the script with the "run as" option. And in the run as command you specify the credentials you need to hardcode.

2

Include domain if necessary

$username = "username"  
$password = "password"

$cred = New-Object System.Management.Automation.PSCredential -ArgumentList @($username,(ConvertTo-SecureString -String $password -AsPlainText -Force))

#assign $cred to the webServicesProxy.Credentials
$webServicesProxy.Credentials = $cred
0

In PowerShell, to address the 401 Unauthorized error, I needed to incorporate CredentialCache as shown below:

Add-Type -Path "$($dllPath)\Microsoft.SharePoint.Client.dll"
Add-Type -Path "$($dllPath)\Microsoft.SharePoint.Client.Runtime.dll"

$siteUrl = "https://example.fakeurl.com/path/site"

# Create SharePoint Client Context
$context = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl)

# Create NetworkCredential instance
$networkCredential = New-Object System.Net.NetworkCredential($username, $password, $domain)

# Create CredentialCache and add NetworkCredential
$credentialCache = New-Object System.Net.CredentialCache
$credentialCache.Add($siteUrl, "Negotiate", $networkCredential)

# Assign CredentialCache to ClientContext
$context.Credentials = $credentialCache

Remember to close and reopen your PowerShell session or console to clear any cached data before testing

Interestingly, in C#.NET, you don't need to use CredentialCache; System.Net.NetworkCredential alone is sufficient.

Reference: https://learn.microsoft.com/en-us/troubleshoot/developer/webapps/iis/www-authentication-authorization/access-denied-call-web-service

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.