Powershell can't access Object attributes field and values - powershell

I'm working on to get the cmd command output in Powershell. The cmd command I ran as follows.
$obj = cmd.exe /c 'twiddle -s localhost -u xyz -p abc get "service=tempservice" stats'
If I print the object it prints the value as
Stats=[Ljava.lang.String;#c43b53
I want to access the value of this string object in PowerShell so that I can have all the fields and values of this $obj. I'm looking to get the value of one attribute and field named "count=1020132" inside the stats.
My Get-Member doesn't have any property or method which can help me in that.
I tried doing $obj."property" but didn't help.

I would prefer to do it with regex using named capture groups.
Please be so kind and adapt the regular expression to your needs, the used one is only an example.
$str ='Stata=[Ljava.lang.String;#c43b53';
[regex]$rx = "(?<trailer>\S+)\=\[(?<first>\S+)\.(?<second>\S+)\.(?<third>\S+);(?<leader>\S+)";
$m = $rx.Match($str);
$m;
$m.Groups["trailer"].Value;
$m.Groups["first"].Value;
$m.Groups["second"].Value;
$m.Groups["third"].Value;
$m.Groups["leader"].Value;

Related

PowerShell - Merge two variables into one

I'm learning PowerShell so please forgive (what I'm sure is) a simple question.
I'm used to coding BATCH scripts and if I wanted to merge %USERDOMAIN% and %USERNAME% I would:
set zFullUsername=%USERDOMAIN%\%USERNAME%
echo %zFullUsername%
How can I do the same in PowerShell?
Thank you for your time.
On a supported Operating System, I wouldn't even bother with environment variables for this:
$zFullUsername = whoami
Then just access it as required:
$zFullUsername
In PowerShell, you can access environment variables in a few different ways. The way I recommend is to use the $env:VAR variable to access them.
$user = $env:USERNAME
$domain = $env:USERDOMAIN
echo "$domain\$user"
Note: \ is not an escape character in the PowerShell parser, ` is.
Similarly to rendering the echo command (echo is an alias of Write-Output btw) you can create a username variable like so:
$fullUserName = "$domain\$user"
Or you can skip right to creating $fullUserName straight from the environment variables:
$fullUserName = "${env:USERDOMAIN}\${env:USERNAME}"
Note: When variables have non-alphanumeric characters in them, the ${} sequence tells PowerShell everything between the ${} is part of the variable name to expand.
It seems the : in $env:VAR is actually an exception to this rule, as"Username: $env:USERNAME" does render correctly. So the ${} sequence above is optional.
To avoid confusion when trying to apply this answer in other areas, if you needed to insert the value of an object property or some other expression within a string itself, you would use a sub-expression within the string instead, which is the $() sequence:
$someVar = "Name: $($someObject.Name)"
When using either ${} or $(), whitespace is not allowed to pad the outer {} or ().

Set variable in a single PowerShell line

Can any of you let me know how to state a variable in a PowerShell line? Just like I could do using PowerShell ISE I would like to be able to do so via the plain console.
You can assign values to a PowerShell variable by combining a variable name, an assignment operator, and an expression. Here is a simple example:
>> $a = 1 + 1
If you want to populate multiple variables with the same value, you can save some typing as in the example below:
>> $a = $b = $c = 1
You can also define multiple variables with different values on one line:
>> $a, $b, $c = 1, 2, 3
To display the value of a variable, you don’t need a special command as in many other programming languages; entering the variable name is enough. This works in a script and on a command prompt.
>> $c
To take values from user, similar to Python input() function you can use:
$Number = Read-Host "Please enter a number"
Read the official documentation here.
I finally found the solution to get the job done. I just set the variable and end it with a semi-colon. Then I can go on with scripting.
Thank you all.

Passing a variable to a command in a script

I've been searching all over the place and since I'm taking my first steps in PERL this might be one of he dumbest questions but here it goes.
So I'm creating a script to manage my windows and later bind it to keyboard shortcuts, so I I'm trying to run a command and passing some variables:
my $command = `wmctrl -r :ACTIVE: -e 0,0,0,$monitors->{1}->{'width'}/2,$monitors->{1}->{'height'}`;
But I get an error saying I'm not passing the right parameters to the command, but if I do this, everything works great:
my $test = $monitors->{1}->{'width'}/2;
my $command = `wmctrl -r :ACTIVE: -e 0,0,0,$test,$monitors->{1}->{'height'}`;
So do I really have to do this? assign it first to a variable and then pass it, or there's a more elegant way of doing it?
The backticks operator (or the qx{}) accepts A string which is (possibly) interpolated. So accepts string and not expression like $var/2.
Thats mean than the $variables ($var->{1}->{some} too) are expanded but not the arithmetic expressions.
Therefore your 2 step variant works, but not the first.
If you want evaluate an expression inside the string you can use the next:
my $ans=42;
print "The #{[ $ans/2 ]} is only the half of answer\n";
prints
The 21 is only the half of answer
but it is not very readable, so better and elegant is what you're already doing - calculate the command argument in andvace, and to the qx{} or backticks only pass the calculated $variables.

how to retrieve array variable elements passed as a command line argument to Expect script

I have a Perl script that invokes an Expect script with command line arguments passed to it.
I wanted to know if we pass an array variable as argument, how do we retrieve the array variable in expect script?
For a simple variable passed as command line argument : set var [lindex $argv 0] is used.
I need to know how to access array variable in similar fashion and use its elements in the expect script. a sample code would be of great help!
looking forward for responses,
Thank you.
The expect global variable $argv is a list (i.e. a perl array). If you want to capture it in a different variable:
set myvar $argv
Or use it directly
foreach item $argv {
do something with $item
}

SyncRoot property in Powershell

This question is regarding the function forward_dns from the following blog:
http://powershellmasters.blogspot.com/2009/04/nslookup-and-powershell.html
So say I have a piece of code in powershell that looks like this:
$cmd = "nslookup google.com " + $NameserverIPAddress;
$result = Invoke-Expression ($cmd);
This snippet uses the nslookup DOS command to do a DNS lookup. Since it's a DOS command, the object returned by Invoke-Expression is basically an array of strings, one for each line of output.
In the example function, in order to retrieve line 4 of the output, the original author uses the following syntax:
$result.SyncRoot[3];
I found that this also works just fine:
$result[3];
What is the purpose of SyncRoot in this context?
There is no purpose in this example.
SyncRoot property is a way to treat in a safe manner ( generally with a lock in .net) arrays handled by more that one thread. see here and here