Get-SPWeb无法找到具有ID或URL的对象

时间:2018-09-12 18:51:47

标签: powershell sharepoint sharepoint-2010 powershell-v2.0

问题:

即使存在SharePoint网站以及被调用的文档库,我似乎也无法弄清楚为什么出现以下错误。文档库Lib1有文档/文件夹,文档库Lib2为空。有人知道出了什么问题吗?


错误:

Get-SPWeb : Cannot find an SPWeb object with Id or Url : Lib1 and site Url https://sharepoint.oshirowanen.com/sites/oshirodev.
At C:\Users\Oshiro\Desktop\CopyDocs.ps1:31 char:23
+       $web = Get-SPWeb <<<<  -Site $site -Identity $identity -ErrorAction Stop;
    + CategoryInfo          : InvalidData: (Microsoft.Share....SPCmdletGetWeb:SPCmdletGetWeb) [Get-SPWeb], SPCmdletPipeBindException
    + FullyQualifiedErrorId : Microsoft.SharePoint.PowerShell.SPCmdletGetWeb

脚本:

01  Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
02  
03  function Get-SPList([uri]$url) {
04    # Nullify variables $site, $web and $list
05    $site = $web = $list = $null
06  
07    # Get site collection
08    $site = New-Object -TypeName Microsoft.SharePoint.SPSite -ArgumentList $([Microsoft.SharePoint.Utilities.SPEncode]::UrlDecodeAsUrl($url.AbsoluteUri));
09  
10    # Get site-relative URL
11    $webURL = ([Microsoft.SharePoint.Utilities.SPEncode]::UrlDecodeAsUrl($url.AbsoluteUri)) -replace $site.Url;
12  
13    # Remove query information if included
14    if (-not [string]::IsNullOrEmpty($url.Query)) {
15      $webURL = $webURL.Replace($url.Query, [string]::Empty);
16    }
17  
18    # Process the array of segments backwards,
19    # removing segments one by one from the end of the URL,
20    # until the URL of the lowest level subsite is identified
21    -1..-($url.Segments.Count) | ForEach-Object {
22      if ($web -eq $null) {
23        # Once the correct URL is obtained, initialize a variable containing an
24        # instance of SPWeb class for the lowest level subsite
25        if($webUrl -eq "/"){
26          $identity = $webUrl
27        } else {
28          $identity = $webUrl.Trim("/")
29        }
30  
31        $web = Get-SPWeb -Site $site -Identity $identity -ErrorAction Stop;
32        $webUrl = $webUrl -replace [Microsoft.SharePoint.Utilities.SPEncode]::UrlDecodeAsUrl($url.Segments[$_]);
33      }
34    }
35  
36    if ($web -ne $null) {
37      0..($url.Segments.Count - 1) | ForEach-Object {
38        $listUrl += $url.Segments[$_];
39        if ($list -eq $null) {
40          $list = $(trap {continue}; $web.GetList($listUrl.TrimEnd("/")));
41        }
42      }
43    }
44  
45    $web.Dispose();
46    $site.Dispose();
47    return $list;
48  }
49  
50  
51  function Copy-SPDocumentLibrary([string]$source,[string]$destination,[switch]$overwrite) {
52    # Get source list
53    $sourceSPList = Get-SPList -url $source
54  
55    # Get destination list
56    $destSPList = Get-SPList -url $destination
57    $spFileCollection = $destSPList.RootFolder.Files
58  
59    # Loop through each item and copy to destination list
60    foreach($item in $sourceSPList.Items) {
61      $file = $sourceSPList.ParentWeb.GetFile($item.File)
62      $targetDocUrl = $file.Url -replace $sourceSPList.RootFolder.Name,
63      $destSPList.RootFolder.Name
64  
65      # Check if folder exists
66      if(-not($destSPList.ParentWeb.GetFolder($file.ParentFolder.Url).Exists)) {
67        # Check each subfolder
68        $folderURL = $file.Url.Split("/")
69        $addFolder = $folderURL[0]
70        for($i=1;$i -lt ($folderURL.Count -1);$i++) {
71          $addFolder = $addFolder + "/" + $folderURL[$i]
72          $destSPList.ParentWeb.Folders.Add($addFolder) | Out-Null
73        }
74        $addFolder = $null
75      }
76  
77      # Check if target file exists
78      if(-not($overwrite) -and $destSPList.ParentWeb.GetFile($targetDocUrl).Exists) {
79        Write-Host "File $targetDocUrl already exists"
80        Continue
81      }
82  
83      $spFileCollection.Add($targetDocUrl,$file.OpenBinary(),$overwrite) | Out-Null
84    }
85  }
86  
87  Copy-SPDocumentLibrary -source "https://sharepoint.oshirowanen.com/sites/oshirodev/Lib1" -destination "https://sharepoint.oshirowanen.com/sites/oshirodev/Lib2" -overwrite

用法:

.\CopyDocs.ps1

其他信息:

  • SharePoint 2010
  • Powershell v2
  • 使用管理员帐户登录的SharePoint 2010命令行管理程序
  • 正在Windows Server 2008 R2 Standard上执行的脚本,该脚本已使用管理员帐户登录

1 个答案:

答案 0 :(得分:0)

由于这种类型的错误,我想您将无法访问内容数据库。我不确定您的问题记录是否涵盖了这一点。

要检查您的用户是否列出:

Get-SPShellAdmin 

如果缺少它,则需要添加它。

要将用户添加为Shell管理员,请执行以下操作:

Add-SPShellAdmin -UserName <user name> -Database <database name> 

获取您的ID和内容数据库。我将创建一个存根示例(请根据您的需要对其进行调整):

Add-SPShellAdmin -UserName "CONTOSO\Oshiro" -Database (Get-SPContentDatabase -Identity "oshirodev_Content")