【发布时间】: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