【问题标题】:Cannot open sharepoint UNC path unless already opened through Windows Explorer除非已通过 Windows 资源管理器打开,否则无法打开 sharepoint UNC 路径
【发布时间】:2016-11-26 21:35:54
【问题描述】:

我希望有人能阐明这一点,因为它一直让我分心。

我有一个脚本,如果该路径存在,它将通过 UNC 路径将它创建的报告保存到共享点文档库,否则它将保存到网络驱动器位置的 UNC 路径作为后备。

我注意到使用test-path 进行检查、保存(通过 msexcel COM 对象)或尝试使用invoke-item 在 Windows 资源管理器中打开文件夹仅在我已经访问过共享点站点(通过 Web 浏览器 或 windows explorer)自PC上次登录以来(我运行的是Windows 7 Enterprise Service Pack 1 - 64-bit edition)。

如果自上次登录后我还没有手动打开共享点,test-path 返回 false,其他方法导致 ItemNotFoundException 例如

ii : Cannot find path '\\uk.sharepoint.mydomain.local\sites\mycompany\myteam\Shared Documents\Reports' because it does not exist.
At line:1 char:1
+ ii '\\uk.sharepoint.mydomain.local\sites\mycompany\myteam\Shared Document ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (\\uk.sharepoint...\Reports:String) [Invoke-Item], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.InvokeItemCommand

代码示例区域:

$LANPath = "\\myserver\myshare\teamdirs\scriptdir"
$SharepointPath = "\\uk.sharepoint.mydomain.local\sites\mycompany\myteam\Shared Documents\Reoprts"
$ScriptPath = $LANPath + "\bin"
If (Test-Path $SharepointPath) {$BasePath = $SharepointPath;write-host "Using sharepoint to save reports"} else {$BasePath = "$LANPath\Reports";write-host "Using LAN to save reports - sharepoint not accessible"}

$_|select -expandproperty HTMLBody | Out-File $($BasePath + "\Eml_body.html")
    Write-Host "Reformating HTML"
    $html = New-Object -ComObject "HTMLFile";
    $source = Get-Content -Path ($BasePath + "\Eml_body.html") -Raw;

在我的 COM 对象中保存 excel 电子表格时:

$workbook._SaveAs($fileout,[Microsoft.Office.Interop.Excel.XlFileFormat]::xlOpenXMLWorkbook,$Missing,$Missing,$false,$false,[Microsoft.Office.Interop.Excel.XlSaveAsAccessMode]::xlNoChange,[Microsoft.Office.Interop.Excel.XlSaveConflictResolution]::xlLocalSessionChanges,$true,$Missing,$Missing)

【问题讨论】:

  • 我感觉 WebClient 服务将能够帮助您。 $webclient = New-Object System.Net.WebClient 这是 Windows 资源管理器用来访问 SharePoint 位置的服务。供参考:msdn.microsoft.com/en-us/library/…
  • @Thriggle 啊,当然,它是运行 WebDAV 的网络服务器,而不是文件服务器。像鸭子一样走路,像鸭子一样嘎嘎......不是鸭子!我玩过 webclient,但在将 UseDefaultCredentials 设置为 true 时无法对其进行身份验证。相反,我使用了 Invoke-WebRequest,因为这足以让网站进入 WebDAV 缓存,然后 UNC 路径应该可以工作。如果您发布为答案,我将相应地标记为答案。我使用的语法是Invoke-WebRequest -Uri "http://uk.sharepoint.mydomain.local/sites/mycompany/myteame/Shared Documents/Reports" -UseDefaultCredentials
  • 从头开始 - Invoke-WebRequest 可用于访问该站点,但因为它没有使用 UNC 路径,它没有调用 WebDAV,所以我需要重新访问 WebClient 并弄清楚为什么 UseDefaultCredentials 会导致身份验证错误 - 等待更多的谷歌搜索和搜索
  • 根据WebClient.Credentials documentation 上的这个评论,DefaultCredentials 可能是 ASP.NET 工作进程的那些,它不一定有权访问文件:If the WebClient class is being used in a middle tier application, such as an ASP.NET application, the DefaultCredentials belong to the account running the ASP page (the server-side credentials). Typically, you would set this property to the credentials of the client on whose behalf the request is made.

