Using powershell with 7zip - password protect each file in a folder - powershell

I am trying to password protect 10 files (using the same password) in a folder. Once completed, i would like to have 10 individual zip files each with the same password. My script is as follows but it errors out saying it cant open the file as archive.
$list=gci "C:\temp\test"
foreach ($file in $list)
{
$file1=$file|select -ExpandProperty name
& 'c:\program files\7-zip\7z.exe' a $file1.zip $file1 -p123
}
This is the error i get:
Open archive: testfile.pdf
7z.exe : ERROR: testfile.pdf
At line:6 char:5
+ & 'c:\program files\7-zip\7z.exe' a $file1.zip $file1 -p123
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (ERROR: testfile.pdf:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
Can not open the file as archive
System ERROR:
Incorrect function.
Any help would be greatly appreciated. Thanks

Oh boy...figured it out myself..needed quotation marks!!!
& 'c:\program files\7-zip\7z.exe' a "$file1.zip" $file1 -p123

Related

Trying to extract version info and save to INI file on Windows 11 with PowerShell

I am not familiar with PowerShell and I am struggling at the outset.
My task I want to achieve:
Get version number from EXE file.
I know I can get that:
(Get-Item "MeetSchedAssistSetup.exe").VersionInfo.ProductVersionRaw
In the console window it displays:
Major Minor Build Revision
----- ----- ----- --------
23 0 3 0
Open a INI file. I installed the PSIni module but this line fails:
$ini = Get-IniContent version_meetschedassist2.ini
It says:
Get-IniContent : The 'Get-IniContent' command was found in the module 'PsIni', but the module could not be loaded. For
more information, run 'Import-Module PsIni'.
At line:1 char:8
+ $ini = Get-IniContent version_meetschedassist2.ini
+ ~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Get-IniContent:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CouldNotAutoloadMatchingModule
How do I load the INI?
My task it to update this bit in the INI:
[MeetSchedAssist Update]
LatestVersion=23.03
LatestVersionString=23.0.3
So I want to:
Open INI
Extract Version from EXE
Update the two INI values from the Version
Save INI
I tried using Import-Module:
Import-Module : File C:\Program Files\WindowsPowerShell\Modules\PsIni\3.1.3\PsIni.psm1 cannot be loaded because running scripts is disabled on
this system. For more information, see about_Execution_Policies at https:/go.microsoft.com/fwlink/?LinkID=135170.
At line:1 char:1
+ Import-Module PsIni
+ ~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : SecurityError: (:) [Import-Module], PSSecurityException
+ FullyQualifiedErrorId : UnauthorizedAccess,Microsoft.PowerShell.Commands.ImportModuleCommand
No joy.
Here's a very quick and very dirty way of doing this just as an example:
$exe_info = Get-Item -Path '.\MeetSchedAssistSetup.exe'
$ini_path = '.\version_meetschedassist2.ini'
$ini = Get-IniContent -FilePath $ini_path
$ini['MeetSchedAssist Update']['LatestVersion'] =
'{0}.{1}{2}' -f $exe_info.VersionInfo.FileMajorPart,
$exe_info.VersionInfo.FileMinorPart, $exe_info.VersionInfo.FileBuildPart
$ini['MeetSchedAssist Update']['LatestVersionString'] =
'{0}.{1}.{2}' -f $exe_info.VersionInfo.FileMajorPart,
$exe_info.VersionInfo.FileMinorPart, $exe_info.VersionInfo.FileBuildPart
Out-IniFile -FilePath $ini_path -InputObject $ini -Force
Since Get-IniContent gets saved into memory via $ini, you can replace the values with what you need them to be and pass it back out with Out-IniFile specifying $ini as the -InputObject for it. The values are updated using string concatenating of the version info.

requested file operation failed in PowerShell

I'm trying to read a txt file from my directory and getting the following error. This is the first time seeing it and I don't think it happened to me before. So, just wondering what's going on and how to fix this.
$FilePath = 'C:\Users\yzqpsn\Desktop\Audit-Log-Script\teams.txt'
$Data = Get-Content -Path $FilePath
Get-Content : The requested file operation failed because the storage policy blocks that type of file. For more information, contact your system
administrator.
At line:1 char:9
+ $Data = Get-Content -Path $FilePath
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ReadError: (C:\Users\yzqpsn...cript\teams.txt:String) [Get-Content], IOException
+ FullyQualifiedErrorId : GetContentReaderIOError,Microsoft.PowerShell.Commands.GetContentCommand

Error while fetching the files from sftp server using powershell

I'm trying to get *.xlsx files from the sftp server folder "/From/Outbound/TEST" using putty. I know it's something to do with the source path. I tried the below but nothing is working. Please please help.
"$Source/*.xlsx"
'$Source/*.xlsx'
$Source = "sftp#33.113.250.45:/From/Outbound/TEST"
[string]$TransferResults = & 'C:\Program Files\PuTTY\pscp.exe' -l 'user' -pw 'password' $Source/*.xlsx $Destination
I'm getting the below error:
pscp.exe : pscp: wildcard '*.xlsx' matched no files
At Test.ps1:29 char:31
+ [string]$TransferResults = & 'C:\Program Files\PuTTY\pscp.exe' -l 'user' - ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (pscp: wildcard '*.xlsx' matched no files:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError

Reading zip file in Powershell (openread)

I am trying to read zip file by openread method, but it gives error that could not find zip file.
Below is the code that I am using:
Code:
$Sourcefolder= "C:\Users\My.pc\Downloads"
$Myzipfile= gci $Sourcefolder -Filter *.zip | sort LastWriteTime | select -last 1
Add-Type -AssemblyName System.IO.Compression.FileSystem
$zip = [System.IO.Compression.ZipFile]::OpenRead($Myzipfile)
Error:
Exception calling "OpenRead" with "1" argument(s): "Could not find file 'C:\Users\My.pc\15367448612289827.zip'."
At line:4 char:1
+ $zip = [System.IO.Compression.ZipFile]::OpenRead($Myzipfile)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : FileNotFoundException
What should I do? any help please.
Change $Myzipfile to $Myzipfile.FullName:
$zip = [System.IO.Compression.ZipFile]::OpenRead($Myzipfile.FullName)

Getting access denied when trying to copy/zip C:\Users\Me\Documents via powershell

I'm trying to write a script to quickly zip a list of directories and I'm getting an access denied to 'C:\Users\Me\Documents\My Music', which I gather is due to it being a symbolic link.
How can I accomplish this and exclude the symbolic links to be able to zip a directory? I don't think it's permissions related as I'm running as admin, etc.
Code:
function ZipFiles( $zipfilename, $sourcedir )
{
Write-Host "Zipping $sourcedir to $zipfilename"
Add-Type -Assembly System.IO.Compression.FileSystem
$compressionLevel = [System.IO.Compression.CompressionLevel]::Optimal
[System.IO.Compression.ZipFile]::CreateFromDirectory($sourcedir, $zipfilename, $compressionLevel, $false)
}
ZipFiles -sourcedir "C:\Users\Me\Documents" -zipfilename "C:\TestZip.zip"
Error:
Zipping C:\Users\me\Documents to C:\TestZip.zip
Exception calling "CreateFromDirectory" with "4" argument(s): "Access to the path 'C:\Users\Me\Documents\My Music' is denied."
At line:8 char:5
[System.IO.Compression.ZipFile]::CreateFromDirectory($sourcedir, $zipfilenam ...
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
CategoryInfo : NotSpecified: (:) [], MethodInvocationException
FullyQualifiedErrorId : UnauthorizedAccessException
Stab in the dark but, can you set the Source dir like the following?
-sourcedir '\\?\C:\users\Me\Documents'