How to pass arguments to ADF pipeline using powershell - powershell

I have created a pipeline which has two parameters at pipeline level.
I want to send the values to these parameters using powershell and trigger the pipeline.
Any idea how to do it using Powershell.

I'll leave a script that you can then modify to your needs:
Login-AzureRmAccount
Select-AzureRmSubscription -Subscription "yourSubId"
$dfname = "youDataFActoryName"
$rgName = "yourResourceGroupName"
$pipe = "pipeName"
$parameters = #{
"param1" = "asdasd"
"param2" = "123456"
}
Invoke-AzureRmDataFactoryV2Pipeline -DataFactoryName $dfname -ResourceGroupName $rgName -PipelineName $pipe -Parameter $parameters
Hope this helped!

Parameter is to be passed as a hashtable. The hashtable is created using #
e.g :
$param = #{"year" = "2022"}
Invoke-AzDataFactoryV2Pipeline -ResourceGroupName "UAT" -DataFactoryName "ADF" -PipelineName "Snapshot" -Parameter $param

Related

Jenkins Pipeline Powershell Variables

I having problems with Jenkins Pipeline when trying to excecute a PS Script and pass in a variable from a parameter
I saw a similar problem to mine here
Powershell script in jenkins pipeline
pipeline{
agent any
parameters {
choice choices: ['enargas', 'ci','scada','despacho','degnet','degabi','cm','AccesoDegas'], name: 'MODULO'
}
stages{
stage('Nea Modulos'){
steps{
powershell(
script: '''
$srvPassword = ConvertTo-SecureString "pass" -AsPlainText -Force
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist "AD\\user", $srvPassword
$parameters = #{
ComputerName = '172.16.40.8'
Credential = $cred
ArgumentList =123
ScriptBlock = {
Param ($param1)
echo $param1
[System.Environment]::SetEnvironmentVariable( \'modulo\', ${params.MODULO}, [System.EnvironmentVariableTarget]::User);
}
}
Invoke-Command #parameters
''')
}
}
}
}
I am expecting that a variable is ceated on the remote pc but no value comes from de parameter

Add-AzKeyVaultNetworkRule on Powershell script throwing error

