I'm tinkering with JobDSL and can't seem to find a way to run several powershell commands in one go. Example:
job('whatever'){
steps{
powershell("""$var = $env:mybuildvar
cmdlet2 $var""")
}
}
How do I achieve this? Thanks!
it seems that """ """ works for batch, but not for powershell.
Also, if I try to use $var with escaping or without JobDSL fails with
ERROR: (sandbox_CI_Dev, line 15) No signature of method:
javaposse.jobdsl.dsl.helpers.step.StepContext.powershell() is
applicable for argument types: (java.lang.String) values: [$var =
$ENV:mybuildvar]
The error is reproducable on jobdsl playground (http://job-dsl.herokuapp.com/), use following code (or anything similar to code above):
job('whatever') {
steps{
powershell("write-output $")
}
}
also powershell('write-output test; write-output test') doesn't work
The method name is powerShell, not powershell. See https://jenkinsci.github.io/job-dsl-plugin/#path/job-steps-powerShell.
And Groovy interpolates double quoted strings, see String interpolation. You need to use single quotes to avoid the interpolation if you want to use the dollar sign ($), e.g. '$var'. Use triple single quotes for multiline strings.
job('whatever'){
steps{
powerShell('''$var = $env:mybuildvar
cmdlet2 $var''')
}
}
Related
How it will look this using preg_replace_callback?
$str = preg_replace('/\&\#([0-9]+)\;/me', "code2utf('\\1',{$lo})", $str);
If I am not mistaken you want to use preg_replace_callback instead of using the /e modifier.
If you want to pass extra parameters to the callback function you could make use of the use indentifier or wrap the callback in another function.
The second example could look like:
$str = preg_replace_callback(
'/\&\#([0-9]+)\;/m', function ($matches) use ($lo) {
// function body with return statement
}, $str
);
Notes
Your regex \&\#([0-9]+)\; will match a string like 𸽡. I think you don't have to ecape the & and #.
In your code you use return strtoupper($matches[1], $lo); but strtoupper takes one parameter instead of 2 parameters.
If this is what you want to match, then when running your code you could see that $matches[1] contains "233333" so this will be called return strtoupper("233333");
I need to parse some C# files to get the values of some constant variables. I know I can do something like
$input = Get-Content C:\somefile.cs ...
then loop over each line and do some text matching.
...but was wondering whether I can utilize some sort of a C# DOM object to get the values of constants?
You can load the type dynamically from the powershell command line, and then evaluate the constant.
Example:
C:\somefile.cs contents:
public static class Foo
{
public const string SOME_CONSTANT = "StackOverflow";
}
Powershell command line:
Add-Type -Path C:\somefile.cs
$constantValue = [Foo]::SOME_CONSTANT
From a main PowerShell script I want to call another PowerShell script with an optional parameter and another non optional string array parameter. Something like this (with the dysfunctional call included for explanatory reasons):
$theOptionalParam = "maybe"
$theArrayParam = "A", "B"
$theDirectory = "SomeRelativePath"
#This is the part not working:
.\$theDirectory\SubScript.ps1 -OptionalParam $theOptionalParam -ArrayParam $theArrayParam
The SubScript.ps1 starts off like this:
[CmdletBinding()]
Param(
[Parameter(Mandatory=$false)][string]$OptionalParam,
[Parameter(Mandatory=$true)][string[]]$ArrayParam
)
But no matter what I try, I get an error or simply the first value of the array ("A") and the rest is discarded.
How do I execute the subscript correct with both the dynamic path, optional parameter and array parameter?
To interpret .\$theDirectory\SubScript.ps1 as expandable string and expand value of $theDirectory variable, you should use invoke operator &.
& .\$theDirectory\SubScript.ps1 -OptionalParam $theOptionalParam -ArrayParam $theArrayParam
You can see that by looking at parsed AST:
{& .\$theDirectory\SubScript.ps1 -OptionalParam $theOptionalParam -ArrayParam $theArrayParam}.
Ast.EndBlock.Statements[0].PipelineElements[0].CommandElements[0].GetType().Name
# ExpandableStringExpressionAst
{.\$theDirectory\SubScript.ps1 -OptionalParam $theOptionalParam -ArrayParam $theArrayParam}.
Ast.EndBlock.Statements[0].PipelineElements[0].CommandElements[0].GetType().Name
# StringConstantExpressionAst
As you can see, if you does not use & invoke operator, then .\$theDirectory\SubScript.ps1 interpreted as constant string, not as expandable string.
Lets assume I have the following Java code:
public String foo()
{
// returns foo()
String log = "foo() : Logging something!"
return log;
}
Can I search in Eclipse for foo() occurring only in a String literal, but not anywhere else in the code? So in the example here Eclipse should only find the third occurrance of foo(), not the first one, which is a function name and not the second one, which is a comment.
Edit: Simple Regular Expressions won't work, because they will find foo() in a line like
String temp = "literal" + foo() + "another literal"
But here foo() is a function name and not a String literal.
You can try it like this:
"[^"\n]*foo\\(\\)[^"\n]*"
You have to escape brackets, plus this regex do not match new lines or additional quotes, which prevent wrong matches.
Maybe you should use regex to find any occurence of foo() between two " ?
The following script gives me what I want but Perl also throws me a warning saying "Useless use of a variable in void context". What does it mean?
use strict;
use warnings;
my $example = 'http\u003a//main\u002egslb\u002eku6\u002ecom/c0/q7LmJPfV4DfXeTYf/1260269522170/93456c39545857a15244971e35fba83a/1279582254980/v632/6/28/a14UAJ0CeSyi3UTEvBUyMuBxg\u002ef4v\u002chttp\u003a//main\u002egslb\u002eku6\u002ecom/c1/q7LmJPfV4DfXeTYf/1260269522170/3cb143612a0050335c0d44077a869fc0/1279582254980/v642/10/20/7xo2MJ4tTtiiTOUjEpCJaByg\u002ef4v\u002chttp\u003a//main\u002egslb\u002eku6\u002ecom/c2/q7LmJPfV4DfXeTYf/1260269522170/799955b45c8c32c955564ff9bc3259ea/1279582254980/v652/32/4/6pzkCf4iqTSUVElUA5A3PpMAoA\u002ef4v\u002chttp\u003a//main\u002egslb\u002eku6\u002ecom/c3/q7LmJPfV4DfXeTYf/1260269522170/cebbb619dc61b3eabcdb839d4c2a4402/1279582254980/v567/36/19/MBcbnWwkSJu46UoYCabpvArA\u002ef4v\u002chttp\u003a//main\u002egslb\u002eku6\u002ecom/c4/q7LmJPfV4DfXeTYf/1260269522170/1365c39355424974dbbe4ae8950f0e73/1279582254980/v575/17/15/EDczAa0GTjuhppapCLFjtaQ\u002ef4v';
my #raw_url = $example =~ m{(http\\u003a.+?f4v)}g;
my #processed_url = map {
s{\\u003a}{:}g,$_;
s{\\u002e}{.}g,$_;
s{\\u002d}{#}g,$_;
} #raw_url;
print join("\n",#processed_url);
And why this map thing doesn't work if I omit those dollar underscores like so?
my #processed_url = map {
s{\\u003a}{:}g;
s{\\u002e}{.}g;
s{\\u002d}{#}g;
} #raw_url;
When I omit those dollar underscores, I get nothing except for a possibly success flag "1". What am I missing? Any ideas? Thanks like always :)
What you want is...
my #processed_url = map {
s{\\u003a}{:}g;
s{\\u002e}{.}g;
s{\\u002d}{#}g;
$_;
} #raw_url;
A map block returns the value composed of the last statement evaluated as its result. Thats why we pass the $_ as the last statement. The substitution operator s{}{} returns the number of substitutions made.
In your prior setup, you had by itself the following statement. Which is pretty much meaningless and that is what Perl is warning about.
s{\\u003a}{:}g, $_;
You already have the answer you were looking for, but I wanted to point out a subtlety about using the substitution operator inside a map block: your original array is also being modified. If you want to preserve the original array, one way to do it is to make a copy of the array, then modify only the copy:
my #processed_url = #raw_url;
for (#processed_url) {
s{\\u003a}{:}g;
s{\\u002e}{.}g;
s{\\u002d}{#}g;
}
Or, if you only need one array, and you want the original to be modified:
for (#raw_url) {
s{\\u003a}{:}g;
s{\\u002e}{.}g;
s{\\u002d}{#}g;
}