Can a function be used in a equation - powershell

Is there a way to call a function when using an equation within Powershell.
I am trying to something like the following however the last line returns an error You must provide a value expression....
function quote($str) {
return """" + $str + """";
};
$a= "abc: " + quote('hi'); # <-- Doesn't Work
I realize I could assign the quote to an intermediate variable and then do the concatenation ($q=quote('hi'); $a="abc: " + q$) however I am hoping there is a simpler syntax.

Is this what you mean:
function quote($str) {
return """" + $str + """";
};
$a= "abc: " + $(quote('hi'));
# edit: as per Joey's comment, this will also work:
$a= "abc: " + (quote('hi'));
Edit
Re-written using PowerShell syntax:
# Function name is verb-noun, from approved verbs
# https://msdn.microsoft.com/en-us/library/ms714428(v=vs.85).aspx
Function Add-Quote{
# parameters this function takes
param([string]$str)
# No need for return and semi colon.
# I tend to use return as it makes my code reading easier
"""" + $str + """"
}
$a = "abc: " + (Add-Quote -str 'hi')

You could use the format operator -f to insert the string.
function quote {
param($str)
"`"$str`""
}
$a = 'abc: {0}' -f (quote 'hi')

Related

Truncate, Convert String and set output as variable

It seems so simple. I need a cmdlet to take a two word string, and truncate the first word to just the first character and truncate the second word to 11 characters, and eliminate the space between them. So "Arnold Schwarzenegger" would output to a variable as "ASchwarzeneg"
I literally have no code. My thinking was to
$vars=$var1.split(" ")
$var1=""
foreach $var in $vars{
????
}
I'm totally at a loss as to how to do this, and it seems so simple too. Any help would be appreciated.
Here is one way to do it using the index operator [ ] in combination with the range operator ..:
$vars = 'Arnold Schwarzenegger', 'Short Name'
$names = foreach($var in $vars) {
$i = $var.IndexOf(' ') + 1 # index after the space
$z = $i + 10 # to slice from `$i` until `$i + 10` (11 chars)
$var[0] + [string]::new($var[$i..$z])
}
$names

list of reserved variables in mib2c

Where can I find the list of reserved variable names for mib2c "language"? I mean the possible variables that are not described here, like ${name}, which evaluates to the OID name that is passed as the argument to mib2c.c.
Are there any other variables like that?
Is there one that carries the name of the .conf file that was passed to the mib2c.
Looking at local/mib2c from net-snmp-5.7.3, the pre-populated variables are populated by the following code:
$outputName = $mibnode->{'label'} if (!defined($outputName));
$outputName =~ s/-/_/g;
$vars{'name'} = $outputName;
$vars{'oid'} = $oid;
$vars{'example_start'} = " /*\n" .
" ***************************************************\n" .
" *** START EXAMPLE CODE ***\n" .
" ***---------------------------------------------***/";
$vars{'example_end'} = " /*\n" .
" ***---------------------------------------------***\n" .
" *** END EXAMPLE CODE ***\n" .
" ***************************************************/";
So, you end up with the following pre-populated variables:
$name is the "output prefix" specified using the -f option (or $mibnode->{'label'}, whatever that is, if the -f option wasn't used), with dashes substituted with underscores.
$oid is the value of mib2c's argument (called "mibNode" in the usage help).
$example_start and $example_end are hard-coded strings.
That's it.
To create $config with the value of the -c argument (or mib2c.conf if the -c option wasn't used), you could alter mib2c to add the following to the assignments shown above:
$vars{config} = $configfile;
Alternatively, I believe the following will also create $config, but the value passed to the -c option will be prepended with a directory name:
#perleval $vars{config} = $configfile; 0#
You could try to obtain the original value with the following (which assumes the original value didn't contain a /);
#perleval $vars{config} = $configfile =~ m{([^/]+)\z}s ? $1 : undef; 0#
Completely untested. I don't know anything about SNMP or mib2c.

adding numbers returned from function in powershell

I have the following basic code in powershell where I'm calculating the value of x
function add3([int]$num) {
return ($num + 3);
}
[String]$argA = "AB";
[int]$x = (add3($argA.Length) + 2);
Write-Host($x)
Running this in ISE I'm getting the value 5 instead of 7.
In PowerShell, you call functions by listing the arguments separated by spaces, not commas, and parentheses are not used. This is a common source of confusion. Your expression:
[int]$x = (add3($argA.Length) + 2);
Is a call to the function add3 with three arguments: ($argA.Length) and the strings + and 2. Since your function has only one argument, the result is 2 + 3 = 5 and the other two are discarded (calling a function with extraneous parameters is not an error in PowerShell). You can verify this is what's happening by changing your function:
function add3([int]$num, $y, $z) {
Write-Host $y;
Write-Host $z;
return ($num + 3);
}
The solution is to parenthesize the expression properly:
$x = (add3 $argA.Length) + 2;
You're getting the correct output.
Try to use this approach instead:
[int]$x = (add3($argA.Length)) + 2
function add3($num) {
return ($num + 3);
}
[String]$argA = "AB";
$x = (add3($argA.Length)) +2
Write-Host($x)

How can I treat a string as a sequence of hexadecimal values in PowerShell?

I have a string of characters in PowerShell like so:
Encoded: A35C454A
I want to treat each character as a hexadecimal value. In Ruby, this is as simple as Encoded[0].hex. How can I do this in PowerShell?
Simples:
[Convert]::ToInt32($encoded[0], 16);
(ToInt16 can be used too, but the built-in Int type is actually a shorthand for Int32)
please have a look
function asciiToHex($a)
{
$b = $a.ToCharArray();
Foreach ($element in $b) {$c = $c + "%#x" + [System.String]::Format("{0:X}",
[System.Convert]::ToUInt32($element)) + ";"}
$c
}
http://learningpcs.blogspot.in/2009/07/powershell-string-to-hex-or-whatever.html

perl assign string on multiple lines

I'd like to be able to assign the concatenation of multiple strings to a variable.
I'm looking for something like this:
$variable = 123
$string = "hello" +
"this is a " +
$variable +
"string";
Is it possible to do something along these lines in perl?
+ does addition. . does concatenation.
my $variable = 123;
my $string = "hello" .
"this is a " .
$variable .
"string";
From perlop #Additive Operators:
Additive Operators
Binary + returns the sum of two numbers.
Binary - returns the difference of two numbers.
Binary . concatenates two strings.
Therefore, for string concatenation you need:
$string = "hello" .
"this is a " .
$variable .
"string";