0
votes

I'm trying to make a script that will automaticly find the correct folder on a network drive. These are our users' homefolders.

I want to zip the specified folder and place it somewhere else.

I keep getting an error that access is denied on the network drive when the zipping proces start.

I tried running elevated powershell, but the same error occurs. Here is the script I made:

[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

These are the resulting errors:

Get-ChildItem : The specified network name is no longer available. At C:\windows\system32\windowspowershell\v1.0\Modules\Microsoft.PowerShell.Archive\Microsoft.PowerShell.Archive.psm1:663 char:20 + $dirContents = Get-ChildItem -LiteralPath $sourceDirPath -Recurse + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ReadError: (\server\Home3\user\Desktop:String) [Get-ChildItem], IOException + FullyQualifiedErrorId : DirIOError,Microsoft.PowerShell.Commands.GetChildItemCommand Get-ChildItem : The specified network name is no longer available. At C:\windows\system32\windowspowershell\v1.0\Modules\Microsoft.PowerShell.Archive\Microsoft.PowerShell.Archive.psm1:663 char:20 + $dirContents = Get-ChildItem -LiteralPath $sourceDirPath -Recurse + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ReadError: (\server\Home3\user\Documents:String) [Get-ChildItem], IOException + FullyQualifiedErrorId : DirIOError,Microsoft.PowerShell.Commands.GetChildItemCommand Remove-Item : Cannot find path 'C:\testzippowershell\user.zip' because it does not exist. At C:\windows\system32\windowspowershell\v1.0\Modules\Microsoft.PowerShell.Archive\Microsoft.PowerShell.Archive.psm1:233 char:21 + ... Remove-Item -LiteralPath $DestinationPath -Force -Recurse ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (C:\testzippowershell\user.zip:String) [Remove-Item], ItemNotFoundException + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.RemoveItemCommand Exception calling "GetFiles" with "0" argument(s): "The specified network name is no longer available. " At C:\windows\system32\windowspowershell\v1.0\Modules\Microsoft.PowerShell.Archive\Microsoft.PowerShell.Archive.psm1:677 char:13 + $files = $currentContent.GetFiles() + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : IOException

Does anyone know how to get the compress-archive working for a UNC path?

Thanks!

Edit:

This is the current result: (The single slash is actually double, but get lost when I edit to remove details from my company)

Get-ChildItem : Access to the path '\server\Home3\user\Desktop' is denied. At C:\windows\system32\windowspowershell\v1.0\Modules\Microsoft.PowerShell.Archive\Microsoft.PowerShell.Archive.psm1:663 char:20 + $dirContents = Get-ChildItem -LiteralPath $sourceDirPath -Recurse + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : PermissionDenied: (\server\Home3\user\Desktop:String) [Get-ChildItem], UnauthorizedAccessException + FullyQualifiedErrorId : DirUnauthorizedAccessError,Microsoft.PowerShell.Commands.GetChildItemCommand Get-ChildItem : Access to the path '\server\Home3\user\Documents' is denied. At C:\windows\system32\windowspowershell\v1.0\Modules\Microsoft.PowerShell.Archive\Microsoft.PowerShell.Archive.psm1:663 char:20 + $dirContents = Get-ChildItem -LiteralPath $sourceDirPath -Recurse + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : PermissionDenied: (\server\Home3\user\Documents:String) [Get-ChildItem], UnauthorizedAccessException + FullyQualifiedErrorId : DirUnauthorizedAccessError,Microsoft.PowerShell.Commands.GetChildItemCommand Remove-Item : Cannot find path 'C:\testzippowershell\user.zip' because it does not exist. At C:\windows\system32\windowspowershell\v1.0\Modules\Microsoft.PowerShell.Archive\Microsoft.PowerShell.Archive.psm1:233 char:21 + ... Remove-Item -LiteralPath $DestinationPath -Force -Recurse ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (C:\testzippowershell\user.zip:String) [Remove-Item], ItemNotFoundException + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.RemoveItemCommand
Exception calling "GetFiles" with "0" argument(s): "Access to the path '\server\Home3\user\Desktop' is denied." At C:\windows\system32\windowspowershell\v1.0\Modules\Microsoft.PowerShell.Archive\Microsoft.PowerShell.Archive.psm1:677 char:13 + $files = $currentContent.GetFiles() + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : UnauthorizedAccessException

1
For me its working, I can compress files from a unc-path. what is the result of test-path -path $source`?guiwhatsthat
Looks like you're missing a slash from the start of your UNC path as the error shows \server\Home3\user\Desktop, I'm not sure how this is happening though! maybe try putting single quotes around each path?James C.
If I use -path instead of -literalpath I get these error: Get-ChildItem : Access to the path '\\server\Home3\user\Desktop' is denied.Niels Frederickx
I can get it working with a commandline7ip: [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" cd $source C:\temp\7z1604-extra\7za.exe a -t7z $username+".zip"Niels Frederickx
I tried the following to test the path, but it seems to be working PS H:\> # PowerShell Checks If a File Exists $WantFile = "\\server\Home3\user" $FileExists = Test-Path $WantFile If ($FileExists -eq $True) {Write-Host "Yippee"} Else {Write-Host "No file at this location"} YippeeNiels Frederickx

1 Answers

0
votes

I was stupid.

The homedrive I was testing was my own.

I tried with a different and it was working without any issues.