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

问题描述 投票:6回答:1

题:

我似乎无法弄清楚为什么我收到下面的错误,即使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 Management Shell使用管理员帐户登录
  • 在Windows Server 2008 R2 Standard上执行的脚本,该脚本使用管理员帐户登录
powershell sharepoint sharepoint-2010 powershell-v2.0
1个回答
0
投票

有了这种类型的错误,我猜你会忘记访问内容数据库。我不确定你的问题是否涵盖了这一点。

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

Get-SPShellAdmin 

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

要将用户添加为Shell管理员:

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

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

Add-SPShellAdmin -UserName "CONTOSO\Oshiro" -Database (Get-SPContentDatabase -Identity "oshirodev_Content") 
© www.soinside.com 2019 - 2024. All rights reserved.