【问题标题】:Querying SharePoint XML data by GUID in PowerShell在 PowerShell 中按 GUID 查询 SharePoint XML 数据
【发布时间】:2012-01-09 02:07:15
【问题描述】:

我有一个很大的 XML 文件。它包含我的 SharePoint 2010 迁移检查日志(我们正在为 07 到 10 的迁移做好准备)。我对显示所有缺少的 web 部件所在位置的部分感兴趣,特别是它们的 URL。我一直在尝试使用每个被 PS SP10 工具发现有问题的 guid 来识别关联的站点 URL。我对 Powershell 比较陌生,想知道是否有一种简单的方法来获取数据?

get-childitem R:\testfile.xml | select-string -pattern "c7843aae-4c86-8206-0125-d00117cb461c"

返回每个实例,例如: 测试文件.xml:112644: 但是,我需要与 webpart 关联的 URL。

$xml.databases.Database[1].Site[0].Webs.Web

(这与 testfile 相同) 是存储此信息的级别(给我 ID、URL、LanguageId、TemplateName、TemplateId、Features、EventRecieverAssemblies、WebParts、CustomListViews、SetupFiles)。

有没有办法组合这些命令?我只需要找到该 guid 的每个实例及其相关网站的 URL。感谢您的帮助,我是 PowerShell 的新手,我认为它可能可以帮助我完成此文档,而无需手动操作(该 gui 的实例显示了大约 85 次)。

非常感谢。还有,长期潜伏者,第一次发帖。

