zoukankan      html  css  js  c++  java
  • 【解决】根据GUID查找SharePoint对象的PowerShell脚本

    如何根据GUID查找一个SharePoint对象?下文的作者编写了一个PowerShell脚本来实现:
    http://sharepointinterface.com/2012/08/01/finding-a-guid-in-a-sharepoint-haystack/

    但如上脚本不对Field对象进行检索,本人对此脚本进行了一些完善,结果如下:

    <#
    .SYNOPSIS
       FindObjectByGuidInSite.ps1
    .DESCRIPTION
       This script attempts to locate a SharePoint object by its unique ID (GUID) within
       a site collection. The script first attempts to locate a match by examining webs;
       following webs, lists/libraries are examined. Finally, individual items within
       lists and libraries are examined. If an object with the ID is found, information
       about the object is reported back.
    .NOTES
       Original Author: Sean McDonough
       Revised By: Charles Zhang
       Last Revision: 15-May-2013
    .PARAMETER SiteUrl
       The URL of the site collection that will be searched
    .PARAMETER ObjectGuid
       The GUID that identifies the object to be located
    .EXAMPLE
       FindObjectByGuid -SiteUrl http://mysitecollection.com -ObjectGuid 91ce5bbf-eebb-4988-9964-79905576969c
    #>
    param
    (
        [string]$SiteUrl = "$(Read-Host 'The URL of the site collection to search [e.g. http://mysitecollection.com]')",
        [Guid]$ObjectGuid = "$(Read-Host 'The GUID of the object you are trying to find [e.g. 91ce5bbf-eebb-4988-9964-79905576969c]')"
    )
    
    function FindObject($startingUrl, $targetGuid)
    {
        # To work with SP2007, we need to go directly against the object model
        Add-Type -AssemblyName "Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c"
    
        # Grab the site collection and all webs associated with it to start
        $targetSite = New-Object Microsoft.SharePoint.SPSite($startingUrl)
        $matchObject = $false
        $itemsTotal = 0
        $fieldsTotal = 0
        $listsTotal = 0
        $searchStart = Get-Date
        $spWeb = $null
        $spList = $null
        $spField = $null
    
        Clear-Host
        Write-Host ("INITIATING SEARCH FOR GUID: {0}" -f $targetGuid)
    
        # Step 1: see if we can find a matching web.
        $allWebs = $targetSite.AllWebs
        Write-Host ("`nPhase 1: Examining all webs ({0} total)" -f $allWebs.Count)
        foreach ($spWeb in $allWebs)
        {
            $listsTotal += $spWeb.Lists.Count
            if ($spWeb.ID -eq $targetGuid)
            {
                Write-Host "`nMATCH FOUND: Web"
                Write-Host ("- Web Title: {0}" -f $spWeb.Title)
                Write-Host ("-   Web URL: {0}" -f $spWeb.Url)
                $matchObject = $true
                break
            }
            $spWeb.Dispose()
        }
    
        # If we don't yet have match, we'll continue with list iteration
        if ($matchObject -eq $false)
        {
            Write-Host ("Phase 2: Examining all lists and libraries ({0} total)" -f $listsTotal)
            $allWebs = $targetSite.AllWebs
            foreach ($spWeb in $allWebs)
            {
                $allLists = $spWeb.Lists
                foreach ($spList in $allLists)
                {
                    $fieldsTotal += $spList.Fields.Count
                    $itemsTotal += $spList.Items.Count
                    if ($spList.ID -eq $targetGuid)
                    {
                        Write-Host "`nMATCH FOUND: List/Library"
                        Write-Host ("-            List Title: {0}" -f $spList.Title)
                        Write-Host ("- List Default View URL: {0}" -f $spList.DefaultViewUrl)
                        Write-Host ("-      Parent Web Title: {0}" -f $spWeb.Title)
                        Write-Host ("-        Parent Web URL: {0}" -f $spWeb.Url)
                        $matchObject = $true
                        break
                    }
                }
                if ($matchObject -eq $true)
                {
                    break
                }
    
            }
            $spWeb.Dispose()
        }
    
        # If we    don't yet have match, we'll    continue with list fields
        if ($matchObject -eq $false)
        {
            Write-Host ("Phase 3: Examining all lists field ({0} total)" -f    $fieldsTotal)
            $allWebs = $targetSite.AllWebs
            foreach ($spWeb in $allWebs)
            {
                $allLists =    $spWeb.Lists
                foreach($spList    in $allLists)
                {
                    $allFields = $spList.Fields
                    foreach    ($spField in $allFields)
                    {
                        if ($spField.ID    -eq    $targetGuid)
                        {
                            $spList    = $spField.ParentList
                            Write-Host "`nMATCH    FOUND: Field"
                            Write-Host ("-            Field Name: {0}" -f $spField.Title)
                            Write-Host ("-            List Title: {0}" -f $spList.Title)
                            Write-Host ("- List    Default    View URL: {0}" -f $spList.DefaultViewUrl)
                            Write-Host ("-      Parent Web Title: {0}" -f $spWeb.Title)
                            Write-Host ("-        Parent Web URL: {0}" -f $spWeb.Url)
                            $matchObject = $true
                            break
                        }
                    }
    
                    if ($matchObject -eq $true)
                    {
                        break
                    }
                }
    
                if ($matchObject -eq $true)
                {
                    break
                }
            }
        }
    
        # No match yet? Look at list items (which includes folders)
        if ($matchObject -eq $false)
        {
            Write-Host ("Phase 4: Examining all list and library items ({0} total)" -f $itemsTotal)
            $allWebs = $targetSite.AllWebs
            foreach ($spWeb in $allWebs)
            {
                $allLists = $spWeb.Lists
                foreach ($spList in $allLists)
                {
                    try
                    {
                        $listItem = $spList.GetItemByUniqueId($targetGuid)
                    }
                    catch
                    {
                        $listItem = $null
                    }
                    if ($listItem -ne $null)
                    {
                        Write-Host "`nMATCH FOUND: List/Library Item"
                        Write-Host ("-                    Item Name: {0}" -f $listItem.Name)
                        Write-Host ("-                    Item Type: {0}" -f $listItem.FileSystemObjectType)
                        Write-Host ("-       Site-Relative Item URL: {0}" -f $listItem.Url)
                        Write-Host ("-            Parent List Title: {0}" -f $spList.Title)
                        Write-Host ("- Parent List Default View URL: {0}" -f $spList.DefaultViewUrl)
                        Write-Host ("-             Parent Web Title: {0}" -f $spWeb.Title)
                        Write-Host ("-               Parent Web URL: {0}" -f $spWeb.Url)
                        $matchObject = $true
                        break
                    }
                }
                if ($matchObject -eq $true)
                {
                    break
                }
    
            }
            $spWeb.Dispose()
        }
    
        # No match yet? Too bad; we're done.
        if ($matchObject -eq $false)
        {
            Write-Host ("`nNO MATCH FOUND FOR GUID: {0}" -f $targetGuid)
        }
    
        # Dispose of the site collection
        $targetSite.Dispose()
        Write-Host ("`nTotal seconds to execute search: {0}`n" -f ((Get-Date) - $searchStart).TotalSeconds)
    
        # Abort script processing in the event an exception occurs.
        trap
        {
            Write-Warning "`n*** Script execution aborting. See below for problem encountered during execution. ***"
            $_.Message
            Write-Host ("-            Field Name: {0}" -f $spField.Title)
            Write-Host ("-            List Title: {0}" -f $spList.Title)
            Write-Host ("-      Parent Web Title: {0}" -f $spWeb.Title)
            break
        }
    }
    
    # Launch script
    FindObject $SiteUrl $ObjectGuid
  • 相关阅读:
    Azure SQL Storage
    T-SQL quries
    映射盘符
    繁体及其输入法、乱码问题
    匈牙利命名法
    C++四种转换总结
    windows系统下进程间通信
    Qt 中文字符串问题
    PDB文件详解
    DbgView 无法开启Capture Kernel问题
  • 原文地址:https://www.cnblogs.com/jancco/p/3082264.html
Copyright © 2011-2022 走看看