标签: powershell sharepoint unc


【解决方案1】:

您应该能够使用 System.Net.WebClient 对象访问 SharePoint 文件位置。

$client = New-Object System.Net.WebClient

WebClient.Credentials 属性的 documentation 表明在这种情况下,默认凭据可能用于 ASP.NET 服务器端进程,而不是当前用户的凭据:

如果WebClient 类用于中间层应用程序,例如ASP.NET 应用程序,则DefaultCredentials 属于运行ASP 页面的帐户(服务器端凭据)。通常,您会将此属性设置为代表其发出请求的客户端的凭据。

因此,您可能需要手动设置凭据。您可以将它们作为纯文本插入...

$client.Credentials = New-Object System.Net.NetworkCredential("username","pswd","domain")

...或者您可以提示当前用户输入他们的凭据。

$client.Credentials = Get-Credential

这是一个抓取文件并将其内容写入屏幕的示例:

$client = New-Object System.Net.WebClient
$client.Credentials = Get-Credential

$data = $client.OpenRead("http://yoursharepointurl.com/library/document.txt")
$reader = New-Object System.IO.StreamReader($data)
$results = $reader.ReadToEnd()
Write-Host $results
$data.Close()
$reader.Close()

【讨论】:

  • 401 错误不是凭据问题 - 我使用文件夹路径而不是文件路径对我来说是个例外。更改为读取文件并且可以正常工作,但同样,在我没有通过 WebDAV(使用 Windows 资源管理器)访问共享点站点的机器上 - 我收到一个错误:Exception calling "DownloadString" with "1" argument(s): "The network path was not found. " At line:1 char:1 + $webclient.DownloadString($uri); + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : WebException
  • 使用的代码是:$webclient = New-Object System.Net.WebClient; $uri = "\\uk.sharepoint.mydomain.local\sites\mycompany\myteam\Shared Documents\Reports\TRIGGER.txt"; $webclient.DownloadString($uri); ,该文件只包含字符串Success! - 所以我很快就知道它是否有效
  • 这很奇怪。似乎一旦访问它,它就必须缓存该 UNC 路径的映射。是否可以跳过使用 UNC 路径而将网址与 WebClient 服务一起使用,或者出于其他原因需要 UNC?
  • 我的脚本正在 UNC 路径上创建工作文件,然后最后,创建最终 excel 报告的 msexcel comobject 保存到那里 - 即使我在本地或网络驱动器上完成了所有操作,我仍然想要关于 sharepoint 的最终报告。 copy-item 在共享点和 UNC 方面将受到与 test-path 相同的问题的约束。我想我可以使用 webclient 将每个完成的报告上传到 sharepoint - 感觉就像是一个应该很容易的东西的杂物 - 如果我还有很多头发,我会把它撕掉!
  • 我选择尝试更接近地模拟 Windows 资源管理器的功能。 explorer "\\uk.sharepoint.mydomain.local\sites\mycompany\myteam\Shared Documents\Reviews" 可以打开共享点 UNC 路径,然后 test-path 等可以正常使用该路径。由于这是一个旨在由用户运行而不是计划的脚本,所以这对我来说是完美的 - 尽管我仍然希望能够在没有我将来编写的任何非交互式脚本的情况下做到这一点。
【解决方案2】:

我知道这是一个旧线程,但对于那些搜索的人,请查看此链接:https://www.myotherpcisacloud.com/post/Sometimes-I-Can-Access-the-WebDAV-Share-Sometimes-I-Cant!

由于 SharePoint 通过 WebDav 公开其共享,因此您需要确保 WebClient 服务正在您访问路径的计算机上运行。在资源管理器中浏览路径会自动启动服务,而命令行方法则不会。

如果您将 WebClient 的启动类型更改为自动,应该可以解决问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多