When trying to run powershell script, i get 401 unauthorized

I have been trying to run this powershell script and when I put in my credentials, I get a 401 unauthorized error.

I have the primary site collection admin and listed in the global farm admin in the central administration.

Please see code block below, how can i get rid of the 401 unauthorized?!

Transcript started, output file is C:\PowerShell Logs\Powershell_SharePoint.ps1
.log
Starting profile: C:\Program Files\Common Files\Microsoft Shared\Web Server Ext
ensions\14\CONFIG\POWERSHELL\Registration\SharePoint.ps1
PS C:\Users\mirza.baig> ##======================================================
========================
PS C:\Users\mirza.baig> ## Automation  : Get all the list items required for Fac
ulty Directory on the External Site using SharePoint 2010 Lists Web Service in P
owerShell
PS C:\Users\mirza.baig> ## Note        : Run this script to update XML files and
 import to external website servers
PS C:\Users\mirza.baig> ## Author      : Gabriel Zinn
PS C:\Users\mirza.baig> ## Date        : March 30, 2012
PS C:\Users\mirza.baig> ##======================================================
==========================
PS C:\Users\mirza.baig> Add-PsSnapin Microsoft.SharePoint.PowerShell
Add-PSSnapin : Cannot add Windows PowerShell snap-in Microsoft.SharePoint.Power
Shell because it is already added. Verify the name of the snap-in and try again
.
At line:1 char:13
+ Add-PsSnapin <<<<  Microsoft.SharePoint.PowerShell
    + CategoryInfo          : InvalidArgument: (Microsoft.SharePoint.PowerShel
   l:String) [Add-PSSnapin], PSArgumentException
    + FullyQualifiedErrorId : AddPSSnapInRead,Microsoft.PowerShell.Commands.Ad
   dPSSnapinCommand

PS C:\Users\mirza.baig> #------------- Input Parameters --------------
PS C:\Users\mirza.baig> ## Specify the site URL from where you need to get all t
he list items
PS C:\Users\mirza.baig> $webURL1="https://blsconnect.brooklaw.edu"
PS C:\Users\mirza.baig> $webURL2="https://blsconnect.brooklaw.edu/faculty"
PS C:\Users\mirza.baig> ## Specify the list name from where you need to get the
list items
PS C:\Users\mirza.baig> ## FacultyDirectory1 includes CV URL
PS C:\Users\mirza.baig> [string]$listName1 = "Faculty Directory"
PS C:\Users\mirza.baig> ## FacultyDirectory2 includes Photo URL
PS C:\Users\mirza.baig> [string]$listName2 = "Faculty Directory"
PS C:\Users\mirza.baig> [string]$listName3 = "Noteworthy"
PS C:\Users\mirza.baig> [string]$listName4 = "Faculty Publications"
PS C:\Users\mirza.baig> ## Specify the location where the output xml file has to
 be generated
