Powershell: Expand-Archive: Cannot validate argument on parameter 'Path' - powershell

I'm trying to run a powershell command to extract all zip files into a folder to a new path.
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -command Get-ChildItem 'Z:\audio\music\purchased-music\incoming' -Filter *.zip | Expand-Archive -DestinationPath 'Z:\audio\music\purchased-music\incoming-unzipped' -Force
It looks like the command should work, but I get an error as such:
Expand-Archive : Cannot validate argument on parameter 'Path'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.
At line:1 char:140
+ ... ter *.zip | Expand-Archive -DestinationPath 'Z:\audio\music\purchased ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:PSObject) [Expand-Archive], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Expand-Archive
Expand-Archive : Cannot validate argument on parameter 'Path'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.
At line:1 char:140
+ ... ter *.zip | Expand-Archive -DestinationPath 'Z:\audio\music\purchased ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:PSObject) [Expand-Archive], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Expand-Archive
Expand-Archive : The path ' Directory: Z:\audio\music\purchased-music\incoming' either does not exist or is not a valid file system path.
At line:1 char:140
+ ... ter *.zip | Expand-Archive -DestinationPath 'Z:\audio\music\purchased ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: ( Directory: ...-music\incoming:String) [Expand-Archive], InvalidOperationException
+ FullyQualifiedErrorId : ArchiveCmdletPathNotFound,Expand-Archive
The path and destination both do exist:
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -command Get-ChildItem 'Z:\audio\music\purchased-music\incoming' -Filter *.zip
Directory: Z:\audio\music\purchased-music\incoming
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 2/6/2021 4:28 PM 38489484 Satsuma.zip
PS C:\Windows\System32\WindowsPowerShell\v1.0> dir Z:\audio\music\purchased-music\incoming-unzipped
Directory: Z:\audio\music\purchased-music\incoming-unzipped
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 12/16/2020 2:15 AM 20320 touch.txt

To expand on Olaf's helpful comment, this would work with braces as per powershell /?, or about_PowerShell_exe documentation:
PS C:\Windows\System32\WindowsPowerShell\v1.0> C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -command {Get-ChildItem 'Z:\audio\music\purchased-music\incoming' -Filter *.zip | Expand-Archive -DestinationPath 'Z:\audio\music\purchased-music\incoming-unzipped' -Force}
Since you're using PowerShell, you don't need to launch the exe to execute a command:
Get-ChildItem 'Z:\audio\music\purchased-music\incoming' -Filter *.zip |
Expand-Archive -DestinationPath 'Z:\audio\music\purchased-music\incoming-unzipped' -Force
(the PS C:\Windows\System32\WindowsPowerShell\v1.0> isn't relevant to code execution as you're specifying full paths Z:\audio\... etc - you could be running this from any location)

Related

Is there an error-less PowerShell equivalent to: tar cfz hierarchy.tgz hierarchy

In GNU/Linux, I can just do this:
tar cfz hierarchy.tgz hierarchy
and it just works, even if some of the items in hierarchy are being used by other processes.
In Windows, I can right-click on a folder and send it to a compressed zip file, so long as it's in another place, like the Desktop, and that works.
But in Windows PowerShell, something you would think would be so simple, isn't:
PS E:\> Compress-Archive -Path e:\lib -DestinationPath .\e-lib-all.zip
ZipArchiveHelper : The process cannot access the file
'E:\lib\company\data\data-azure-java\1.0.1\data-azure-java-1.0.1.jar' because it is being used by another process.
At
C:\Windows\system32\WindowsPowerShell\v1.0\Modules\Microsoft.PowerShell.Archive\Microsoft.PowerShell.Archive.psm1:697
char:30
+ ... sArchived = ZipArchiveHelper $subDirFiles.ToArray() $destinationPath ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : PermissionDenied: (E:\lib\company...-java-1.0.1.jar:String) [Write-Error], IOException
+ FullyQualifiedErrorId : CompressArchiveUnauthorizedAccessError,ZipArchiveHelper
New-Object : Exception calling ".ctor" with "1" argument(s): "Stream was not readable."
At
C:\Windows\system32\WindowsPowerShell\v1.0\Modules\Microsoft.PowerShell.Archive\Microsoft.PowerShell.Archive.psm1:808
char:38
+ ... $srcStream = New-Object System.IO.BinaryReader $currentFileStream
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [New-Object], MethodInvocationException
+ FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand
I asked a coworker, what's the PowerShell equivalent of the GUI way that just works? He thought what the GUI does in the background is create a copy using service level rather than user permissions, and then zips that.
Is there a PowerShell way to do this? I've tried some other things that did not work, namely:
Copy-Item -Path "E:\lib" -Force -PassThru | `
Get-ChildItem | `
Compress-Archive -DestinationPath "E:\e-lib-all.zip"
Get-ChildItem -Recurse -Path $sourcePath | Compress-Archive -DestinationPath $destinationPath
Get-ChildItem -Recurse -Path "E:\lib" | Compress-Archive -DestinationPath "E:\e-lib-all.zip"
Yes, I looked at previous SO post Compress-Archive error: PermissionDenied but it didn't help (I got the same errors).

