Using parameter array in Azure Automation Powershell script - azure

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(",")

Related

How to write an expression that evaluates a pipeline parameter to TRUE/FALSE?

I have a pipeline parameter fed by a config file in SQL.
Sometimes that parameter will be empty, not NULL but just empty ('').
How do I write an expression that will evaluate the parameter to TRUE/FALSE(blank/not blank) that I can put into my IF activity?
Basic question but thanks a lot.
I tried
#pipeline().parameters.x = ''
but it just told me Parameter x = '' was not found .......
You can use the below expression in the if activity to evaluate a parameter is empty or not.
#empty(pipeline().parameters.ok)
Sample demonstration:
A sample parameter ok.
For example, purpose I have created a string variable which I will use inside if to check the output.
In if give the above expression.
Inside True activities I have given a set variable activity and gave some value and did the same inside false activities.
Output when the parameter value is not given(empty).
Output when we gave any value to the parameter

Powershell: Pass result of Get-AzTableRow to function

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.

Creating a powershell function that will take on ONE parameter and outputs a concatenated string value (fname+lname)

I'm looking for some guidance or tips for how to revise the function below. My goal is create a function that accepts one parameter and then returns a string. In this case the string will be a name of a super hero. The catch is that the function must output the full name concatenated together (ex. $FullName = $FName + "" +$LName). Can anyone give me some guidance on how I can accomplish this?
So far a have functional function that accepts one parameter but the output is the full name as one singular string. I am unable to find away to concatenate a first and last name and use it as my single parameter for this function. The commented out section of the function is just some trial and error code that I have attempted so far. Any help is appreciated!
The strings I'm working with:
Bat Man,
Wonder Woman,
Aqua Man
function Get-DCHero {
[CmdletBinding()]
param (
[Parameter(Mandatory=$true)][ValidateSet('Batman','Superman','Aquaman','Wonder Woman',ErrorMessage = "'{0}' is not a DC Super Hero. Please trying one of the following: '{1}'")]
[string]$Name
<#
[Parameter(Mandatory=$true)][ValidateSet('Batman','Superman','Aquaman','Wonder Woman',ErrorMessage = "'{0}' is not a DC Super Hero. Please trying one of the following: '{1}'")]
[string]$FullName = $FName + "" +$LName
[ValidateSet('Bat','Super','Aqua','Wonder','Flash',ErrorMessage = "'{0}' is not a DC Super Hero. Please trying one of the following: '{1}'")]
[string]$FName
[ValidateSet('Man','Man','Man','Woman','Flash',ErrorMessage = "'{0}' is not a DC Super Hero. Please trying one of the following: '{1}'")]
[string]$LName
#>
)
Write-OutPut "$Name is a DC Super hero."
}

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

How to remove milliseconds from utcnow() result in Azure data factory

I want to pass a value for parameter usertime, the value should be like 2020-07-23T13:19:31Z , which will be used in my source connection url.
For this i supplied utcnow() function in the value tab. But i realized utcnow() will return the value as "2018-04-15T13:00:00.0000000Z"
To remove the millisecond part i have used the expression substring(utcnow(),1,20).
and also used expression formatDateTime('utcnow()', 'yyyy-MM-ddTHH:mm:ss').
Both my trails are useless where my expression returning error ass invalid parameter.
Could you please help me how can i supply the value 2020-07-23T13:19:31Z in Azure data factory datasource parameters.
You don't want utcNow inside quotes, here is an example from one of my pipelines using your format:
#formatDateTime(utcnow(), 'yyyy-MM-ddTHH:mm:ss')
which gives this result, setting a variable named x:
{
"name": "x",
"value": "2020-07-24T13:44:42Z"
}
Build it in 'Add dynamic content' as you can pick the functions and it will format properly if you aren't familiar.
Your substring won't work because it requires a string for the first parameter and utcnow is a timestamp.

Resources