I need to add the Outbound IPs of Azure Function-App to Azure KeyVault Firewall Rule to be whitelisted, using powershell, to be executed on a pipeline. My script is :
param(
[Parameter()]
[String]$resourcegrp,
[String]$funcname,
[String]$kv
)
$functionApp = Get-AzFunctionApp -ResourceGroupName $resourcegrp -Name $funcname
Add-AzKeyVaultNetworkRule -VaultName $kv -ResourceGroupName $resourcegrp `
-IpAddressRange ($functionApp.PossibleOutboundIPAddress).Trim()
Update-AzKeyVaultNetworkRuleSet -VaultName $kv -ResourceGroupName $resourcegrp `
-DefaultAction Deny -Bypass AzureServices `
-IpAddressRange ($functionApp.PossibleOutboundIPAddress).Trim()
The above is giving below error:
where as if I do the same on powershell prompt like below, it works fine.
Can someone suggest what's wrong in my PS1 file, or what can be a better way to achieve the same with a PowerShell script.
This is because the property $functionApp.PossibleOutboundIPAddress is a single string
$functions[0].PossibleOutboundIPAddress | gm
TypeName: System.String
But Add-AzKeyVaultNetworkRule expects a string array
Get-Help Add-AzKeyVaultNetworkRule -Parameter ipaddressrange
-IpAddressRange <System.String[]>
Specifies allowed network IP address range of network rule.
You should be able to make this work by splitting the value from the functionApp on the , delimiter
$addAzKeyVaultNetworkRuleSplat = #{
VaultName = $kv
ResourceGroupName = $resourcegrp
IpAddressRange = $functionApp.PossibleOutboundIPAddress -split ','
}
Add-AzKeyVaultNetworkRule #addAzKeyVaultNetworkRuleSplat

Cannot find overload for ExecuteBatch error in powershell

I am doing batch uploads from a csv file to Azure table storage through a Powershell script and i have a command: $table.CloudTable.ExecuteBatch($batchOperation)
for which i'm getting the error mentioned in the header of the question of my post. I believe that "ExecuteBatch" is a method in the old AzureRm module and not the newer Az module which i am using, which is causing it to break. Is there a corresponding method in Az module for "ExecuteBatch"?
According to my test, if we use new Azure PowerShell module Az to manage Azure Table storage, we need to use the SDK Microsoft.Azure.Cosmos.Table.
So if ou want to use ExecuteBatch method, we need to use the command [Microsoft.Azure.Cosmos.Table.TableBatchOperation] $batchOperation = New-Object -TypeName Microsoft.Azure.Cosmos.Table.TableBatchOperation to create TableBatchOperation. For example:
Connect-AzAccount
$ResourceGroupName = "testfun06"
$StorageAccountName="testfun06bf01"
$TableName="People"
$keys=Get-AzStorageAccountKey -ResourceGroupName $ResourceGroupName -Name $StorageAccountName
$ctx = New-AzStorageContext -StorageAccountName $StorageAccountName -StorageAccountKey $keys[0].Value
$table = Get-AzStorageTable -Name $TableName -Context $ctx
$e = New-Object Microsoft.Azure.Cosmos.Table.DynamicTableEntity("Jim","test")
$e1 = New-Object Microsoft.Azure.Cosmos.Table.DynamicTableEntity("Jim","test1")
[Microsoft.Azure.Cosmos.Table.TableBatchOperation] $batchOperation = New-Object -TypeName Microsoft.Azure.Cosmos.Table.TableBatchOperation
$batchOperation.InsertOrMerge($e)
$batchOperation.InsertOrMerge($e1)
$table.CloudTable.ExecuteBatch($batchOperation)

Powershell Split function not working in VSTS Release pipeline

I have written a powershell script which takes multiple webapps(comma separated) as input.
I am splitting these webapps using powershell split function and configuring webapps by traversing each one of them using for-each loop.
Everything works fine in Powershell editor but when I configure the same script to VSTS release pipeline , split function doesn't work and which results in failure.
Input : devopstestwebapp1,devopstestwebapp2
Code : $WebAppName = $WebAppName.Split(',')
Output (After Split) : devopstestwebapp1 devopstestwebapp2
Error : The Resource 'Microsoft.Web/sites/devopstestwebapp1
devopstestwebapp2' under resource group 'DevOpsResourseGroup' was not found.
Following is my powershell script
# Parameters
param (
[Parameter(Position=0,mandatory=$true)]
[string] $AADAppID,
[Parameter(Position=1,mandatory=$true)]
[string] $AADKey,
[Parameter(Position=2,mandatory=$true)]
[string] $TenantId,
[Parameter(Position=3,mandatory=$true)]
[string] $ResourceGroupName,
[Parameter(Position=4,mandatory=$true)]
[string] $ServerName,
[Parameter(Position=5,mandatory=$true)]
[string] $RGLocation,
[Parameter(Position=6,mandatory=$true)]
[string] $WebAppName,
[Parameter(Position=7,mandatory=$true)]
[string] $SubscriptionName
)
# Connect to Azure
$ssAADKey = ConvertTo-SecureString $AADKey -AsPlainText -Force
$psCredential = New-Object System.Management.Automation.PSCredential($AADAppID, $ssAADKey)
Connect-AzureRmAccount -ServicePrincipal -Credential $psCredential -Subscription $SubscriptionName -TenantId $TenantId
write-host $WebAppName
$WebAppName = $WebAppName.Split(',')
write-host $WebAppName
Foreach ($servicename in $WebAppName)
{
write-host $servicename
  }
Below works perfectly with VSTS powershell task :
Store app name in variable :
$WebAppName = '$(WebAppName)'
write-host $WebAppName
foreach($servicename in $WebAppName.Split(','))
{
write-host $servicename
}
Output :
2019-04-22T11:02:02.7680996Z devopstestwebapp1,devopstestwebapp2,devopstestwebapp3
2019-04-22T11:02:02.7737101Z devopstestwebapp1
2019-04-22T11:02:02.7750490Z devopstestwebapp2
2019-04-22T11:02:02.7765756Z devopstestwebapp3
The problematic line is this one:
$WebAppName = $WebAppName.Split(',')
You are reassigning the result of split to the same variable $WebAppName which has been declared as a string in the parameter list. So the array result of Split will be cast to a string, not an array anymore.
The solution is to assign the result of split to a new variable:
$WebAppNameSplit = $WebAppName.Split(',')

How do I reliably get variable from child

I am building a sql Job Generator in Powershell. My PS skills aren't the greatest,
I want to get a value created in function CreateSqlTask. The variable I want is $job. I get an array of objects back from this function.
#My call
$returnParams = CreateSqlTask ( LIST OF PARAMS)
$returnParams[0] is the value of the jobSchedule Creation $returnParams[1] is the variable I want, this is the value of $job .
as a programmer I do not believe it is relable to just assume $returnParams[1] is always the variable I need. What is the proper way to handle this case?
#Here is the function implementation:
function CreateSqlTask
{
Param ( LIST OF PARAMS )
#Make all errors terminating
$ErrorActionPreference = "Stop"
#Create the SQL Job
$job = CreateSqlJob -serverInstance $serverInstance -jobName $jobName -jobDesc $jobDesc -jobCategory $jobCategory -jobAlertOperator $jobAlertOperator -jobEmailLevel $jobEmailLevel
#Create the SQL Job Step
$jobStep = CreateSqlJobStep $job $stepName $stepCmd
#Alter the Job to tell it what step should execute first
$job.StartStepID = $jobStep.ID
$job.Alter()
#Create the SQL Job Schedule
CreateSqlJobSchedule $job `
$schedName `
$schedFreqType `
$schedFreqRecurFactor `
$schedFreqInterval `
$schedFreqSubDayType `
$schedFreqSubDayInterval `
$startingSchedHour `
$startingSchedMinute `
$endingSchedHour `
$endingSchedMinute
return $job
}
A function should return only one type of object. If you don't need the job creation return, you can send that output to $null or assign it to a variable within the function. If you do need that along with the other information I'd create a custom object or hash table that includes that information along with the job information and return that.
As you have pointed out, both CreateSqlJobSchedule and return $job are returning values. If you need both of these then I suggest you assign these to a new object:
$jobDetails = #{
CreateSqlJob = $CreateSqlJob
CreateSqlJobSchedule = $CreateSqlJobSchedule
}
return $jobDetails
The above assumes you have assigned the two calls to two variables, you can then refer to them by name:
$createSqlTaskResults = CreateSqlTask
$createSqlTaskResults.CreateSqlJob
$createSqlTaskResults.CreateSqlJobSchedule
Here's how it would look in your example:
#Here is the function implementation:
function CreateSqlTask
{
Param ( LIST OF PARAMS )
#Make all errors terminating
$ErrorActionPreference = "Stop"
#Create the SQL Job
$CreateSqlJob = CreateSqlJob -serverInstance $serverInstance -jobName $jobName -jobDesc $jobDesc -jobCategory $jobCategory -jobAlertOperator $jobAlertOperator -jobEmailLevel $jobEmailLevel
#Create the SQL Job Step
$jobStep = CreateSqlJobStep $job $stepName $stepCmd
#Alter the Job to tell it what step should execute first
$job.StartStepID = $jobStep.ID
$job.Alter()
#Create the SQL Job Schedule
$CreateSqlJobSchedule = CreateSqlJobSchedule $job `
$schedName `
$schedFreqType `
$schedFreqRecurFactor `
$schedFreqInterval `
$schedFreqSubDayType `
$schedFreqSubDayInterval `
$startingSchedHour `
$startingSchedMinute `
$endingSchedHour `
$endingSchedMinute
$jobDetails = #{
CreateSqlJob = $CreateSqlJob
CreateSqlJobSchedule = $CreateSqlJobSchedule
}
return $jobDetails
}
One option you have is within the CreateSQLJob function to return a custom object that includes the pertinent information you want.
For instance:
$job = New-Object Object
$obj | Add-Member Noteproperty serverInstance -value $serverInstance
$obj | Add-Member Noteproperty jobName -value $jobName
Then you would be able to call into those properties:
$obj.jobName
The above is a contrived example.