2016-04-06 40 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的啓動類型更改爲自動,它應該可以解決問題。

相關問題