Using Windows PowerShell to batch remove "[" character from filenames

Trying to use PowerShell to remove an odd character, but it keeps breaking on [:
Filename is test [t].txt
Dir | Rename-Item -NewName { $_.Name -replace "[","_" }
Results in:
PS D:\test> Dir | Rename-Item -NewName { $_.Name -replace "[","_" }
Rename-Item : The input to the script block for parameter 'NewName' failed.
Invalid regular expression pattern: [.
At line:1 char:27
+ Dir | Rename-Item -NewName <<<< { $_.Name -replace "[","_" }
+ CategoryInfo : InvalidArgument: (test [t].txt:PSObject) [Rename-Item], ParameterBindingException
+ FullyQualifiedErrorId : ScriptBlockArgumentInvocationFailed,Microsoft.PowerShell.Commands.RenameItemCommand
I've tried a few common ways to escape the character \[, '[', ... and nada.
Thoughts and ideas?
PS: Trying the Replace() method with \[ escape:
PS D:\test> Dir | Rename-Item -NewName{ $_.Name.Replace("\[","_")}
Rename-Item : Cannot rename because item at 'Microsoft.PowerShell.Core\
FileSystem::D:\test\test [t].txt' does not exist.
At line:1 char:18
+ Dir | Rename-Item <<<< -NewName{ $_.Name.Replace("\[","_")}
+ CategoryInfo : InvalidOperation: (:) [Rename-Item], PSInvalidOperationException
+ FullyQualifiedErrorId : InvalidOperation,Microsoft.PowerShell.Commands.RenameItemCommand
PPS: The file DOES exist:
PS D:\test> dir
Directory: D:\test
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 1/27/2018 4:08 PM 448 rb.bat
-a--- 1/27/2018 3:04 PM 4 test [t].txt
The appending of the PS header to the path by something inside PS when it sees a [ in the file name is the problem. If I replace the [ (bracket) with a ( (paren) everything works as expected.
The escape charachter is a backslash. So your regex pattern should look like this:
Get-ChildItem |
Rename-Item -NewName {$_.Name -replace '\[|\]','_'}
This will replace the opening square bracket and the closing square bracket.
Yes, I see that you resolved your situation with brute force. This code appears to produce the filename you desire.
PS C:\src\t\renfunc> Get-ChildItem
Directory: C:\src\t\renfunc
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 2018-09-11 08:51 7 test [2].txt
PS C:\src\t\renfunc> Get-ChildItem | ForEach-Object { $_.Name -replace '\[|\]','_' }
test _2_.txt

PowerShell - Get-ChildItem does not exclude directory

When recursively searching through a specific directory, Get-ChildItem will search starting at the root. I do not want to search through the C:\Windows directory. I want to restrict the search to the C:\Docs directory.
Here is what I am running:
PS> Get-ChildItem -path “C:\docs” -Filter "*crypt*" -recurse -ErrorAction Stop
Get-ChildItem : Access to the path 'C:\Windows\CSC' is denied.
At line:1 char:1
+ Get-ChildItem -path “C:\docs” -Filter "*crypt*" -recurse -ErrorAction ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : PermissionDenied: (C:\Windows\CSC:String) [Get-ChildItem], UnauthorizedAccessException
+ FullyQualifiedErrorId : DirUnauthorizedAccessError,Microsoft.PowerShell.Commands.GetChildItemCommand
PS> Get-ChildItem -path “C:\docs” -Filter "*crypt*" -exclude "C:\windows" -recurse -ErrorAction Stop
Get-ChildItem : Access to the path 'C:\Windows\CSC' is denied.
At line:1 char:1
+ Get-ChildItem -path “C:\docs” -Filter "*crypt*" -exclude "C:\windows" ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : PermissionDenied: (C:\Windows\CSC:String) [Get-ChildItem], UnauthorizedAccessException
+ FullyQualifiedErrorId : DirUnauthorizedAccessError,Microsoft.PowerShell.Commands.GetChildItemCommand
EDIT: TL;DR: User error. I do not have the C:\Docs directory.
I am editing this script, which is running on several servers. I am testing it on my laptop. I still don't understand why it would look through the rest of the file system, once it could not find the starting path.
It looks like (I haven't searched enough) this may be a bug in Get-ChildItem; if you pass a non-existent path to the -path parameter, it searches from the root of that drive (at least for local drives).
Before calling Get-ChildItem, test for the existence of the path and you can avoid this.
$mypath = "c:\docs";
if (test-path -path $mypath) {
Get-childitem –path $mypath –filter “*crypt*" -recurse -ErrorAction stop;
} else {
Write-Warning "$mypath not found";
}

Power Shell Community Extensions - Zip file not being created. Errors Encountered in ISE

I am almost done with my Power Shell script, however, I am trying to zip a folder containing images in a network share. I have added/imported the module called pscx for the Power Shell Community Extensions (http://pscx.codeplex.com/). The zip file is not being created and I get errors in the ISE window.
The code that does the zipping is:
Import-Module Pscx
cd $ZipFileCreationDir
write-zip -path $FolderToCompress -outputpath $ZipFileName -noclobber -quiet -flattenpaths -level 9 | move-item $AS2OutgoingDir
The variables contain the following information:
$ZipFileCreationDir = "\\10.0.100.3\www2.varietydistributors.com\catalog"
$FolderToCompress = "newpictures"
$ZipFileName = "vdi.zip"
$AS2OutgoingDir = "\\10.0.100.3\AS2_Outgoing"
The errors encountered are:
Import-Module : The specified module 'Pscx' was not loaded because no valid module file was found in any module directory.
At C:\Documents and Settings\Administrator.VDICORP\Desktop\ProcessOSImages.ps1:73 char:22
+ Import-Module <<<< Pscx
+ CategoryInfo : ResourceUnavailable: (Pscx:String) [Import-Module], FileNotFoundException
+ FullyQualifiedErrorId : Modules_ModuleNotFound,Microsoft.PowerShell.Commands.ImportModuleCommand
Write-Zip : Cannot find drive. A drive with the name 'Microsoft.PowerShell.Core\FileSystem' does not exist.
At C:\Documents and Settings\Administrator.VDICORP\Desktop\ProcessOSImages.ps1:75 char:18
+ write-zip <<<< -path $FolderToCompress -outputpath $ZipFileName -noclobber -quiet -flattenpaths -level 9 | move-item $AS2OutgoingDir
+ CategoryInfo : ObjectNotFound: (Microsoft.PowerShell.Core\FileSystem:String) [Write-Zip], DriveNotFoundException
+ FullyQualifiedErrorId : DriveNotFound,Pscx.Commands.IO.Compression.WriteZipCommand
Please assist if you are able to. Thanks in advance. In short, I am trying to zip a folder located at \10.0.100.3\www2.varietydistributors.com\catalog\newpictures and then move the zip file to \10.0.100.3\AS2_Outgoing
I then changed the code to the following and then reran the script in the ISE:
#Add-PSSnapin PSCX
#. "$Pscx:ProfileDir\PscxConfig.ps1"
#Import-Module Pscx
cd "Microsoft.PowerShell.Core\FileSystem::$ZipFileCreationDir"
write-zip -path $FolderToCompress -outputpath $ZipFileName -noclobber -quiet -flattenpaths -level 9 | move-item "Microsoft.PowerShell.Core\FileSystem::$AS2OutgoingDir"
I then get the following errors:
Write-Zip : Cannot find drive. A drive with the name 'Microsoft.PowerShell.Core\FileSystem' does not exist.
At C:\Documents and Settings\Administrator.VDICORP\Desktop\ProcessOSImages.ps1:75 char:18
+ write-zip <<<< -path $FolderToCompress -outputpath $ZipFileName -noclobber -quiet -flattenpaths -level 9 | move-item "Microsoft.PowerShell.Core\FileSystem::$AS2OutgoingDir"
+ CategoryInfo : ObjectNotFound: (Microsoft.PowerShell.Core\FileSystem:String) [Write-Zip], DriveNotFoundException
+ FullyQualifiedErrorId : DriveNotFound,Pscx.Commands.IO.Compression.WriteZipCommand

powershell expand-archive cmdlet - problem with unpack split zip files

I'm trying to unpack zip file which is in pieces (file.z01, file.z02, file.zip)
I use expand-archive cmdlet from PSCX but i get an error
PS C:\> expand-archive -format zip -path c:\temp\file.zip -outputpath c:\temp\ -showprogress
error message:
Expand-Archive : Internal error! hresult: 00000001 At line:1 char:15
+ expand-archive <<<< -format zip -path c:\temp\file.zip
-outputpath c:\temp\ -showprogress
+ CategoryInfo : NotSpecified:
(C:\temp\file.zip:String) [Expand-Archive],
InvalidOperationException
+ FullyQualifiedErrorId :
FileError,Pscx.Commands.IO.Compression.ExpandArchiveCommand
when i use the same command for .rar file (file.part1.rar, file.part2.rar) it works fine
any ideas ?