gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 16:04:31 35 4
gpt4 key购买 nike

问题:

我似乎无法弄清楚为什么会出现以下错误,即使存在 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 上执行的脚本
  • 最佳答案

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

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

    Get-SPShellAdmin 

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

    要将用户添加为 Shell 管理员:
    Add-SPShellAdmin -UserName <user name> -Database <database name> 

    获取您的 ID 和内容数据库。我将创建一个 stub 示例(请根据需要进行调整):
    Add-SPShellAdmin -UserName "CONTOSO\Oshiro" -Database (Get-SPContentDatabase -Identity "oshirodev_Content") 

    关于powershell - Get-SPWeb 无法找到带有 ID 或 URL 的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52301664/

    35 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com