7Zip Powershell not working - windows

I'm having trouble getting a .7z file to extract via Powershell.
My PowerShell function looks like this:
function unzip($file, $destination)
{
& 'C:\Program Files\7-Zip\7z.exe' x -y $file -o"$destination";
}
I get this error:
7z.exe :
At restoreQA.ps1:26 char:5
+ & 'C:\Program Files\7-Zip\7z.exe' x -y $file -o"$destination";
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
Command Line Error:
Too short switch:
-o
There seems to be some sort of parsing error but I've tried every different combination to get it working.
Any ideas on why this is not working?

You need to put -o in quotes:
& 'C:\Program Files\7-Zip\7z.exe' x -y $file "-o$destination"

Related

finding an error in running c++ code in visual studio code

g++ : The term 'g++' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name,
or if a path was included, verify that the path is correct and try again.
At line:1 char:31
+ cd "d:\PRACTICE\" ; if ($?) { g++ 1.cpp -o 1 } ; if ($?) { .\1 }
+ ~~~
+ CategoryInfo : ObjectNotFound: (g++:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException

"Unable to find specified file" - Powershell

I wrote this function in my powershell profile:
function start {
if ($args[0] == "blender") {
Invoke-Item 'C:\Program Files\Blender Foundation\Blender 2.90\blender.exe'
}
}
It gives me the error below on line 1, at character 1:
Unable to find specified file
The file path is correct, since if I copy and paste the code Invoke-Item ... in my powershell it opens Blender normally. What can the problem be?
Thank you!
This happens, as your function name collides with Start-Process.:
get-alias -Name start
CommandType Name Version Source
----------- ---- ------- ------
Alias start -> Start-Process
Renaming the function is the first step for correction like so,
function start-myApps {
if ($args[0] == "MyApplication") {
Invoke-Item 'C:\Program Files\MyApplication\MyApplication.exe'
}
}
But that isn't enough, as it gives another an error:
Start-MyApps "MyApplication"
= : The term '=' is not recognized as the name of a cmdlet, function, script file, or operable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:2 char:19
+ if ($args[0] == "MyApplication") {
+ ~
+ CategoryInfo : ObjectNotFound: (=:String) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : CommandNotFoundException
This one is caused because Powershell's equality comparison operator is -eq, not ==.
The working version has different name and operator like so,
function start-myApps {
if ($args[0] -eq "MyApplication") {
Invoke-Item 'C:\Program Files\MyApplication\MyApplication.exe'
}
}

jq assignment where the path contain space

This might be a simple question but I am struggling to figure out how jq work with spaces in elements. (The following is in windows powershell)
PS C:\Users\Home> "{}" | jq ".mytest.path = """"success"""""
{
"mytest": {
"path": "success"
}
}
Now, I want to make "my test" two separate words. My desired output is
{
"my - test": {
"path": "success"
}
}
What query would I used? I tried the following:
PS C:\Users\Home> "{}" | jq ".""my - test"".path = """"success"""""
jq : jq: error: Could not open file test.path = "success": Invalid argument
At line:1 char:8
+ "{}" | jq ".""my - test"".path = """"success"""""
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (jq: error: Coul...nvalid argument:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
PS C:\Users\Home> "{}" | jq --raw-output ".""my test"".path = """"success"""""
jq : jq: error: Could not open file test.path = "success": Invalid argument
At line:1 char:8
+ "{}" | jq --raw-output ".""my test"".path = """"success"""""
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (jq: error: Coul...nvalid argument:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
Any suggestions?
First, setting aside for the moment all the issues related to peculiarities of different shells, the jq filter you need is:
.["my test"].path = "success"
(If ever in doubt about using the .foo syntax, you can always fall back on the fundamental form: .["key name"].)
Since you are evidently conversant with the transmogrification required by the mighty PowerShell, I'll just point out here that one can simply place the jq program in a file (say program.jq) and invoke jq with the -f option (jq -f program.jq ...).

fail to copy file from one vm (windows ) to another vm (windows ) with ansible

it is possible to copy file from one vm (windows ) to another vm (windows ) with ansible
- script: test.ps1
Z: is network map
copy z:\PROGRA~1\test\File.txt C:\Users\file.txt
failed with error:
changed: [{{ host1 }}] => {"changed": true, "rc": 0, "stderr": "copy : Cannot fi
nd drive. A drive with the name z does not exist.\r\nAt C:\\Users\\vagrant\\AppD
ata\\Local\\Temp\\ansible-tmp-1427807499.79-10385112351761\r\n7\\test.ps1:1 char
:1\r\n+ copy z:\\PROGRA~1\\DyadicSec\\ssl\\192.168.50.13.cert C:\\Users\\192.168
.50.12.cert\r\n+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~\r\n + CategoryInfo : ObjectNotFound: (z:String) [Copy
-Item], DriveNot \r\n FoundException\r\n + FullyQualifiedErrorId : DriveNot
Found,Microsoft.PowerShell.Commands.Copy \r\n ItemCommand\r\n \r\n", "stdout":
""}
if I run the ps1 on the vm its work fine
Driver isn't mounted I guess.
$ echo -e 'copy : Cannot find drive. A drive with the name 'z' does not exist.\r\nAt C:\\Users\\vagrant\\AppData\\Local\\Temp\\ansible-tmp-1427807499.79-10385112351761\r\n7\\test.ps1:1 char:1\r\n+ copy z:\\PROGRA~1\\DyadicSec\\ssl\\192.168.50.13.cert C:\\Users\\192.168.50.12.cert\r\n+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\r\n + CategoryInfo : ObjectNotFound: (z:String) [Copy-Item], DriveNot \r\n FoundException\r\n + FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.Copy \r\n ItemCommand\r\n \r\n'
copy : Cannot find drive. A drive with the name z does not exist.
At C:\Users\vagrant\AppData\Local\Temp\ansible-tmp-1427807499.79-10385112351761
7\test.ps1:1 char:1
+ copy z:\PROGRA~1\DyadicSec\ssl\192.168.50.13.cert C:\Users\192.168.50.12.cert
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (z:String) [Copy-Item], DriveNot
FoundException
+ FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.Copy
ItemCommand
$

Powershell ParentContainsErrorRecordException on shell namespace

I'm trying to write an unzip powershell script, but I'm running into issues with the $shell.NameSpace() function.
function unzip ($sourceFile, $destination){
$shell = new-object -com shell.application
$zip = $shell.NameSpace($sourceFile)
foreach($item in $zip.items()){
$shell.Namespace($destination).copyhere($item) #Error here
}
}
unzip "$PWD\folder.zip" $PWD
When I run this, I get an error on the second $shell.NameSpace() call.
You cannot call a method on a null-valued expression.
At C:\scriptDir\unzipScript.ps1:9 char:6
+ $shell.NameSpace($destination).copyhere($item)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : InvokeMethodOnNull
What I dont understand is why this is failing on the second call. Clearly PWD exists and is a directory based on the first parameter.
Shell.NameSpace takes a string. $PWD is a PathInfo object. You can use $pwd.Path instead.
unzip "$PWD\folder.zip" $PWD.Path
As an alternative you can use the .Net System.IO.Compression.ZipFile class. Here is a sample.

Resources