2016-04-06 39 views
1

我希望有人能够阐明这一点,因为它一直在让我分心。无法打开共享点UNC路径,除非已通过Windows资源管理器打开

我有一个脚本,它将通过UNC路径将其创建的报告保存到SharePoint文档库中(如果路径存在),否则将作为回退保存到网络驱动器位置的UNC路径。

我注意到,与test-path检查,保存(通过msexcel的COM对象),或者尝试使用invoke-item如果我已经访问SharePoint网站只工作(通过Web浏览器窗口打开Windows资源管理器的文件夹资源管理器),因为电脑上次登录(我正在运行Windows 7企业服务包1 - 64位版本)。

如果自上次登录后我还没有手动分享点,则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 

例领域:

$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) 

$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电子表格时

+1

我有一种感觉,WebClient服务将能够帮助你。'$ webclient = New-Object System.Net.WebClient'这是Windows资源管理器用来访问SharePoint位置的服务。作为参考:https://msdn.microsoft.com/en-us/library/system.net.webclient(v=vs.110).aspx – Thriggle

+0

@Thriggle啊,当然,这是一个运行WebDAV的Web服务器,而不是文件服务器。像鸭子一样走路,鸭子像鸭子......不是鸭子!我有一个使用webclient的游戏,但在将UseDefaultCredentials设置为true时无法进行身份验证。相反,我已经使用了Invoke-WebRequest,因为它应该足以让站点进入WebDAV缓存,此后UNC路径应该可以工作。如果你张贴作为答案,我会相应地标记为答案。我使用的语法是“Invoke-WebRequest -Uri”http://uk.sharepoint.mydomain.local/sites/mycompany/myteame/Shared Documents/Reports“-UseDefaultCredentials' –

+0

Scratch that - ”Invoke-WebRequest' for the access但是因为它没有使用UNC路径,所以我不需要调用WebDAV,所以我需要重新访问WebClient并了解为什么'UseDefaultCredentials'导致认证错误 - 更多的搜索和搜索SO正在等待 –

回答

0

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

$client = New-Object System.Net.WebClient 

documentationWebClient.Credentials属性表明,在这种情况下,默认凭证可能是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.

因此,您可能希望手动设置凭据。您可以插入纯文本...

$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() 
+0

401错误不是证书问题 - 对于我使用文件夹路径而不是文件路径。改变阅读一个文件,并在那里工作,但再次,在我没有通过WebDAV访问SharePoint的机器(使用Windows资源管理器) - 我得到一个错误:'异常调用“1”参数的“DownloadString” s):“网络路径 未找到。” “ 在线:1 char:1 + $ webclient.DownloadString($ uri); + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo:NotSpecified:(:) [],MethodInvocationException + FullyQualifiedErrorId: WebException' –

+0

使用的代码是:'$ webclient = New-Object System.Net.WebClient; $ uri =“\\ uk.sharepoint.mydomain.local \ sites \ mycompany \ myteam \ Shared Documents \ Reports \ TRIGGER.txt”; $ webclient.DownloadString($ uri); '那个文件只包含字符串'Success!' - 所以我很快就知道它是否有效 –

+0

这很奇怪。似乎它必须缓存该访问过的UNC路径的映射。您可以跳过使用UNC路径并将Web地址用于WebClient服务,还是出于其他原因需要使用UNC? – Thriggle

0

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

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

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

相关问题