jq assignment where the path contain space - windows

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 ...).

Related

jq in a Jenkins pipeline not saving output to variable

So in my Jenkins pipeline I run a couple of curl commands across different stages. I store the ouput of Stage1 into a file and for every item in that list I run another curl command and use the output of that to extract some values using jq.
However from the second stage I can't seem to store the jq extracted values into variables to echo them later. What am I doing wrong?
{Stage1}
.
.
.
{Stage2}
def lines = stageOneList.readLines()
lines.each { line -> println line
stageTwoList = sh (script: "curl -u $apptoken" + " -X GET --url " + '"' + "$appurl" + "components/tree?component=" + line + '"', returnStdout: true)
pfName = sh (script: "jq -r '.component.name' <<< '${stageTwoList}' ")
pfKey = sh (script: "jq -r '.component.key' <<< '${stageTwoList}' ")
echo "Component Names and Keys\n | $pfName | $pfKey |"
}
returns in the end for Stage2
[Pipeline] sh
+ jq -r .component.name
digital-hot-wallet-gateway
[Pipeline] sh
+ jq -r .component.key
dhwg
[Pipeline] echo
Component Names and Keys
| null | null |
Any help in the right direction appreciated!
You passed true as the argument for the returnStdout argument to the shell step method for stageTwoList, but then forgot to use the same argument for the JSON parsed returns to the next two variable assignments:
def lines = stageOneList.readLines()
lines.each { line -> println line
stageTwoList = sh(script: "curl -u $apptoken" + " -X GET --url " + '"' + "$appurl" + "components/tree?component=" + line + '"', returnStdout: true)
pfName = sh(script: "jq -r '.component.name' <<< '${stageTwoList}' ", returnStdout: true)
pfKey = sh(script: "jq -r '.component.key' <<< '${stageTwoList}' ", returnStdout: true)
echo "Component Names and Keys\n | $pfName | $pfKey |"
}
Note you can also make this much easier on yourself by doing the JSON parsing natively in Groovy and with Jenkins Pipeline step methods:
String stageTwoList = sh(script: "curl -u $apptoken" + " -X GET --url " + '"' + "$appurl" + "components/tree?component=" + line + '"', returnStdout: true)
Map stageTwoListData = readJSON(text: stageTwoList)
pfName = stageTwoListData['component']['name']
pfKey = stageTwoListData['component']['key']

"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'
}
}

7Zip Powershell not working

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"

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.

Multiple statements using && [duplicate]

This question already has answers here:
Can I get "&&" or "-and" to work in PowerShell?
(13 answers)
Closed 9 years ago.
What would be the equivalent statement of CMD's:
dir && cd ..
in Powershell?
I tried:
dir -and cd ..
but it throws error:
Get-ChildItem : A parameter cannot be found that matches parameter name
'and'.
At line:1 char:5
+ dir -and (cd ..)
+ CategoryInfo : InvalidArgument: (:) [Get-ChildItem], ParameterBindingException
+ FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell
.Commands.GetChildItemCommand
There is not a direct equivalent in PowerShell of cmd.exe && which means "only execute right-hand side if the left-hand side succeeds." However you could write a short function to do the equivalent:
function IfTrue([ScriptBlock] $testExpression, [ScriptBlock] $runExpression) {
if ( & $testExpression ) { & $runExpression }
}
For example:
IfTrue { get-childitem "fileThatExists.txt" -ea SilentlyContinue } { "File exists..." }
If you want for the $testExpression to produce output, the IfTrue function can be written as follows:
function IfTrue([ScriptBlock] $testExpression, [ScriptBlock] $runExpression) {
& $testExpression
if ( $? ) { & $runExpression }
}
Bill
How about this?
dir; if ($?) {cd ..}
Running get-help about_automatic_variables | more explains:
$? Contains the execution status of the last operation. It contains
TRUE if the last operation succeeded and FALSE if it failed.
In PS, dir is just an alias for get-ChildItem; cd likewise for Set-Location.
edit: Same question here, including an answer straight from the horse's mouth.

Resources