Powershell: Pass result of Get-AzTableRow to function - azure

I am using below PowerShell code in azure functions to retrieve rows of users in azure table who belongs to a particular location.
$Rows = Get-AzTableRow -table $cloudTable -customFilter "((Location eq '$loc') and (PartitionKey eq 'User'))"
Next, I need to pass the result ($Rows) of the above query to a function as parameter. Tried to define the function as
function display_rows($param){
$tempcount = $param.Count
for($i=0; $i -lt $tempcount; $i++){
...code body...
}
}
and invoke the function as display_rows "$Rows". But it seems it's not working as intended. Is this the proper way to pass the result of Get-AzTableRow to a function? please help me with this.

the basic problem is that you are sending a "stringified" version of the object to the function. [grin]
when you do ...
display_rows "$Rows"
... are not sending the object in $Rows to the function. you are converting that object into a string and sending that.
that simple string object aint what you want. [grin] you want the complex object instead.
so, the fix is to NOT wrap $Rows in double quotes. just use it bare, like so ...
display_rows $Rows
that will send the object contained in the $Var to the function, not the .ToString() of that object.
as an aside, you otta NEVER use quotes around a $Var unless you are sure you need them.

Related

Cannot parse url string for Microsoft graph because using the Invoke-MSGraphRequest command and query parameters

I cannot parse and make a call using the current URL because when I use the $filter and $select query parameters it breaks the string, yet it works great in Postman and give me all the data I needed.
Connect-MSGraph
Invoke-MSGraphRequest -Url "https://graph.microsoft.com/beta/deviceManagement/managedDevices?$select=emailaddress,id,imei,operatingSystem,ownerType,managedDeviceOwnerType&$filter=(operatingSystem eq 'iOS')" -HttpMethod GET
I need to filter these devices then if the ownership is personal, I was going to use graph API again to Update the object device using PATCH. Please help with this
https://learn.microsoft.com/en-us/graph/query-parameters#filter-parameter
https://learn.microsoft.com/en-us/graph/api/intune-devices-manageddevice-get?view=graph-rest-1.0
The immediate solution to your problem is to simply escape the verbatim $'s with a backtick `:
Invoke-MSGraphRequest -Url "https://graph.microsoft.com/beta/deviceManagement/managedDevices?`$select=emailaddress,id,imei,operatingSystem,ownerType,managedDeviceOwnerType&`$filter=(operatingSystem eq 'iOS')" -HttpMethod GET
Or to use single-quotes ' to avoid PowerShell attempting to expand what looks like variables - literal single-quotes inside the URL will have to be escaped by doubling them:
Invoke-MSGraphRequest -Url 'https://graph.microsoft.com/beta/deviceManagement/managedDevices?$select=emailaddress,id,imei,operatingSystem,ownerType,managedDeviceOwnerType&$filter=(operatingSystem eq ''iOS'')' -HttpMethod GET
That being said, I'd personally recommend constructing the query parameters from simpler parts:
$endpointURL = 'https://graph.microsoft.com/beta/deviceManagement/managedDevices'
# assign variable parts of the filter to a variable
$targetOperatingSystem = 'iOS'
# construct a hashtable containing all the query parameters
$GraphParameters = [ordered]#{
'$select' = 'emailaddress,id,imei,operatingSystem,ownerType,managedDeviceOwnerType'
'$filter' = "(operatingSystem eq '$targetOperatingSystem')"
}
# construct query string and final URL from the individual parts above
$queryString = $GraphParameters.GetEnumerator().ForEach({ $_.Key,$_.Value -join '=' }) -join '&'
$URL = $endpointURL,$queryString -join '?'
And then finally invoke Invoke-MSGraphRequest -Url $URL -HttpMethod Get

Using parameter array in Azure Automation Powershell script

For a function I want to use an array of DayOfWeek to exclude certain days from the automation script. For that I've setup the following function:
Param
(
[Parameter (Mandatory= $true)]
[ValidateNotNullOrEmpty()]
[DayofWeek[]] $ExcludeDays
)
foreach ($ExcludeDay in $ExcludeDays)
{
Write-Output $ExcludeDay
}
In the Azure testpane I've included the array as follows:
and this is the error it returns:
Failed
Cannot process argument transformation on parameter 'ExcludeDays'. Cannot convert value "Monday, Friday, Saturday" to type "System.DayOfWeek[]".
I've tried it simularly in Powershell by creating a function that takes the same parameter array and had no issue with similar input. Anybody knows how to get it working?
You should pass them as ['Monday','Friday','Saturday'].
You should pass them as ['Monday','Friday','Saturday']. as Joy answered
another solution would be get input as
Monday,Tuesday,Wednesday
and split it.
$CharArray = $InputString.Split(",")

