Update a SharePoint List with PowerShell

Some time ago I wrote an introductary post on Powershell and SharePoint 2007. Based on that post I recently got a question on how to run a WMI query to determine the IP address of a server and place the result in an IP field of a custom SharePoint list.

WMI stands for Windows Management Instrumentation and is the management infrasturcture for the Windows operating system. It also has an object model that you can use within a .NET application, but also from within PowerShell. A good starting point for using WMI within Powershell is the following tutorial:

http://www.powershellpro.com/powershell-tutorial-introduction/powershell-tutorial-scripting/

To test my sample code of this blog post I created a custom list with name "Servers" and some additional fields like IPAddress and CPUSpeed. 

When opening the PowerShell command prompt, the first thing you have to do is load the SharePoint assembly:

[System.Reflection.Assembly]::LoadWithPartialName(”Microsoft.SharePoint”)

To get an instance of a certain SharePoint list, you could use Powershell code like the following: 

$siteUrl = "your sharepoint site url"

$webName = "your webname"

$spSite = new-object Microsoft.SharePoint.SPSite($siteurl)

$spWeb = $spSite.OpenWeb($webName)

$spList = $spWeb.Lists["Servers"]

For querying WMI with PowerShell you use the get-wmiobject method. To get information about the computer processor you need to query the WMI namespace Win32_Processor. This namespace returns information about architecture, availability, name, cpu status, processor speed, devices, manufactures, etc.

$computername = "your computer name"

$processor = get-wmiobject Win32_Processor -computername $computername

To get information about the IP Adress you need to query the WMI namespace Win32_NetworkAdapterConfiguration. This namespace returns information about DHCP, the IP Address, DNS domain and service name of the queried server. 

$configs = get-wmiobject Win32_NetworkAdapterConfiguration -computername $computername

As for the calculation of the CPU speed, you can use the CurrentClockSpeed or the MaxClockSpeed property of the processor instance. As the data type of these properties are both unsigned integers, it will need some formatting before storing it in the SharePoint list:

"{0:0.##}" -f ($processor.MaxClockSpeed/1000) + " GHz"

As the CPU speed is also part of the processor name, you can also strip it out from the $processor.Name string.

Adding a new item to a SharePoint list using PowerShell is not so different from using the SharePoint object model from within a .NET application:

$spitem = $spList.Items.Add()

$spitem["Title"] = $config.HostName

$spitem["IPAddress"] = $config.IPAddress

$spitem["CPUSpeed"] = $cpuspeed

$spitem.Update()

When using the above syntax, pay attention that you use the internal name of the field. F.e. if you see in the SharePoint list view a column with title "CPU Speed", chances are great that the internal name of this field is "CPU_x0020_Speed".

Now putting it all together, the sample code looks like the following. You can place it within a foreach loop to loop through a list of all servers.

$siteUrl = "http://yoursharepointsite"

$webName = "yourweb" 

$spSite = new-object Microsoft.SharePoint.SPSite($siteurl)

$spWeb = $spSite.OpenWeb($webName)

$listName = "your list name"

$spList = $spWeb.Lists[$listName]

$computername = "yourcomputername"

$processor = get-wmiobject Win32_Processor -computername $computername

$configs = get-wmiobject Win32_NetworkAdapterConfiguration -computername $computername

foreach ($config in $configs)

{

   $spitem = $spList.Items.Add()

   $spitem["Title"] = $config.ServiceName

   $ipaddress = $config.IPAddress | out-string

   $spitem["IPAddress"] = $config.IPAddress + "" 

   $spitem["CPUSpeed"] = "{0:0.##}" -f ($processor.MaxClockSpeed/1000) + " GHz"

   $spitem.Update()

}

 This is a screenshot of my test list after execution of previous code:

image

I hope this example is helpful. In case of questions, don't hesitate to post a comment.