尼克

    <Databases>
  <Database SiteCount="2" Name="WSS_Content" DataSource="xxx">
    <Site Id="ef5d110e-3562-4e4f-bdf4-0ee688e92701" OwnerLogin="xxxx" InSiteMap="True">
      <Webs Count="1">
        <Web Id="eb0591b4-0ccd-4834-8f97-abe9974b8a32" Url="site/xxxxxxa" LanguageId="1033" TemplateName="xxx#0" TemplateId="10001">
          <Features>
            <Feature Id="00bfea71-c796-4402-9f2f-0eb9a6e71b18" Count="1" DisplayName="WebPageLibrary" InstallPath="C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\Template\Features\WebPageLibrary" Status="Installed" />
          </Features>
          <EventReceiverAssemblies>
            <EventReceiverAssembly Name="Microsoft.Office.InfoPath.Server, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" Status="Installed" />
          </EventReceiverAssemblies>
          <WebParts>
            <WebPart Id="ce9aa113-48cf-ddee-0c03-597445e5b7ab" Count="1" Status="Installed" Type="Microsoft.SharePoint.WebPartPages.ImageWebPart" />
            <WebPart Id="293e8d0e-486f-e21e-40e3-75bfb77202de" Count="35" Status="Installed" Type="Microsoft.SharePoint.WebPartPages.ListFormWebPart" />
            <WebPart Id="2242cce6-491a-657a-c8ee-b10a2a993eda" Count="35" Status="Installed" Type="Microsoft.SharePoint.WebPartPages.ListViewWebPart" />
          </WebParts>
          <CustomListViews />
          <SetupFiles>
            <SetupFile Path="C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\Template\1033\STS\doctemp\word\wdtmpl.doc" Count="3" Status="Installed" />
            <SetupFile Path="C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\Template\Features\BasicWebParts\MSContentEditor.dwp" Count="1" Status="Installed" />
          </SetupFiles>
        </Web>
      </Webs>
    </Site>
    <Site Id="389fa2ac-4b8a-4704-ad98-e49619c981a0" OwnerLogin="xxx\srv-xxx" InSiteMap="True">
      <Webs Count="2">
        <Web Id="3820015a-56cf-41b8-9607-bbed7775f514" Url="/site/xxxxx" LanguageId="1033" TemplateName="OSRV#0" TemplateId="40">
          <Features>
            <Feature Id="00bfea71-5932-4f9c-ad71-1557e5751100" Count="1" DisplayName="IssuesList" InstallPath="C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\Template\Features\IssuesList" Status="Installed" />
          </Features>
          <EventReceiverAssemblies>
            <EventReceiverAssembly Name="Microsoft.Office.InfoPath.Server, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" Status="Installed" />
            <EventReceiverAssembly Name="Microsoft.Office.Server, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" Status="Installed" />
          </EventReceiverAssemblies>
          <WebParts>
            <WebPart Id="d55b3b6b-6281-707b-73d0-0c49581475ad" Count="1" Status="Installed" Type="Microsoft.Office.Server.Search.WebControls.SearchStatsWebPart" />
            <WebPart Id="6172b769-e922-1e5e-b706-1073173cbce3" Count="1" Status="Installed" Type="Microsoft.Office.Server.Search.WebControls.CompletedCrawls" />
            <WebPart Id="bc0158dd-a329-57ce-3563-1afacb17ec6c" Count="1" Status="Installed" Type="Microsoft.Office.Server.Search.WebControls.SystemStatus" />
            <WebPart Id="f5c3ff60-e752-3a90-84f8-3677f8384e2d" Count="2" Status="Installed" Type="Microsoft.Office.Server.Search.WebControls.CoreResultsWebPart" />
            <WebPart Id="f2c50a02-9894-4ace-bb3f-4146a24cd940" Count="2" Status="Installed" Type="Microsoft.Office.Server.Search.WebControls.SearchPagingWebPart" />
            <WebPart Id="c54a0f4e-d855-ad45-ef85-4e67abf15a4d" Count="1" Status="Installed" Type="Microsoft.Office.Server.Search.WebControls.ActiveCrawls" />
            <WebPart Id="e60f6c95-e86c-4717-2c0d-6d8563c9caf7" Count="1" Status="Installed" Type="Microsoft.SharePoint.WebPartPages.ContentEditorWebPart" />
            <WebPart Id="293e8d0e-486f-e21e-40e3-75bfb77202de" Count="29" Status="Installed" Type="Microsoft.SharePoint.WebPartPages.ListFormWebPart" />
            <WebPart Id="2242cce6-491a-657a-c8ee-b10a2a993eda" Count="19" Status="Installed" Type="Microsoft.SharePoint.WebPartPages.ListViewWebPart" />
            <WebPart Id="74bd016c-baa0-14a8-d5d8-b75dc7e6f429" Count="1" Status="Installed" Type="Microsoft.SharePoint.Portal.WebControls.ContactFieldControl" />
            <WebPart Id="fb35a198-aea0-3c26-e40c-df473fe9b07b" Count="2" Status="Installed" Type="Microsoft.Office.Server.Search.WebControls.HighConfidenceWebPart" />
            <WebPart Id="669602d9-e116-ccb8-eea3-e37ad589b14b" Count="1" Status="Installed" Type="Microsoft.Office.Server.Search.WebControls.SearchSummaryWebPart" />
            <WebPart Id="f5897322-ddd4-c990-d012-f9d4fe2180ad" Count="2" Status="Installed" Type="Microsoft.SharePoint.Portal.WebControls.SearchBoxEx" />
          </WebParts>
          <CustomListViews />
          <SetupFiles>
            <SetupFile Path="C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\Template\1033\STS\doctemp\word\wdtmpl.doc" Count="2" Status="Installed" />
          </SetupFiles>
        </Web>
        <Web Id="d2e7ca33-fc74-4907-a34f-4c5ae1dbde84" Url="/ssp/admin/xxxxa" LanguageId="1033" TemplateName="PROFILES#0" TemplateId="51">
          <Features>
            <Feature Id="00bfea71-c796-4402-9f2f-0eb9a6e71b18" Count="1" DisplayName="WebPageLibrary" InstallPath="C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\Template\Features\WebPageLibrary" Status="Installed" />
          </Features>
          <EventReceiverAssemblies />
          <WebParts>
            <WebPart Id="293e8d0e-486f-e21e-40e3-75bfb77202de" Count="6" Status="Installed" Type="Microsoft.SharePoint.WebPartPages.ListFormWebPart" />
            <WebPart Id="2242cce6-491a-657a-c8ee-b10a2a993eda" Count="5" Status="Installed" Type="Microsoft.SharePoint.WebPartPages.ListViewWebPart" />
            <WebPart Id="f6bfd4dd-e6b5-7cb0-e080-e7674fcdd856" Count="1" Status="Installed" Type="Microsoft.SharePoint.Portal.WebControls.BusinessDataDetailsWebPart" />
          </WebParts>
          <CustomListViews />
          <SetupFiles>
            <SetupFile Path="C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\Template\1033\STS\doctemp\word\wdtmpl.doc" Count="1" Status="Installed" />
          </SetupFiles>
        </Web>
      </Webs>
    </Site>
  </Database>

【问题讨论】:

  • 你能举一个你正在使用的xml的例子吗?
  • 当然这是 xml &lt;Databases&gt; &lt;Database&gt; &lt;Site&gt; &lt;Webs&gt; &lt;Web&gt; (this level contains the site URL) &lt;Features&gt; &lt;Feature&gt; &lt;WebParts&gt; &lt;WebPart&gt; (this level has the ID used to Identify the part) 的文件结构如果你想看到实际代码让我知道,我必须在某个地方有一个文件主机。感谢您的帮助
  • 编辑您的问题并附加一个具体的 xml,可能来自 testfile.xml (不像您上面的内容)。相关代码也会有所帮助
  • 添加了一些testfile.xml,编辑了一些信息以适应这里。到目前为止,谢谢。

标签: xml sharepoint powershell migration


【解决方案1】:

你可以这样做:

$xml.SelectNodes("//Web") | %{ 
    if($_.SelectNodes("./WebParts/WebPart") | ?{ $_.id -eq "f6bfd4dd-e6b5-7cb0-e080-e7674fcdd856" } ){
        $_.Url
    }
}

