Using powershell to update a Sharepoint list

5.5k Views Asked by At

Good day. I am using a variant of PowerShell called PowerCLI 5.1. I am using this to update a multi-column SharePoint 2013 List with multiple items from a VMware Virtual Center database. The code that I'm using is this:

Add-PSSnapin Microsoft.SharePoint.PowerShell
$viservers = "MyServer"
ForEach ($singleViserver in $viservers)

{
   Connect-VIServer $singleViserver -User domainuser -Password accountpassword

    $HostReport = @()
    Get-Datacenter -Name NYDC | Get-VM |Get-View| %{
    $Report = "" | select Name, VMos, IP, NumCPU, MemoryMB, FQDN, Status, Admin1, Admin2
    $Report.Name =$_.Name
    $Report.VMos =$_.Summary.Config.GuestFullName
    $Report.IP =$_.Guest.IPAddress
    $Report.NumCPU =$_.Config.Hardware.NumCPU
    $Report.MemoryMB =$_.Config.Hardware.MemoryMB
    $Report.FQDN =$_.Guest.HostName
    $Report.Admin1 = (Get-VIObjectByVIView $_).CustomFields["Admin1"]
    $Report.Admin2 = (Get-VIObjectByVIView $_).CustomFields["Admin2"]
    $HostReport += $Report
  }
}   

$web = Get-SPWeb http://mysharepointsite
$list = $web.Lists["MYVMList"]
foreach ($item in $list.Items)

 {
   $item["Name"] = $Report.Name;
   $item["Guest_OS"] = $Report.VMos;
   $item["Memory_Size"] = $Report.MemoryMB;
   $item["CPU_Count"] = $Report.NumCPU;
   $item["IP_Address"] = $Report.IP;
   $item["FQDN"] = $Report.FQDN;
   $item["Admin1"] = $Report.Admin1;
   $item["Admin2"] = $Report.Admin2;
   $item.Update();
 }

However, it seems to populate my entire list with only the first VM properties and ignores all the other VM's. Now I know that the first part of my code works because I can properly update an Excel spreadsheet with all my VM's and their properties.

I'm not sure what I did wrong with updating the SharePoint list with those properties. Any help would be appreciate, thank you.

1

There are 1 best solutions below

0
On

You are not actually iterating through the VM properties (you keep putting the same VM property into each item) Here is the code I used to get from one list to another list (kind of the same as you).

#Check for snappin
if((Get-PSSnapin | Where {$_.Name -eq "Microsoft.SharePoint.PowerShell"}) -eq $null) {Add-PSSnapin Microsoft.SharePoint.PowerShell;}

#array to hold key value
$tests = @()

#web items are going to
$web = Get-SPWeb "https://share.site/departments/it"

$Global:list = $web.Lists["locations"]

#web items come from
$sourceWebURL = "https://share.site"
$sourceListName = "locations"
$spSourceWeb = Get-SPWeb $sourceWebURL
$Global:spSourceList = $spSourceWeb.Lists[$sourceListName] 

$spSourceItems = $spSourceList.Items 
$spSourceItems | % {
    $tests += $_["Acronym"] #key value used in both lists
}


foreach ($test in $tests) {
    $mainItem = $Global:spSourceList.Items | where {$_['Acronym'] -like $test}
    $newItem = $Global:list.Items | where {$_['Acronym'] -like $test}

    $newItem["Short Name"] = $mainItem["Short Name"] #do this for all item updates
    $newItem.Update()

}