Accessing an object property with a "#" in the property name in Powershell? - powershell

I have an array of objects that comes back from a REST API call. The objects are "name" and "#text". Terrible names, I know, but I have no control over that. How do I get the value of both? I can get the value of #text for one entry via select -ExpandProperty `#text. I'm trying to push all the entries through the pipeline for further processing and coming up short on how to reference the #text in subsequent pipeline commands. I've tried selecting out the name and #text and renaming #text to something like 'value' but the back-tick escape used in a computed variable like below doesn't appear to work:
$bxml.properties.property|select #{n='Name';e={$_.name}},#{n='Value';e={$_.`#text}}
Anybody have suggestions as to how to code this so I can use the value of #text further down the pipeline?

Enclose the value in ' '
$bxml.properties.property|select #{n='Name';e={$_.name}},#{n='Value';e={$_.'#text'}}

Related

Using a variable in the Request Body form in Azure Data Factory Copy Data activity

Is it possible to use a variable in the "Request body" field in a Copy data activity?
This is my pipeline:
Output of "Get requestBody":
Allready full of unwanted slashes.
Output of "Set variable":
I using this expression in the "Request body" Field:
#replace(variables('requestBody'),'\','')
This is the input of "Copy data":
I cant get rid of all slashes. Is it even possible to use a variable for "Request body"?
As your lookup activity output returns JSON string value it includes an escape character backslash '\'.
To remove the escape character, create an array type variable and convert the lookup out to JSON array in append variable activity as below.
Create array variable.
Lookup output: As I checked firstrow only property in lookup activity, the output results firstrow only.
In the Append variable activity, get the lookup output and convert it to JSON array.
#json(activity('Lookup1').output.firstrow.requestbody)
Append variable result: Use this variable in later activities.
You can refer to these SO & SO threads for reference.
To remove the backslashes, I had to use this:
#replace(string(variables('requestBody')), '\"', '"')

How to add bash array variable to mongo update query

I am writing a bash script which updates a mongo document. If it's a new document then it creates a new document in mongo else updates it.
I want to pass bash array variable to the mongo query since few fields are of array type. I am not sure how to pass the array field. Any help would be appreciated.
This is my query:
db.my_col.update({emp_id: '"'$emp_id'"'}, {$set: {contacts: '"'${contacts}'"', emp_name: '"'$emp_name'"'}}, {upsert: true})
If I just add like normal variable the only first value in variable gets added.
If you put your array variable into a string, only the first item will be there, right
Basically, you get all of them with this syntax ${contacts[#]}, but they will be unquoted and separated with spaces and likely won't work in a query because of wrong syntax.
So you'd need to manually convert a Bash array to a JS array. I don't know an automagical way to do it, but consider this function:
function jsarray {
res=""
for a in ${#}; do
res="$res,\"$a\""
done
echo [ ${res:1} ]
}
It goes through it's arguments, wraps each one in quotes and joins together with commas.
Use it like this (I assume $contacts is an array of string values):
IFS=""
contacts=("skype name" "email address" phone website whatever)
jscontacts=$(jsarray $contacts[#])
# Proceed to using $jscontacts in your query.
After that $jscontacts will be a string value of [ "skype name","email address","phone","website","whatever" ].
Notes:
IFS="" - IFS stands for internal field separator, this is how Bash separates arguments from each other. By default it's " " (a space). Resetting it to nothing to preserve array items with spaces in them; otherwise "skype name" beсome two separate values of "skype" & "name".
${res:1} - skips the first character from $res (because it's a comma).
jscontacts=$(...) captures the terminal output into a jscontacts variable.

How to print variable inside function

I'm using RRDs::Simple function and it needs bunch of parameters.
I have placed these parameters in a special variable (parsing, sorting and calculating data from a file) with all quotes, commas and other stuff.
Of course
RRDs::create ($variable);
doesn't work.
I've glanced through all perl special variables and have found nothing.
How to substitute name of variable for the data that contained in that variable?
At least could you tell me with what kind of tools(maybe another special variables) it can be done?
Assuming I'm understanding what you're asking:
You've build the 'create' data in $variable, and are now trying to use RRDs::create to actually do it?
First step is:
print $variable,"\n"; - to see what is actually there. You should be able to use this from the command line, with rrdtool create. (Which needs a filename, timestep, and some valid DS and RRA parameters)
usually, I'll use an array to pass into RRDs::create:
RRDs::create ( "test.rrd", "-s 300",
"DS:name:GAUGE:600:U:U", )
etc.
If $variable contains this information already, then that should be ok. The way to tell what went wrong is:
if ( RRDs::error ) { print RRDs::error,"\n"; }
It's possible that creating the file is the problem, or that your RRD definitions are invalid for some reason. rrdtool create on command line will tell you, as will RRDs::error;

How to display the value of a Perl variable in a tooltip

I've got a CGI perl script which processes a large Application Configuration file and displays extracted config data in table format.
One of the columns shows the hostname for a given server extracted from the config file.
What I'd like to do is have the alias name displayed (stored in $alias) when the user hovers over the hostname - much like a tooltip.
Each table row will have a different hostname and alias and I'm creating the table by iterating through an array.
I'm using the object-oriented approach to CGI Perl.
I tried the using the following:
print $cgi->start_td({class=>'primpeer',title=>'$aliasName'}),"$hostName";
but this just echoed the $aliasName in the tooltip rather than the contents of $aliasName
Put the value of your $alias into a title attribute (presumably of a td element). It will produce a tooltip for you like the alt attribute of an img element.
Variables inside single quote are not interpolled.
Just use double quote instead of single quote:
print $cgi->start_td({class=>'primpeer',title=>"$aliasName"}),"$hostName";
here __^ here __^
or without any quotes:
print $cgi->start_td({class=>'primpeer', title=>$aliasName}), $hostName;

Powershell script problem (Get-content vs assigning to variable)

I'm attempting to write a Twitter Powershell script that will use community created interfaces PoshTwitter with the Twitter API to attempt and find a list of followers who are potential spammers.
I have a feeling that my problem lies not with the particular cmdlet I'm calling (Get-TwitterFollowers), but rather with the difference between assigning a variable:
If I try this:
$rawFol = get-twitterfollowers -page $page -raw 1
$rawFol is different than if I do this:
get-twitterfollowers -page $page -raw 1 > .\page$page.txt
$rawFol = gc .\page$page.txt
The Get-TwitterFollowers cmdlet returns an XML file converted to string.
What things can I try to determine the differences between these two assignments? They look like they'd result with same content.
The difference you're seeing is how powershell handles new lines in strings. When calling the get-twitterfollowers CmdLet, it is either returning a single string or an array of strings. My guess by your description is that it returns a string. So the $rawFol variable will have a single string value. Any new lines are simply embedded into the string value.
The second command you write the return to a file. Now all of the newlines in the string are represented as lines in the file. Later when you call gc on that file, each line will be returned as a separate string. So the $rawFol variable will now have an array of strings.