Skip to content

Instantly share code, notes, and snippets.

@f-bader
Last active February 8, 2018 15:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save f-bader/870ac600a6520dd7eca15903e35456aa to your computer and use it in GitHub Desktop.
Save f-bader/870ac600a6520dd7eca15903e35456aa to your computer and use it in GitHub Desktop.
Start a Runbook with an array
param(
[Parameter(Mandatory = $true)]$Parameter
)
$Parameter.GetType()
if ($Parameter -is [array]) {
Write-Output "It's an array. No conversion necessary."
} else {
try {
$Parameter = $Parameter | ConvertFrom-Json
Write-Output "Converted from JSON to array"
} catch {
$Parameter = @($Parameter)
Write-Output "Converted from String to array"
}
}
$Parameter.GetType()
$Parameter
#region Connect to Azure with certificate
try {
# Get the connection "AzureRunAsConnection "
$servicePrincipalConnection = Get-AutomationConnection -Name "AzureRunAsConnection"
Write-Output "Logging in to Azure..."
Add-AzureRmAccount `
-ServicePrincipal `
-TenantId $servicePrincipalConnection.TenantId `
-ApplicationId $servicePrincipalConnection.ApplicationId `
-CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint | Out-Null
# Switch Azure subscription
Write-Output "Switch to Azure SubscriptionId $($servicePrincipalConnection.SubscriptionId)"
Select-AzureRmSubscription -SubscriptionId $servicePrincipalConnection.SubscriptionId | Out-Null
} catch {
if (!$servicePrincipalConnection) {
throw "Connection $connectionName not found."
} else {
throw $_.Exception
}
}
#endregion
$AzureAutomationRunbook = "Test-ObjectConversion"
$HybridWorker = "HybridWorkerGroup"
# Start runbook and parameter is an array
$AzureRunbookParameter = @{
"Parameter" = "String"
}
Get-AzureRmAutomationAccount | Start-AzureRmAutomationRunbook -Parameters $AzureRunbookParameter -Name $AzureAutomationRunbook -RunOn $HybridWorker
# Start runbook and parameter is an array
$AzureRunbookParameter = @{
"Parameter" = @("Blue","Jeans")
}
Get-AzureRmAutomationAccount | Start-AzureRmAutomationRunbook -Parameters $AzureRunbookParameter -Name $AzureAutomationRunbook -RunOn $HybridWorker
# Start runbook and parameter is an JSON
$AzureRunbookParameter = @{
"Parameter" = ( @("Blue","Jeans") | ConvertTo-Json )
}
Get-AzureRmAutomationAccount | Start-AzureRmAutomationRunbook -Parameters $AzureRunbookParameter -Name $AzureAutomationRunbook -RunOn $HybridWorker
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment