使用PowerShell更新Sharepoint列表

时间:2013-08-26 20:50:23

标签: sharepoint-2013 powercli

美好的一天。我使用的是名为PowerCLI 5.1的PowerShell变体。我使用它来更新包含VMware Virtual Center数据库中多个项目的多列SharePoint 2013列表。我正在使用的代码是:

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();
 }

但是,它似乎只用第一个VM属性填充我的整个列表,并忽略所有其他VM。现在我知道我的代码的第一部分是有效的,因为我可以使用我的所有VM及其属性正确更新Excel电子表格。

我不确定使用这些属性更新SharePoint列表时我做错了什么。任何帮助都会很感激,谢谢。

1 个答案:

答案 0 :(得分:0)

您实际上并没有遍历VM属性(您继续将相同的VM属性放入每个项目中)以下是我用于从一个列表到另一个列表的代码(与您相同的类型)。

#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()

}