简体   繁体   English

反序列化(SharePoint)XML到PSObject和性能

[英]De-serializing (SharePoint) XML to PSObject and Performance

Beloved SO'ers , 亲爱的SO'ERS

I have a server running Powershell to access SharePoint 2010 data via Web Services. 我有一台运行Powershell的服务器,可通过Web服务访问SharePoint 2010数据。 I have several PS Scripts doing business logic on SharePoint data. 我有几个PS脚本在SharePoint数据上执行业务逻辑。 While this script works, my issue is performance . 虽然此脚本有效,但我的问题是Performance

  • I'm omitting the annoying ows_ prefix 我省略了烦人的ows_前缀
  • I've considered using an XMLDocument and SelectNodes but remember, this is no telling what fields will be provided so this has to be dynamic. 我考虑过使用XMLDocumentSelectNodes但是请记住,这并不能说明将提供哪些字段,因此必须动态。
  • I've tried to access PSSerializer as this post recommends but I don't have it. 我已尝试按照该帖子的建议访问PSSerializer ,但我没有它。 Is it in PS 3.0? 在PS 3.0中吗?
  • I'm searching for a multi-value field and turning that into an SPFieldLookupValueCollection 我正在寻找一个多值字段并将其转换为SPFieldLookupValueCollection
  • The performance hit appears to be taking place during iteration of matches2.Matches 性能matches2.Matches 似乎发生在matches2.Matches迭代过程中。

It took around 45 seconds to process 800 items and I believe this method could be improved. 处理800个项目大约需要45秒 ,我相信这种方法可以改进。

SP2010XmlToPSObject.ps1 SP2010XmlToPSObject.ps1

param ([string]$xml)

#Handle empty result set
if($xml.IndexOf("ItemCount='0'") -ge 0){return @()}

#Load SharePoint DLL
$a = [Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")

$rows = @()
$matches = $xml | Select-String "(<z:row(?:.|\n|\r)*?(?:/>))" -AllMatches

$matches.Matches | %{
      $obj = New-Object PSObject
      $matches2 = $_.Value | Select-String "(?:ows_)(\w*)(?:=)(?:`")((.*?)(?:`"))" -AllMatches
      $matches2.Matches | % {
            #handle multi-value fields
            if($_.Groups[3].Value.IndexOf(";#") -gt -1){
                  #$a = @()
                  $o = New-Object Microsoft.SharePoint.SPFieldLookupValueCollection $_.Groups[3].Value
                  $obj | Add-Member NoteProperty $_.Groups[1].Value $o
            }
            else{
                  $obj | Add-Member NoteProperty $_.Groups[1].Value $_.Groups[3].Value
            }
      }

      $rows += $obj
}

return $rows

ps Sorry, looks like SO syntax highlighter didn't like powershell's escape (`) char. ps抱歉,看起来SO语法突出显示不喜欢powershell的转义(`)char。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM