【发布时间】:2018-03-15 14:49:35
【问题描述】:
我正在尝试制作一个脚本,它会自动在网络驱动器上找到正确的文件夹。这些是我们用户的主文件夹。
我想压缩指定的文件夹并将其放在其他地方。
当压缩过程开始时,我不断收到网络驱动器上的访问被拒绝的错误消息。
我尝试运行提升的 powershell,但出现同样的错误。 这是我制作的脚本:
[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic') | Out-Null
$username = [Microsoft.VisualBasic.Interaction]::InputBox("Enter Name ")
$source=Get-ChildItem -Path \\server\Home1, \\server\Home2, \\server\Home3 -Include $username | Select-Object -ExpandProperty FullName
$destination = "C:\testzippowershell\"+$username+".zip"
Compress-Archive -literalpath $source -DestinationPath $destination
这些是由此产生的错误:
Get-ChildItem : 指定的网络名称不再可用。在 C:\windows\system32\windowspowershell\v1.0\Modules\Microsoft.PowerShell.Archive\Microsoft.PowerShell.Archive.psm1:663 char:20 + $dirContents = Get-ChildItem -LiteralPath $sourceDirPath -递归+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~ + CategoryInfo : ReadError: (\server\Home3\user\Desktop:String) [Get-ChildItem],IOException + FullyQualifiedErrorId : DirIOError,Microsoft.PowerShell.Commands.GetChildItemCommand Get-ChildItem :指定的网络名称不再可用。在 C:\windows\system32\windowspowershell\v1.0\Modules\Microsoft.PowerShell.Archive\Microsoft.PowerShell.Archive.psm1:663 char:20 + $dirContents = Get-ChildItem -LiteralPath $sourceDirPath -递归+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~ + CategoryInfo : ReadError: (\server\Home3\user\Documents:String) [Get-ChildItem],IOException + FullyQualifiedErrorId : DirIOError,Microsoft.PowerShell.Commands.GetChildItemCommand 删除项目:找不到路径“C:\testzippowershell\user.zip”,因为 它不存在。在 C:\windows\system32\windowspowershell\v1.0\Modules\Microsoft.PowerShell.Archive\Microsoft.PowerShell.Archive.psm1:233 char:21 + ... 删除项目 -LiteralPath $DestinationPath -Force -递归 ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (C:\testzippowershell\user.zip:String) [删除项目],ItemNotFoundException + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.RemoveItemCommand 异常 使用“0”参数调用“GetFiles”:“指定的网络名称 不再被提供。 “ 在 C:\windows\system32\windowspowershell\v1.0\Modules\Microsoft.PowerShell.Archive\Microsoft.PowerShell.Archive.psm1:677 字符:13 + $files = $currentContent.GetFiles() + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : IOException
有谁知道如何让压缩存档适用于 UNC 路径?
谢谢!
编辑:
这是当前结果: (单斜线实际上是双斜线,但是当我编辑以从我的公司中删除详细信息时会丢失)
Get-ChildItem : 访问路径 '\server\Home3\user\Desktop' 是 否认。在 C:\windows\system32\windowspowershell\v1.0\Modules\Microsoft.PowerShell.Archive\Microsoft.PowerShell.Archive.psm1:663 字符:20 + $dirContents = Get-ChildItem -LiteralPath $sourceDirPath -Recurse + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~ + CategoryInfo : PermissionDenied: (\server\Home3\user\Desktop:String) [Get-ChildItem], 未授权访问异常 + FullyQualifiedErrorId : DirUnauthorizedAccessError,Microsoft.PowerShell.Commands.GetChildItemCommand Get-ChildItem :对路径 '\server\Home3\user\Documents' 的访问权限为 否认。在 C:\windows\system32\windowspowershell\v1.0\Modules\Microsoft.PowerShell.Archive\Microsoft.PowerShell.Archive.psm1:663 字符:20 + $dirContents = Get-ChildItem -LiteralPath $sourceDirPath -Recurse + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~ + CategoryInfo : PermissionDenied: (\server\Home3\user\Documents:String) [Get-ChildItem], 未授权访问异常 + FullyQualifiedErrorId : DirUnauthorizedAccessError,Microsoft.PowerShell.Commands.GetChildItemCommand 删除项目:找不到路径“C:\testzippowershell\user.zip”,因为 它不存在。在 C:\windows\system32\windowspowershell\v1.0\Modules\Microsoft.PowerShell.Archive\Microsoft.PowerShell.Archive.psm1:233 字符:21 + ... Remove-Item -LiteralPath $DestinationPath -Force -Recurse ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~ + CategoryInfo : ObjectNotFound: (C:\testzippowershell\user.zip:String) [Remove-Item], ItemNotFoundException + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.RemoveItemCommand
使用“0”参数调用“GetFiles”的异常:“访问路径 '\server\Home3\user\Desktop' 被拒绝。”在 C:\windows\system32\windowspowershell\v1.0\Modules\Microsoft.PowerShell.Archive\Microsoft.PowerShell.Archive.psm1:677 字符:13 + $files = $currentContent.GetFiles() + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : UnauthorizedAccessException
【问题讨论】:
-
对我来说它的工作,我可以从一个 unc 路径压缩文件。 test-path -path $source`的结果是什么?
-
看起来您在 UNC 路径的开头缺少一个斜杠,因为错误显示为
\server\Home3\user\Desktop,但我不确定这是怎么发生的!也许尝试在每条路径周围加上单引号? -
如果我使用 -path 而不是 -literalpath,我会收到以下错误:Get-ChildItem : Access to the path '\\server\Home3\user\Desktop' is denied.
-
我可以使用命令行 7ip:[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic') | Out-Null $username = [Microsoft.VisualBasic.Interaction]::InputBox("Enter Name") $source=Get-ChildItem -Path \\server\Home1, \\server\Home2, \\server\Home3 -Include $用户名 | Select-Object -ExpandProperty FullName $destination = "C:\testzippowershell\"+$username+".zip" cd $source C:\temp\7z1604-extra\7za.exe a -t7z $username+".zip"
-
我尝试了以下方法来测试路径,但它似乎可以正常工作 PS H:\> # PowerShell 检查文件是否存在 $WantFile = "\\server\Home3\user" $FileExists = Test-Path $WantFile If ($FileExists -eq $True) {Write-Host "Yippee"} Else {Write-Host "No file at this location"} Yippee