3

So we have an inventory list with details about each item, mostly drop down menus and check boxes, some comments and descriptions. These records are in SharePoint. Sometimes we need to update multiple items in there in addition to a large number of other steps and I am trying to automate most of these steps including the updates to their SharePoint record. What is the best way to go about this in PowerShell from any remote computer?

Would I connect to the database, find the record and and update the record there? Is there an easier way? I tried finding PowerShell CLI tools for SharePoint but I don't see them available anywhere.

For example, I might want to update this field here:

enter image description here

EDIT:

Here's the code I've tried but it does nothing at all for some reason, not even an error which means it's finding the list but... It's returning 0 items found for whatever reason.

class Program
{
    static void Main(string[] args)
    {
        // Get the context
        ClientContext context = new ClientContext("http://sp.mydomain.com/sites/ts/it/ITG");
        // Get the list
        List recordsList = context.Web.Lists.GetByTitle("ServerInventory_Official-List");
        // Get specific item by id number
        CamlQuery query = CamlQuery.CreateAllItemsQuery();
        ListItemCollection items = recordsList.GetItems(query);
        context.Load(items);
        context.ExecuteQuery();
        Console.WriteLine("Number of items: " + items.Count);
        foreach (ListItem listItem in items)
        {
            Console.WriteLine(listItem["HostName"]);
            if (listItem["HostName"].ToString() == "myserverhostname")
            {
                listItem["ActiveFlag"] = "Decommissioned";
                Console.WriteLine("This Happened");
            }
        }

    }
}
2

Here's a basic approach. This could be made more robust by passing variables to -ArgumentList in the Invoke-Command and then setting variables inside the script.


Invoke-Command -ComputerName SEVERNAME -Verbose -ScriptBlock {
  $VerbosePreference='Continue'

  if((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null)
  {
    Add-PSSnapin Microsoft.SharePoint.PowerShell -Verbose
  }

  $Web = Get-SPWeb http://your.sp.web.url
  $List = $Web.Lists.TryGetList("LISTNAME")
  # By ID
  $ListItem = $List.GetItemByUniqueId("UNIQUEID")
  # By Title - Or any field as long as it is unique - if not unique, handle with a loop
  # $ListItem = $List.Items | ? {$_["Title"] -eq "UNIQUE TITLE"}

  $ListItem["ActiveFlag"] = "Decommissioned"

  $ListItem.Update()
}

Example with args


$FlagValue = "Decommissioned"
$IdsToUpdate = "10","13","11"
Invoke-Command -ComputerName SEVERNAME -ArgumentList $FlagValue,$IdsToUpdate -Verbose -ScriptBlock {
  $FlagValue = $args[0]
  $IdsToUpdate = $args[1]

  $VerbosePreference='Continue'

  if((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null)
  {
    Add-PSSnapin Microsoft.SharePoint.PowerShell -Verbose
  }

  $Web = Get-SPWeb http://your.sp.web.url
  $List = $Web.Lists.TryGetList("LISTNAME")

  foreach($Id in $IdsToUpdate)
  {
    $ListItem = $List.GetItemByUniqueId("UNIQUEID")
    $ListItem["ActiveFlag"] = $FlagValue
    $ListItem.Update()
  }
}

You could easily pass in list names and other variables that you might get outside of the scriptblock itself.

Hope this helps.

If you don't have accesss to the SharePoint Servers, this same thing can be accomplished with CSOM in a Console Application.


// Get the context
ClientContext context = new ClientContext("http://your.sp.web.url");
// Get the list
List recordsList = context.Web.Lists.GetByTitle("LISTNAME");
// Get specific item by id number
ListItem item = recordsList.GetItemById(ID);
// Change item & Update
item["ActiveFlag"] = "Decommissioned";
item.Update();
// ExecuteQuery
context.ExecuteQuery();

You can build a loop similar to the one in the powershell above. You may want to look at PnP materials for more ideas.

Ref: Basic Operations Using CSOM

  • You should not mess with the database directly, unless you like being in an unsupported (by Microsoft) state. – Ken Hansen Apr 2 '18 at 16:48
  • I wanna try this, the thing is I can't seem to add or get any powershell snapins. It can't find cmdlets called Microsoft.SharePoint.PowerShell on my machine. How does one install them on a machine that is not an SP server? – Nathan McKaskle Apr 2 '18 at 19:53
  • You can't. That is why this is run inside Invoke-Command. It opens a remote session on the server and runs the commands there. You need to have access to the SharePoint Server and have to have PowerShell rights in order to work with SharePoint PowerShell. – Ken Hansen Apr 3 '18 at 13:26
  • I added a CSOM example, Javascript or REST could do this too. – Ken Hansen Apr 3 '18 at 13:43
  • Wow you think by now they'd have some powershell cmdlets that work on any computer, like Citrix, Nutanix and VMWare have all done. Thanks for the help, I'll try REST if the invoke doesn't work. – Nathan McKaskle Apr 4 '18 at 12:12

Your Answer

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

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