HTML::TreeBuilder::XPath findvalue returns concatenation of values

The findvalue function in HTML::TreeBuilder::XPath returns a concatenation of any values found by the xpath query.
Why does it do this, and how could a concatenation of the values be useful to anyone?
Why does it do this?
When you call findvalue, you're requesting a single scalar value. If there are multiple matches, they have to be combined into a single value somehow.
From the documentation for HTML::TreeBuilder::XPath:
findvalue ($path)
...If the path returns a NodeSet, $nodeset->xpath_to_literal is called automatically for you (and thus a Tree::XPathEngine::Literal is returned).
And from the documentation for Tree::XPathEngine::NodeSet:
xpath_to_literal()
Returns the concatenation of all the string-values of all the nodes in the list.
An alternative would be to return the Tree::XPathEngine::NodeSet object so the user could iterate through the results himself, but the findvalues method already returns a list.
How could a concatenation of the values be useful to anyone?
For example:
use strict;
use warnings 'all';
use 5.010;
use HTML::TreeBuilder::XPath;
my $content = do { local $/; <DATA> };
my $tree = HTML::TreeBuilder::XPath->new_from_content($content);
say $tree->findvalue('//p');
__DATA__
<p>HTML is just text.</p>
<p>It can still make sense without the markup.</p>
Output:
HTML is just text.It can still make sense without the markup.
Usually, though, it makes more sense to get a list of matches and iterate through them instead of doing dumb concatenation, so you should use findvalues (plural) if you could have multiple matches.
Use
( $tree->findvalues('//p') )[0] ;
instead.

How to pass value from controller to view

i want to pass a variable to view file. i am using the following code:
View::factory('admin/modules/video_category')->set($x, $parent_id);
to pass value of $parent_id to $x but $x variable is showing null.
please advise how to do this correctly.
View::factory('some_view')->set('variable_name', $value);
and than in the view, you can call $variable_name.

preg_replace: reference object in replacement

Do you know of any way to reference an object in the replacement part of preg_replace. I'm trying to replace placeholders (delimited with precentage signs) in a string with the values of attributes of an object. This will be executed in the object itself, so I tried all kinds of ways to refer to $this with the /e modifier. Something like this:
/* for instance, I'm trying to replace
* %firstName% with $this->firstName
* %lastName% with $this->lastName
* etc..
*/
$result = preg_replace( '~(%(.*?)%)~e', "${'this}->{'\\2'}", $template );
I can't get any variation on this theme to work. One of the messages I've been getting is: Can't convert object Model_User to string.
But of course, it's not my intention to convert the object represented by $this to a string... I want to grab the attribute of the object that matches the placeholder (without the percentage signs of course).
I think I'm on the right track with the /e modifier. But not entirely sure about this either. Maybe this can be achieved much more simple?
Any ideas about this? Thank you in advance.
Like I commented to Paul's answer: in the meanwhile I found the solution myself. The solution is much more simple than I thought. I shouldn't have used double quotes.
The solution is as simple as this:
$result = preg_replace( '~(%(.*?)%)~e', '$this->\\2', $template );
Hope this helps anyone else for future reference.
Cheers.
Check out preg_replace_callback - here's how you might use it.
class YourObject
{
...
//add a method like this to your class to act as a callback
//for preg_replace_callback...
function doReplace($matches)
{
return $this->{$matches[2]};
}
}
//here's how you might use it
$result = preg_replace_callback(
'~(%(.*?)%)~e',
array($yourObj, "doReplace"),
$template);
Alternatively, using the /e modifier, you could maybe try this. I think the only way to make it work for your case would be to put your object into global scope
$GLOBALS['yourObj']=$this;
$result = preg_replace( '~(%(.*?)%)~e', "\$GLOBALS['yourObj']->\\2", $template );

Resources