在这里,我正在寻找 guid f6bfd4dd-e6b5-7cb0-e080-e7674fcdd856,它返回 /ssp/admin/xxxxa,即 Web 的 URL

【讨论】:

  • 哇,效果很好。太感谢了。真的让我很开心。
  • 是否也可以抓取计数?我已经使用您的代码尝试过这个,但似乎无法同时抓住它们。再次感谢您!
【解决方案2】:

你可以使用这个代理功能

Function Get-Content {
<#
***************************************************************************

 --------------------8<-----------------------------------------
    Ajout de deux nouveaux paramètres pour simplifier
    le traitement des fichiers XML: [-AsXml] [-XPath <String>]

    Attention: l'expression Xpath est sEnSiBlE à La cAsSe

                                  Walid toumi             
 --------------------8<-----------------------------------------

 PS> # Exemples d'utilisation:

 PS> $file = "$PSHOME\types.ps1xml"

 PS> $u = cat $file -As | Select-Xml -XP "//ScriptProperty" | Select -Expand Node
 PS> $u

 PS> $Xml = Get-Content $file -AsXml
 PS> $Xml.Types.Type[1..10]

 PS> Get-Content $file -AsXml -XPath "//Type[contains(Name,'Xml')]/Name"

 **************************************************************************

.ForwardHelpTargetName Get-Content
.ForwardHelpCategory Cmdlet

#>

[CmdletBinding(DefaultParameterSetName='Path', SupportsTransactions=$true)]
param(
    [Parameter(ValueFromPipelineByPropertyName=$true)]
    [System.Int64]
    ${ReadCount},

    [Parameter(ValueFromPipelineByPropertyName=$true)]
    [System.Int64]
    ${TotalCount},

    [Parameter(ParameterSetName='Path', Mandatory=$true, Position=0, ValueFromPipelineByPropertyName=$true)]
    [System.String[]]
    ${Path},

    [Parameter(ParameterSetName='LiteralPath', Mandatory=$true, Position=0, ValueFromPipelineByPropertyName=$true)]
    [Alias('PSPath')]
    [System.String[]]
    ${LiteralPath},

    [System.String]
    ${Filter},

    [System.String[]]
    ${Include},

    [System.String[]]
    ${Exclude},

    [System.String]
    ${XPath},

    [Switch]
    ${Force},

    [Switch]
    ${AsXml},

    [Parameter(ValueFromPipelineByPropertyName=$true)]
    [System.Management.Automation.PSCredential]
    ${Credential})

begin
{
    try {
        $outBuffer = $null
        if ($PSBoundParameters.TryGetValue('OutBuffer', [ref]$outBuffer))
        {
            $PSBoundParameters['OutBuffer'] = 1
        }
        $wrappedCmd = $ExecutionContext.InvokeCommand.GetCommand('Microsoft.PowerShell.Management\Get-Content', [System.Management.Automation.CommandTypes]::Cmdlet)
        $cmd = ''
         if($AsXml) {
          [void]$PSBoundParameters.Remove('AsXml')
          $cmd += ' | ForEach-Object {$fx=@()} {$fx+=$_} {$fx -as [Xml]}'
            if($XPath) {
               [void]$PSBoundParameters.Remove('XPath')
               $cmd += ' | Select-Xml -XPath $XPath | Select -expand Node'
            }
        } 
        $ScriptCmd = [ScriptBlock]::Create(
           { & $wrappedCmd @PSBoundParameters }.ToString() + $Cmd
          )
        $steppablePipeline = $scriptCmd.GetSteppablePipeline($myInvocation.CommandOrigin)
        $steppablePipeline.Begin($PSCmdlet)
    } catch {
        throw
    }
}

process
{
    try {
        $steppablePipeline.Process($_)
    } catch {
        throw
    }
}

end
{
    try {
        $steppablePipeline.End()
    } catch {
        throw
    }
}

}

像这样:

$file = "file.xml"
Get-Content $file -AsXml -XPath "//Web[WebParts/WebPart//@*[1][contains(.,'f6bfd4dd-e6b5-7cb0-e080-e7674fcdd856')]]/@*[2]"

或者干脆

Select-Xml -Path $file -XPath '//Web[WebParts/WebPart/@Id = "f6bfd4dd-e6b5-7cb0-e080-e7674fcdd856"]/@Url' | % { $_.node.'#text' }

【讨论】:

  • 谢谢!很好奇,如果我也想获取关联的计数属性,那么我会取回 URL 和计数,我该怎么做呢?我发现 XML 和 powershell 很难学习。非常感谢你们的时间和帮助
猜你喜欢
  • 1970-01-01
  • 2018-06-19
  • 2017-07-19
  • 2019-07-21
  • 1970-01-01
  • 1970-01-01
  • 2014-12-16
  • 1970-01-01
  • 2018-01-26
相关资源
最近更新 更多