gpt4 book ai didi

powershell - 除非已通过 Windows 资源管理器打开,否则无法打开共享点 UNC 路径

转载 作者:行者123 更新时间:2023-12-03 23:17:52 24 4
gpt4 key购买 nike

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

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

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

如果自上次登录以来我还没有手动访问共享点,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)

最佳答案

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

$client = New-Object System.Net.WebClient

documentation WebClient.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()

关于powershell - 除非已通过 Windows 资源管理器打开,否则无法打开共享点 UNC 路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36455875/

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