PS C:\Users\mirza.baig> $outputXmlFilePath1="C:\FacultyDirectoryExport\FacultyDi
rectory1.xml"
PS C:\Users\mirza.baig> $outputXmlFilePath2="C:\FacultyDirectoryExport\FacultyDi
rectory2.xml"
PS C:\Users\mirza.baig> $outputXmlFilePath3="C:\FacultyDirectoryExport\Noteworth
y.xml"
PS C:\Users\mirza.baig> $outputXmlFilePath4="C:\FacultyDirectoryExport\FacultyPu
blications.xml"
PS C:\Users\mirza.baig> ## $viewName is a string that contains the GUID of the v
iew. If you give empty value it will take the values from default view
PS C:\Users\mirza.baig> ## $viewName1 = AllItems of FacultyDirectory1
PS C:\Users\mirza.baig> [string]$viewName1 = "{E2748E63-1F55-4C98-9D03-4342216D2
B10}"
PS C:\Users\mirza.baig> ## $viewName2 = AllItems of FacultyDirectory2
PS C:\Users\mirza.baig> [string]$viewName2 = "{ECD64C1B-D86B-4EFE-B60E-64042ECB2
14E}"
PS C:\Users\mirza.baig> ## $viewName3 = AllItems of Noteworthy
PS C:\Users\mirza.baig> [string]$viewName3 = "{63600BFE-C18A-4833-8143-B2F57DA3F
CAC}"
PS C:\Users\mirza.baig> ## $viewName4 = AllItems of Faculty Publications
PS C:\Users\mirza.baig> [string]$viewName4 = "{C4340954-5E7B-4ACA-9997-3CF370130
4C3}"
PS C:\Users\mirza.baig> [string]$rowLimit = "5000"
PS C:\Users\mirza.baig>
PS C:\Users\mirza.baig> [String]$viewFieldsValue=""
PS C:\Users\mirza.baig> [String]$queryValue=""
PS C:\Users\mirza.baig> [String]$queryOptionsValue=""
PS C:\Users\mirza.baig> [String]$null=""
PS C:\Users\mirza.baig>
PS C:\Users\mirza.baig> #--------- Get List Items Function ----------
PS C:\Users\mirza.baig> Function GetListItems()
>> {
>>
>>       $password = get-content "c:\PowerShell Scripts\mp.txt" | convertto-secu
restring
>>       $credential = New-Object -Typename System.Management.Automation.PSCrede
ntial -argumentlist "BROOKLAW\sp2010admin",$password
>>       $uri = $webURL1 + "/_vti_bin/Lists.asmx?wsdl"
>>       $uri2 = $webURL2 + "/_vti_bin/Lists.asmx?wsdl"
>>       $listsWebServiceReference = New-WebServiceProxy -Uri $uri -Credential $
credential
>>       $listsWebServiceReference2 = New-WebServiceProxy -Uri $uri2 -Credential
 $credential
>>       [System.Xml.XmlDocument]$xmlDoc = New-Object -TypeName System.Xml.XmlDo
cument
>>       [System.Xml.XmlElement]$query = $xmlDoc.CreateElement("Query")
>>       [System.Xml.XmlElement]$viewFields = $xmlDoc.CreateElement("ViewFields"
)
>>       [System.Xml.XmlElement]$queryOptions = $xmlDoc.CreateElement("QueryOpti
ons")
>>       $viewFields.InnerXml = $viewFieldsValue
>>       $query.InnerXml = $queryValue
>>       $queryOptions.InnerXml = $queryOptionsValue
>>       [System.Xml.XmlNode]$nodeListItems = $listsWebServiceReference.GetListI
tems($listName1, $viewName1, $query, $viewFields, $rowLimit, $queryOptions, $nul
l)
>>       $output = New-Object -TypeName System.IO.StreamWriter -ArgumentList $ou
tputXmlFilePath1, $false
>>       $output.WriteLine($nodeListItems.Outerxml)
>>       $output.WriteLine()
>>       $output.Dispose()
>>       Write-Host -ForegroundColor Red "Output file for"$listname1" is generat
ed in the path: "$outputXmlFilePath1
>>       [System.Xml.XmlDocument]$xmlDoc2 = New-Object -TypeName System.Xml.XmlD
ocument
>>       [System.Xml.XmlElement]$query2 = $xmlDoc2.CreateElement("Query")
>>       [System.Xml.XmlElement]$viewFields2 = $xmlDoc2.CreateElement("ViewField
s")
>>       [System.Xml.XmlElement]$queryOptions2 = $xmlDoc2.CreateElement("QueryOp
tions")
>>       $viewFields2.InnerXml = $viewFieldsValue
>>       $query2.InnerXml = $queryValue
>>       $queryOptions2.InnerXml = $queryOptionsValue
>>       [System.Xml.XmlNode]$nodeListItems2 = $listsWebServiceReference2.GetLis
tItems($listName2, $viewName2, $query2, $viewFields2, $rowLimit, $queryOptions2,
 $null)
>>       $output = New-Object -TypeName System.IO.StreamWriter -ArgumentList $ou
tputXmlFilePath2, $false
>>       $output.WriteLine($nodeListItems2.Outerxml)
>>       $output.WriteLine()
>>       $output.Dispose()
>>       Write-Host -ForegroundColor Red "Output file for"$listname2" is generat
ed in the path: "$outputXmlFilePath2
>>       [System.Xml.XmlDocument]$xmlDoc3 = New-Object -TypeName System.Xml.XmlD
ocument
>>       [System.Xml.XmlElement]$query3 = $xmlDoc3.CreateElement("Query")
>>       [System.Xml.XmlElement]$viewFields3 = $xmlDoc3.CreateElement("ViewField
s")
>>       [System.Xml.XmlElement]$queryOptions3 = $xmlDoc3.CreateElement("QueryOp
tions")
>>       $viewFields3.InnerXml = $viewFieldsValue
>>       $query3.InnerXml = $queryValue
>>       $queryOptions3.InnerXml = $queryOptionsValue
>>       [System.Xml.XmlNode]$nodeListItems3 = $listsWebServiceReference.GetList
Items($listName3, $viewName3, $query3, $viewFields3, $rowLimit, $queryOptions3,
$null)
>>       $output = New-Object -TypeName System.IO.StreamWriter -ArgumentList $ou
tputXmlFilePath3, $false
>>       $output.WriteLine($nodeListItems3.Outerxml)
>>       $output.WriteLine()
>>       $output.Dispose()
>>       Write-Host -ForegroundColor Red "Output file for"$listname3" is generat
ed in the path: "$outputXmlFilePath3
>>       [System.Xml.XmlDocument]$xmlDoc4 = New-Object -TypeName System.Xml.XmlD
ocument
>>       [System.Xml.XmlElement]$query4 = $xmlDoc4.CreateElement("Query")
>>       [System.Xml.XmlElement]$viewFields4 = $xmlDoc4.CreateElement("ViewField
s")
>>       [System.Xml.XmlElement]$queryOptions4 = $xmlDoc4.CreateElement("QueryOp
tions")
>>       $viewFields4.InnerXml = $viewFieldsValue
>>       $query4.InnerXml = $queryValue
>>       $queryOptions4.InnerXml = $queryOptionsValue
>>       [System.Xml.XmlNode]$nodeListItems4 = $listsWebServiceReference.GetList
Items($listName4, $viewName4, $query4, $viewFields4, $rowLimit, $queryOptions4,
$null)
>>       $output = New-Object -TypeName System.IO.StreamWriter -ArgumentList $ou
tputXmlFilePath4, $false
>>       $output.WriteLine($nodeListItems4.Outerxml)
>>       $output.WriteLine()
>>       $output.Dispose()
>>       Write-Host -ForegroundColor Red "Output file for"$listname4" is generat
ed in the path: "$outputXmlFilePath4
>> }
>> #--------- Calling the Function -----------
>> GetListItems
>>
ConvertTo-SecureString : Key not valid for use in specified state.
At line:3 char:86
+       $password = get-content "c:\PowerShell Scripts\mp.txt" | convertto-secu
restring <<<<
    + CategoryInfo          : InvalidArgument: (:) [ConvertTo-SecureString], C
   ryptographicException
    + FullyQualifiedErrorId : ImportSecureString_InvalidArgument_Cryptographic
   Error,Microsoft.PowerShell.Commands.ConvertToSecureStringCommand

New-Object : Exception calling ".ctor" with "2" argument(s): "Cannot process ar
gument because the value of argument "password" is null. Change the value of ar
gument "password" to a non-null value."
At line:4 char:31
+       $credential = New-Object <<<<  -Typename System.Management.Automation.P
SCredential -argumentlist "BROOKLAW\sp2010admin",$password
    + CategoryInfo          : InvalidOperation: (:) [New-Object], MethodInvoca
   tionException
    + FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.Power
   Shell.Commands.NewObjectCommand

Exception calling "GetListItems" with "7" argument(s): "The request failed with
 HTTP status 401: Unauthorized."
At line:16 char:82
+       [System.Xml.XmlNode]$nodeListItems = $listsWebServiceReference.GetListI
tems <<<< ($listName1, $viewName1, $query, $viewFields, $rowLimit, $queryOption
s, $null)
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException

Output file for Faculty Directory is generated in the path: C:\FacultyDirectoryE
xport\FacultyDirectory1.xml
Exception calling "GetListItems" with "7" argument(s): "The request failed with
 HTTP status 401: Unauthorized."
At line:29 char:84
+       [System.Xml.XmlNode]$nodeListItems2 = $listsWebServiceReference2.GetLis
tItems <<<< ($listName2, $viewName2, $query2, $viewFields2, $rowLimit, $queryOp
tions2, $null)
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException

Output file for Faculty Directory is generated in the path: C:\FacultyDirectoryE
xport\FacultyDirectory2.xml
Exception calling "GetListItems" with "7" argument(s): "The request failed with
 HTTP status 401: Unauthorized."
At line:42 char:83
+       [System.Xml.XmlNode]$nodeListItems3 = $listsWebServiceReference.GetList
Items <<<< ($listName3, $viewName3, $query3, $viewFields3, $rowLimit, $queryOpt
ions3, $null)
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException

Output file for Noteworthy is generated in the path: C:\FacultyDirectoryExport\N
oteworthy.xml
Exception calling "GetListItems" with "7" argument(s): "The request failed with
 HTTP status 401: Unauthorized."
At line:55 char:83
+       [System.Xml.XmlNode]$nodeListItems4 = $listsWebServiceReference.GetList
Items <<<< ($listName4, $viewName4, $query4, $viewFields4, $rowLimit, $queryOpt
ions4, $null)
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException

Output file for Faculty Publications is generated in the path: C:\FacultyDirecto
ryExport\FacultyPublications.xml
PS C:\Users\mirza.baig>
                                                       


June 17th, 2015 3:13pm

Hi,

Please make sure you have the required permission to run powershell on your On-premise environnment :

  • You must have membership in the securityadmin fixed server role on the SQL Server instance

  • You must have membership in the db_owner fixed database role on all databases that are to be updated.

  • You must be a member of the Administrators group on the server on which you are running the Windows PowerShell cmdlet.

Source : https://technet.microsoft.com/en-us/library/ee806878.aspx

Regards 

Samuel Levesque | http://sharepointerie.com | Please remember to click Mark as Answer on the post that helps you, and to click Unmark as Answer if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.

Free Windows Admin Tool Kit Click here and download it now
June 17th, 2015 8:21pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics