Answered by:
Remote Powershell For Non Farm Admins

Question
-
We have a group of users that want to utilize remote power shell to update a list in SharePoint. These users are not Farm admins and I would like to keep it that way if possible. I have setup the rights for them to remote PowerShell to the box and they can load the command-lets but are getting the error when trying to run
$web = Get-SPWeb http://teams.company.com/sites/home
Cannot access the local farm. Verify that the local farm is properly configured, currently available, and that you have the appropriate permissions to access the database before trying again.
I have found this information on the web but am weary of giving any rights to the config db.
$dbs = Get-SPDatabase; foreach($d in $dbs) {If($d.name.contains("SharePoint_Config")){Add-SPShellAdmin "domain\USER" -database $d}}
If you have had this issue I would greatly appreciate any feedback.
Also these are the commands they will be running. I am thinking that only rights to the site at this point will be needed which they have.
#Get the Contacts List
$List = $web.Lists["Contacts"]
#Get All Items by update its title
$ListItems = $List.items
#Iterate through each item
foreach($Item in $ListItems)
{
#Update the value of the "Title" column
$item["Title"] = "Title Updated!"
#Update the item
$item.Update()
}
Tuesday, December 19, 2017 9:07 PM
Answers
-
Which column contains a unique value you could filter on? You can add a CAML query to the Get-PnPListItem and then pipe the results to a set.
Get-PnPListItem -List Products -Query "<View><Query><Where><Eq><FieldRef Name='Title'/><Value Type='text'>Widget</Value></Eq></Where></Query></View>"
Example:
$listitems = Get-PnPListItem -List Products -Query "<View><Query><Where><Eq><FieldRef Name='Title'/><Value Type='text'>Widget</Value></Eq></Where></Query></View>" $listitems | foreach { Set-PnPListItem -List Products -Identity $_.id -values @{"Title" = $_.FieldValues["Title"] + " Title updated"} }
There are several CAML Query builders on the web. Here's one:
Mike Smith TechTrainingNotes.blogspot.com
Books: SharePoint 2007 2010 Customization for the Site Owner, SharePoint 2010 Security for the Site Owner- Marked as answer by kaylex22 Thursday, December 21, 2017 7:56 PM
Thursday, December 21, 2017 5:07 PM -
- Marked as answer by kaylex22 Thursday, December 21, 2017 7:56 PM
Thursday, December 21, 2017 5:09 PM
All replies
-
They're executing SSOM. Nearly all SSOM PoSh cmdlets perform either a Local Admin or Farm Admin check before execution. I'd suggest coming up with another method to allow them to do this. They could use CSOM to do this without elevated rights and without remote PowerShell.
https://docs.microsoft.com/en-us/sharepoint/dev/sp-add-ins/complete-basic-operations-using-sharepoint-client-library-code#BasicOps_SPListItemTasks
Trevor Seward
Office Servers and Services MVP
Author, Deploying SharePoint 2016
This post is my own opinion and does not necessarily reflect the opinion or view of Microsoft, its employees, or other MVPs.- Proposed as answer by Dean_Wang Wednesday, December 20, 2017 2:44 AM
Tuesday, December 19, 2017 10:24 PM -
Take a look at the SharePoint Patterns and Practices PowerShell project. It uses CSOM/web services and can be used from client machines to both SharePoint Online and SharePoint On Prem.
https://docs.microsoft.com/en-us/powershell/sharepoint/sharepoint-pnp/sharepoint-pnp-cmdlets
https://dev.office.com/patterns-and-practices
Mike Smith TechTrainingNotes.blogspot.com
Books: SharePoint 2007 2010 Customization for the Site Owner, SharePoint 2010 Security for the Site OwnerWednesday, December 20, 2017 1:47 AM -
I would give them the SharePoint Patterns and Practices PowerShell library.
https://msdn.microsoft.com/en-us/pnp_powershell/pnp-powershell-overview
- Edited by monkeymaj1k Wednesday, December 20, 2017 2:20 AM
Wednesday, December 20, 2017 2:19 AM -
PNP syntax is not the same as server side PowerShell. Here's an example similar to yours.
Connect-PnPOnline -Url http://sp2016/sites/training $listitems = Get-PnPListItem lists/Contacts $listitems | foreach { Set-PnPListItem -List $list -Identity $_.id -values @{"Title" = $_.FieldValues["Title"] + " Title updated"} }
Mike Smith TechTrainingNotes.blogspot.com
Books: SharePoint 2007 2010 Customization for the Site Owner, SharePoint 2010 Security for the Site Owner- Proposed as answer by Dean_Wang Wednesday, December 20, 2017 2:44 AM
Wednesday, December 20, 2017 2:27 AM -
Hi Mike,
Might be easier if I explain what I am trying to do.
I have a list that has about 10 columns. The first column is the name of the item and the last column empty with a content type of single valued text. What they want to be able to do is to have a command that they can put in the value of the first column and then have the last update to yes or no.
I have tried this
$listitems = Get-PnPListItem lists/"Server Details"
Set-PnPListItem -List "Server Details" -Identity column1item -Values @{“column10” = “Yes”}
but get an error of
Set-PnPListItem : Object reference not set to an instance of an object.Wednesday, December 20, 2017 6:50 PM -
Try the quotes around the entire path:
$listitems = Get-PnPListItem "lists/Server Details"
Are you setting $column1item to a list item ID? I.e. does this work?
Set-PnPListItem -List "Server Details" -Identity 1 -Values @{"column10" = "Yes"}
Is "column10" capitalized correctly?
Mike Smith TechTrainingNotes.blogspot.com
Books: SharePoint 2007 2010 Customization for the Site Owner, SharePoint 2010 Security for the Site Owner- Edited by Mike Smith MCT MVPMVP Wednesday, December 20, 2017 7:56 PM
Wednesday, December 20, 2017 6:59 PM -
excuse my ignorance on this Mike,
I thought the -identity was for the item in the list not the column itself. So the users has a list of about 500 items with different names in the first column. they what to programmatic update one of the list items with an entry in the last column.
The first column is labeled "Server name" and the last column is labeled "decommissioned"
so the process would be 1. they delete a server 2.run the script to automatically add a yes to that last column.
I hope I am making sense.Thursday, December 21, 2017 12:44 PM -
Sorry I wanted to let you know I am closer
Running this works
$listitems = Get-PnPListItem "lists/Server Details"
Set-PnPListItem -List "Server Details" -Identity 1 -Values @{"column10" = "Yes"}
But I am wanting to not have to know the ID all the time as there are hundreds of items but rather queue of of and entry in one of the columns (it is a server name)
thoughts.
Thursday, December 21, 2017 2:08 PM -
Which column contains a unique value you could filter on? You can add a CAML query to the Get-PnPListItem and then pipe the results to a set.
Get-PnPListItem -List Products -Query "<View><Query><Where><Eq><FieldRef Name='Title'/><Value Type='text'>Widget</Value></Eq></Where></Query></View>"
Example:
$listitems = Get-PnPListItem -List Products -Query "<View><Query><Where><Eq><FieldRef Name='Title'/><Value Type='text'>Widget</Value></Eq></Where></Query></View>" $listitems | foreach { Set-PnPListItem -List Products -Identity $_.id -values @{"Title" = $_.FieldValues["Title"] + " Title updated"} }
There are several CAML Query builders on the web. Here's one:
Mike Smith TechTrainingNotes.blogspot.com
Books: SharePoint 2007 2010 Customization for the Site Owner, SharePoint 2010 Security for the Site Owner- Marked as answer by kaylex22 Thursday, December 21, 2017 7:56 PM
Thursday, December 21, 2017 5:07 PM -
- Marked as answer by kaylex22 Thursday, December 21, 2017 7:56 PM
Thursday, December 21, 2017 5:09 PM -
I will give it a shot thank you very muchThursday, December 21, 2017 7:56 PM