How to create a schedule which triggers the runbook using PowerShell command? - powershell

I'm trying to create a schedule to trigger a runbook using PowerShell commands in Azure Automation account. Runbook is taking resourcevariable which is an automation variable as an input parameter.
I am able to create a schedule, but I am not able to register it as it is throwing the error:
Invalid runbook parameters
How can I register it and trigger a runbook ?
My code:
$TimeZone = ([System.TimeZoneInfo]::Local).Id
$scheduleName = "newschedule1"
$automationAccountName = "SentinelAutomationAccount"
$resourcegroupName = "myrg"
$RunbookName = "runbook1"
$params = #{"resourcevariable" = $resourcevariable;"flag" = $false}
$starttime = (Get-Date).AddMinutes(40)
$schedule = New-AzAutomationSchedule –AutomationAccountName $automationAccountName –Name $scheduleName –StartTime $starttime -OneTime -ResourceGroupName $resourcegroupName -TimeZone $TimeZone
Register-AzAutomationScheduledRunbook -AutomationAccountName $automationAccountName -RunbookName $RunbookName -ScheduleName $scheduleName -ResourceGroupName $resourcegroupName -Parameters $params
Runbook1 script: (5.1 version)
param (
[Parameter(Mandatory = $true)]
[string] $resourcevariable
)
# Login to Azure
try
{
"Logging in to Azure..."
Connect-AzAccount -Identity
}
catch {
Write-Error -Message $_.Exception
throw $_.Exception
}
$TimeZone = ([System.TimeZoneInfo]::Local).Id
$automationAccount = "xxxAutomationAccount"
$resourcegroup = "xxx"
$scheduleName = "myschedule"
$Runbook = "xxxRunBook"
$params = #{"resourcevariable" = "resourcevariable";"flag" = "false"}
$starttime = (Get-Date).AddMinutes(40)
$schedule = New-AzAutomationSchedule –AutomationAccountName $automationAccount –Name $scheduleName –StartTime $starttime -OneTime -ResourceGroupName $resourcegroup -TimeZone $TimeZone
Register-AzAutomationScheduledRunbook -AutomationAccountName $automationAccount -RunbookName $Runbook -ScheduleName $scheduleName -ResourceGroupName $resourcegroup -Parameters $params.Parameters
Runbook2 script: (7.1 version)
param (
[Parameter(Mandatory = $true)]
[string] $resourcevariable
)
# Login to Azure
try
{
"Logging in to Azure..."
Connect-AzAccount -Identity
}
catch {
Write-Error -Message $_.Exception
throw $_.Exception
}
echo "From Runbook2"

I tried it in my environment and found the same error.
When trying to retrieve parameters stored in the params variable, we should specify them as $params.parameters.
I modified the script and was able to correctly register and trigger the schedule.
Modified script:
$params = #{"resourcevariable" = "resourcevariable";"flag" = "false"}
Register-AzAutomationScheduledRunbook -AutomationAccountName $automationAccount -RunbookName $Runbook -ScheduleName $scheduleName -ResourceGroupName $resourcegroup -Parameters $params.Parameters
myschedule.ps1:
$TimeZone = ([System.TimeZoneInfo]::Local).Id
$automationAccount = "xxxxautomationaccount"
$resourcegroup = "xxxResourcegroup"
$scheduleName = "xxxxxschedule"
$Runbook = "xxxxrunbook"
$params = #{"resourcevariable" = "resourcevariable";"flag" = "false"}
$starttime = (Get-Date).AddMinutes(40)
$schedule = New-AzAutomationSchedule –AutomationAccountName $automationAccount –Name $scheduleName –StartTime $starttime -OneTime -ResourceGroupName $resourcegroup -TimeZone $TimeZone
Output:
Scheduled in Portal successfully:
Note: Run the same script in runbook with the changes given above to get the same results.

Related

Invoke-Pester -OutputFile and -OutputFormat are member of the legacy parameter set

In Pester 4 the commandlet offers the possibility to explicitly specify the OutputPath.
Invoke-Pester -Script $testFile -PassThru -Verbose -OutputFile $tr `
-OutputFormat NUnitXml -CodeCoverage "$tmp/*-*.ps1" `
-CodeCoverageOutputFile $cc -Show All
In version 5, this option is declared as a legacy parameter set and issues a corresponding warning.
WARNING: You are using Legacy parameter set that adapts Pester 5 syntax to Pester 4 syntax. This parameter set is deprecated, and does not work 100%. The -Strict and -PesterOption parameters are ignored, and providing advanced configuration to -Path (-Script), and -CodeCoverage via a hash table does not work. Please refer to https://github.com/pester/Pester/releases/tag/5.0.1#legacy-parameter-set for more information.
Which implementation is planned for the following versions? If the parameter is no longer available, should the test results be extracted from the result object?
There is pretty good documentation written for the new Pester 5 which you can find here: https://github.com/pester/Pester#simple-and-advanced-interface.
That link should take you to the specific section that you're looking for.
Essentially, they moved the configuration to an assembly class [PesterConfiguration]. You can access the defaults by using [PesterConfigruation]::Default or more likely cast it to a new object that you will configure with your specific settings and output path. You could do that like this as an example:
$configuration = [PesterConfiguration]#{
Run = #{
Path = $testFile
}
Output = #{
Verbosity = 'Detailed'
}
Filter = #{
Tag = 'Acceptance'
ExcludeTag = 'WindowsOnly'
}
Should = #{
ErrorAction = 'Continue'
}
CodeCoverage = #{
Enable = $true
OutputPath = $yourPath
}
}
You then pass that configuration object to Invoke-Pester. i.e. Invoke-Pester -Configuration $configuration
You can still use some of the parameters in that 'legacy' style, Pester will just yell at you so that you aren't too surprised when it gets deprecated.
As a side note- I do not see a nunit format for the test output so I don't know if they discontinued that. The only one I see is 'JaCoCo'.
I use Pester 5.1.1 to test Azure Resources after their deployment.
In Azure DevOps Services I execute a PowerShell task within a pipeline that triggers one script (Start-Pester.ps1) which in turn invokes tests from another script (PostDeployment.Tests.ps1) while passing the necessary parameter values.
Start-Pester.ps1
param(
[string]$SubscriptionId,
[string]$TenantId,
[string]$Username,
[string]$Password,
[string]$ResourceGroupName,
[string]$FunctionAppName,
[string]$EventHubNamespaceName,
[string]$EventHubNamespaceAuthorizationRuleName,
[string]$EventHubName,
[string]$EventHubAuthorizationRuleName,
[string]$EventHubAuthorizationRuleName1,
[string]$ModulePath,
[switch]$Publish,
[string]$ResultsPath
)
[string]$SubscriptionId = (Get-Item env:SubscriptionId).value
[string]$TenantId = (Get-Item env:TenantId).value
[string]$Username = (Get-Item env:Username).value
[string]$Password = (Get-Item env:Password).value
[string]$ResourceGroupName = (Get-Item env:ResourceGroupName).value
[string]$FunctionAppName = (Get-Item env:FunctionAppName).value
[string]$EventHubNamespaceName = (Get-Item env:EventHubNamespaceName).value
[string]$EventHubNamespaceAuthorizationRuleName = (Get-Item env:EventHubNamespaceAuthorizationRuleName).value
[string]$EventHubName = (Get-Item env:EventHubName).value
[string]$EventHubAuthorizationRuleName = (Get-Item env:EventHubAuthorizationRuleName).value
[string]$EventHubAuthorizationRuleName1 = (Get-Item env:EventHubAuthorizationRuleName1).value
$WarningPreference = "SilentlyContinue"
Set-Item Env:\SuppressAzurePowerShellBreakingChangeWarnings "true"
[array]$ModuleName = #("Az.Accounts", "Az.Resources", "Az.EventHub", "Az.Functions")
foreach ($Module in $ModuleName) {
Install-Module $Module -Scope CurrentUser -Force -SkipPublisherCheck -confirm:$false -AllowClobber
Import-Module -Name $Module
Get-InstalledModule -Name $Module -AllVersions | Select-Object Name, Version
}
# Authentication
$Credentials = New-Object System.Management.Automation.PSCredential ($Username, $(ConvertTo-SecureString $Password -AsPlainText -Force))
Connect-AzAccount -Credential $Credentials -ServicePrincipal -Tenant $TenantId
# Subscription
Set-AzContext -Subscription $SubscriptionId
$PesterModule = Get-Module -Name Pester -ListAvailable | Where-Object { $_.Version -like '5.*' }
if (!$PesterModule) {
try {
Install-Module -Name Pester -Scope CurrentUser -Force -SkipPublisherCheck -MinimumVersion "5.0" -Repository PSGallery
$PesterModule = Get-Module -Name Pester -ListAvailable | Where-Object { $_.Version -like '5.*' }
}
catch {
Write-Error "Failed to install the Pester module."
}
}
Write-Host "Pester version: $($PesterModule.Version.Major).$($PesterModule.Version.Minor).$($PesterModule.Version.Build)"
$PesterModule | Import-Module
if ($Publish) {
if (!(Test-Path -Path $ResultsPath)) {
New-Item -Path $ResultsPath -ItemType Directory -Force | Out-Null
}
}
$Tests = (Get-ChildItem -Path $($ModulePath) -Recurse | Where-Object { $_.Name -like "*Tests.ps1" }).FullName
$Params = [ordered]#{
Path = $Tests;
Data = #{
ResourceGroupName = $ResourceGroupName;
FunctionAppName = $FunctionAppName;
EventHubNamespaceName = $EventHubNamespaceName;
EventHubNamespaceAuthorizationRuleName = $EventHubNamespaceAuthorizationRuleName;
EventHubName = $EventHubName;
EventHubAuthorizationRuleName = $EventHubAuthorizationRuleName;
EventHubAuthorizationRuleName1 = $EventHubAuthorizationRuleName1;
}
}
$Container = New-PesterContainer #Params
$Configuration = [PesterConfiguration]#{
Run = #{
Container = $Container
}
Output = #{
Verbosity = 'Detailed'
}
TestResult = #{
Enabled = $true
OutputFormat = "NUnitXml"
OutputPath = "$($ResultsPath)\Test-Pester.xml"
}
CodeCoverage = #{
Enabled = $true
Path = $Tests
OutputFormat = "JaCoCo"
OutputPath = "$($ResultsPath)\Pester-Coverage.xml"
}
}
if ($Publish) {
Invoke-Pester -Configuration $Configuration
}
else {
Invoke-Pester -Container $Container -Output Detailed
}
PostDeployment.Tests.ps1
param(
[string]$ResourceGroupName,
[string]$FunctionAppName,
[string]$EventHubNamespaceName,
[string]$EventHubNamespaceAuthorizationRuleName,
[string]$EventHubName,
[string]$EventHubAuthorizationRuleName,
[string]$EventHubAuthorizationRuleName1
)
Describe "Structure Tests" {
BeforeAll {
if ($ResourceGroupName.Length -gt 0) {
$ResourceGroupData = Get-AzResourceGroup -Name $ResourceGroupName
}
if ($EventHubNamespaceName.Length -gt 0) {
$EventHubNamespaceData = Get-AzEventHubNamespace -ResourceGroupName $ResourceGroupName -Name $EventHubNamespaceName
$EventHubNamespaceAuthorizationRuleData = Get-AzEventHubAuthorizationRule -ResourceGroupName $ResourceGroupName -NamespaceName $EventHubNamespaceName -Name $EventHubNamespaceAuthorizationRuleName
}
if ($EventHubName.Length -gt 0) {
$EventHubData = Get-AzEventHub -ResourceGroupName $ResourceGroupName -NamespaceName $EventHubNamespaceName -EventHubName $EventHubName
$EventHubAuthorizationRuleData = Get-AzEventHubAuthorizationRule -ResourceGroupName $ResourceGroupName -NamespaceName $EventHubNamespaceName -EventHubName $EventHubName -Name $EventHubAuthorizationRuleName
$EventHubAuthorizationRuleData1 = Get-AzEventHubAuthorizationRule -ResourceGroupName $ResourceGroupName -NamespaceName $EventHubNamespaceName -EventHubName $EventHubName -Name $EventHubAuthorizationRuleName1
}
if ($FunctionAppName.Length -gt 0) {
$FunctionAppData = Get-AzFunctionApp -Name $FunctionAppName -ResourceGroupName $ResourceGroupName
}
}
# Resource Group
Context -Name "Resource Group" {
It -Name "Passed Resource Group existence check" -Test {
$ResourceGroupData | Should -Not -Be $null
}
}
# Event Hub Namespace
Context -Name "Event Hub Namespace" {
It -Name "Passed Event Hub Namespace existence check" -Test {
$EventHubNamespaceData | Should -Not -Be $null
}
It -Name "Passed Event Hub Namespace tier check" -Test {
$EventHubNamespaceData.Sku.Tier | Should -Be "Standard"
}
It -Name "Passed Event Hub Namespace TU check" -Test {
$EventHubNamespaceData.Sku.Capacity | Should -Be 1
}
It -Name "Passed Event Hub Namespace auto-inflate check" -Test {
$EventHubNamespaceData.IsAutoInflateEnabled | Should -Be $true
}
It -Name "Passed Event Hub Namespace maximum TU check" -Test {
$EventHubNamespaceData.MaximumThroughputUnits | Should -Be 2
}
It -Name "Passed Event Hub Namespace shared access policies check" -Test {
$EventHubNamespaceAuthorizationRuleData.Rights.Count | Should -Be 3
}
}
# Event Hub
Context -Name "Event Hub" {
It -Name "Passed Event Hub existence check" -Test {
$EventHubData | Should -Not -Be $null
}
It -Name "Passed Event Hub 'Listen' shared access policies check" -Test {
$EventHubAuthorizationRuleData.Rights | Should -Be "Listen"
}
It -Name "Passed Event Hub 'Send' shared access policies check" -Test {
$EventHubAuthorizationRuleData1.Rights | Should -Be "Send"
}
}
# Function App
Context -Name "Function App" {
It -Name "Passed Function App existence check" -Test {
$FunctionAppData | Should -Not -Be $null
}
It -Name "Passed Function App AppSettings configuration existence check" -Test {
$FunctionAppData.ApplicationSettings | Should -Not -Be $null
}
It -Name "Passed Function App APPINSIGHTS_INSTRUMENTATIONKEY existence check" -Test {
$FunctionAppData.ApplicationSettings.APPINSIGHTS_INSTRUMENTATIONKEY | Should -Not -Be $null
}
It -Name "Passed Function App FUNCTIONS_WORKER_RUNTIME value check" -Test {
$FunctionAppData.ApplicationSettings.FUNCTIONS_WORKER_RUNTIME | Should -Be "dotnet"
}
}
}
As you can see I am overwriting [PesterConfigruation]::Default with my configuration.
And yes, TestResult block with NUnitXml works as well.
Just add Publish Test Results task at the end of the pipeline. It will pick up the test results and publish them.
Hope this will help someone in the future.
They moved many settings to a new Configuration object.
Described here:
https://pester-docs.netlify.app/docs/commands/New-PesterConfiguration
Old:
Invoke-Pester -Script $testFile -PassThru -Verbose -OutputFile $tr `
-OutputFormat NUnitXml -CodeCoverage "$tmp/*-*.ps1" `
-CodeCoverageOutputFile $cc -Show All
New:
$configuration = [PesterConfiguration]#{
PassThru = $true
Run = #{
Path = $testFile
}
Output = #{
Verbosity = 'Detailed'
}
TestResult = #{
Enabled = $true
OutputFormat = "NUnitXml"
OutputPath = $tr
}
CodeCoverage = #{
Enabled = $true
Path = "$tmp/*-*.ps1"
OutputPath = $cc
}
}
Invoke-Pester -Configuration $configuration

Cannot find an overload for "ExecuteBatch" error in powershell for batch insert

I am doing a batch insert with powershell to Azure table. using latest Az modules, not AzureRm.
$context = New-AzStorageContext $storageAccountName -StorageAccountKey $storageAccountKey
$table = (Get-AzStorageTable –Name myTable –Context $context)
foreach($item in $items){
[Microsoft.WindowsAzure.Storage.Table.TableBatchOperation]$batchOperation = New-Object -TypeName Microsoft.WindowsAzure.Storage.Table.TableBatchOperation
$entity = New-Object -TypeName Microsoft.WindowsAzure.Storage.Table.DynamicTableEntity -ArgumentList $partitionKey, $rowKey
$entity.Properties.Add("ID", $id)
$batchOperation.InsertOrReplace($entity)
}
if ($batchOperation.Count -ne 0) {
$table.CloudTable.ExecuteBatch($batchOperation)
}
But am getting error as:
Cannot find an overload for "ExecuteBatch" and the argument count: "1"
"ExecuteBatch" is this method only available in the old AzureRm module?
ExecuteBatch operation is definitely available.
I believe you're getting this error is because you're using incorrect namespace. You should be using Microsoft.Azure.Cosmos.Table instead of Microsoft.WindowsAzure.Storage.Table.
Please try the following code. I tried it and it works:
$storageAccountName = "account-name";
$storageAccountKey = "account-key=="
$context = New-AzStorageContext $storageAccountName -StorageAccountKey $storageAccountKey
$table = (Get-AzStorageTable –Name myTable –Context $context)
foreach($item in $items){
[Microsoft.Azure.Cosmos.Table.TableBatchOperation]$batchOperation = New-Object -TypeName Microsoft.Azure.Cosmos.Table.TableBatchOperation
$entity = New-Object -TypeName Microsoft.Azure.Cosmos.Table.DynamicTableEntity -ArgumentList $partitionKey, $rowKey
$entity.Properties.Add("ID", $id)
$batchOperation.InsertOrReplace($entity)
}
if ($batchOperation.Count -ne 0) {
$table.CloudTable.ExecuteBatch($batchOperation)
}

Start-AzureRmAutomationRunbook is not accepting System.String parameter

I am trying to automate server reboot for one of our environment in Azure through azure automation.
I have created one runbook to know how many servers online.
Then trying pass the output as a parameter to another runbook and getting the
error.
below is the code.
$connectionName = "AzureRunAsConnection"
try
{
# Get the connection "AzureRunAsConnection "
$servicePrincipalConnection=Get-AutomationConnection -Name $connectionName
"Logging in to Azure..."
Add-AzureRmAccount `
-ServicePrincipal `
-TenantId $servicePrincipalConnection.TenantId `
-ApplicationId $servicePrincipalConnection.ApplicationId `
-CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint
}
catch {
if (!$servicePrincipalConnection)
{
$ErrorMessage = "Connection $connectionName not found."
throw $ErrorMessage
} else{
Write-Error -Message $_.Exception
throw $_.Exception
}
}
$Output = #()
$Running_XenApps = #()
$Output = .\Test.ps1
Foreach ($out in $Output)
{
$Running_XenApps += $out.Name | Out-String
}
Start-AzureRmAutomationRunbook –AutomationAccountName 'acm2eo-azure- automation' -Name 'Server-Reboot' -ResourceGroupName 'acm2eo-automation' -Parameters $Running_Xenapps -Runon acm2eo-hybrid-group1
I am getting below error.
Start-AzureRmAutomationRunbook : Cannot convert 'System.Object[]' to the type 'System.Collections.IDictionary' requiredby parameter 'Parameters'. Specified method is not supported.
Please let me know how to convert this.
Thanks in Advance.
You have to pass a hashtable / dictionary to the -Parameter param:
$params = #{
"YourParameterName" = $Running_XenApps
}
Start-AzureRmAutomationRunbook –AutomationAccountName 'acm2eo-azure- automation' -Name 'Server-Reboot' -ResourceGroupName 'acm2eo-automation' -Parameters $params -Runon acm2eo-hybrid-group1
As documented here: Start-AzureRmAutomationRunbook cmdlet.

Azure Runbook (workflow) output result to Table

I'm recreating an Azure Runbook to coincide with a Logic App functionality. Long story short, I want the Logic App to initiate the Runbook, grab the results from the Runbook and use them for the next steps in the Logic App.
Initially I wanted to grab the JSON output of starting some VMs, where I ended up with the output being like this:
{
"MyVM2": true
}
{
"MyVM1": true
}
I was then going to Parse the JSON to be used in the Runbook, but soon realising that I would not have a consisted number of results (maybe 2 VMs, or 20) I had found the Parse JSON schema to only Parse what I set the schema to be (2, in my case, so anything more would be missed).
Now I figure I could put my output to a table, then use that to allow the Logic App to look inside that table to pull my VM names and success results from. So, here is the Runbook that I've been mutilating:
workflow ShutDownStartByTagasdf
{
Param(
[Parameter(Mandatory=$true)]
[String]
$TagName,
[Parameter(Mandatory=$true)]
[String]
$TagValue,
[Parameter(Mandatory=$true)]
[Boolean]
$Shutdown
)
$connectionName = "AzureRunAsConnection";
try
{
# Get the connection "AzureRunAsConnection "
$servicePrincipalConnection=Get-AutomationConnection -Name $connectionName
# Logging in to Azure
$null = Add-AzureRmAccount `
-ServicePrincipal `
-TenantId $servicePrincipalConnection.TenantId `
-ApplicationId $servicePrincipalConnection.ApplicationId `
-CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint
}
catch {
if (!$servicePrincipalConnection)
{
$ErrorMessage = "Connection $connectionName not found."
throw $ErrorMessage
} else{
Write-Error -Message $_.Exception
throw $_.Exception
}
}
$vms = Find-AzureRmResource -TagName $TagName -TagValue $TagValue | where {$_.ResourceType -like "Microsoft.Compute/virtualMachines"}
Foreach -Parallel ($vm in $vms){
if($Shutdown){
$StopRtn = Stop-AzureRmVm -Name $vm.Name -ResourceGroupName $vm.ResourceGroupName -Force;
$objOut = [PSCustomObject]#{
VM = $vm.Name
Success = $StartRtn.IsSuccessStatusCode
}
}
else {
$StartRtn = Start-AzureRmVm -Name $vm.Name -ResourceGroupName $vm.ResourceGroupName;
$objOut = New-Object psobject -Property #{
VM = $vm.Name
Success = $StartRtn.IsSuccessStatusCode
}
}
$outPut = InlineScript {
$Using:objOut | Format-Table Vm,Success
}
}
}
Ignore the $objOut = [PSCustomObject]#{ part, that's just history of my JSON messing about, and I'm leaving it there for now because I'm not using the $Shutdwon = $True, only $False, which is the very last part after else {
this bit:
else {
$StartRtn = Start-AzureRmVm -Name $vm.Name -ResourceGroupName $vm.ResourceGroupName;
$objOut = New-Object psobject -Property #{
VM = $vm.Name
Success = $StartRtn.IsSuccessStatusCode
}
}
$outPut = InlineScript {
$Using:objOut | Format-Table Vm,Success
}
}
I was trying to implement something along the lines of what is already described here: Create Table with Variables in PowerShell
But there is no Output into the Azure Runbook console, but it does boot the VMs.
A very long winded explanation, but does anyone know how I could output to a Formatted Table inside a Workflow (Runbook) that will yield all my results in one table?
I ended up using an array to capture the info:
workflow ShutDownStartByTagasdf
{
Param(
[Parameter(Mandatory=$true)]
[String]
$TagName,
[Parameter(Mandatory=$true)]
[String]
$TagValue,
[Parameter(Mandatory=$true)]
[Boolean]
$Shutdown
)
$connectionName = "AzureRunAsConnection";
try
{
# Get the connection "AzureRunAsConnection "
$servicePrincipalConnection=Get-AutomationConnection -Name $connectionName
# "Logging in to Azure..."
$null = Add-AzureRmAccount `
-ServicePrincipal `
-TenantId $servicePrincipalConnection.TenantId `
-ApplicationId $servicePrincipalConnection.ApplicationId `
-CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint
}
catch {
if (!$servicePrincipalConnection)
{
$ErrorMessage = "Connection $connectionName not found."
throw $ErrorMessage
} else{
Write-Error -Message $_.Exception
throw $_.Exception
}
}
$result_array = #()
$vms = Find-AzureRmResource -TagName $TagName -TagValue $TagValue | where {$_.ResourceType -like "Microsoft.Compute/virtualMachines"}
Foreach -Parallel ($vm in $vms) {
if($Shutdown){
# Write-Output "Stopping $($vm.Name)";
$StopRtn = Stop-AzureRmVm -Name $vm.Name -ResourceGroupName $vm.ResourceGroupName -Force;
$objOut = New-Object -TypeName psobject -Property #{
VM = $vm.Name
Success = $StopRtn.IsSuccessStatusCode
}
}
else {
# Write-Output "Starting $($vm.Name)";
$StartRtn = Start-AzureRmVm -Name $vm.Name -ResourceGroupName $vm.ResourceGroupName;
$objOut = New-Object -TypeName psobject -Property #{
VM = $vm.Name
Success = $StartRtn.IsSuccessStatusCode
}
}
$workflow:result_array += $objOut
}
$result_array | ConvertTo-Json
}
The last bit $result_array | ConvertTo-Json allowed me to get a better output which I hope to use in the Logic App. The output:
[
{
"VM": "MyVM2",
"Success": true,
"PSComputerName": "localhost",
"PSShowComputerName": true,
"PSSourceJobInstanceId": "dadd87ad-1de1-432c-92b1-4c501c9a7ce8"
},
{
"VM": "MyVM1",
"Success": true,
"PSComputerName": "localhost",
"PSShowComputerName": true,
"PSSourceJobInstanceId": "dadd87ad-1de1-432c-92b1-4c501c9a7ce8"
}
]
I don't like how it's an array of objects, cos now I gotta figure out how to get some braces around it { } within the Logic App to utilise it better.

How can you delete all log files from an Azure WebApp using powershell?

As part of our CI process, I'd like to clear out all of the log files on our App Service before deploying new code to it. I believe the best way to do this is with a KUDU API call using a PowerShell script. I believe that I need to pass it a command. So this is what I've come up with so far based on some other similar questions asked. However, when I run this, I get a "Error 403 - This web app is stopped." response back. So I'm doing something wrong.
The important part is the "$kudoApiCommand".
param(
[string]$resourceGroupName,
[string]$webAppName,
[string]$slotName=""
)
function Get-AzureRmWebAppPublishingCredentials($resourceGroupName, $webAppName, $slotName = $null){
if ([string]::IsNullOrWhiteSpace($slotName)){
$resourceType = "Microsoft.Web/sites/config"
$resourceName = "$webAppName/publishingcredentials"
}
else{
$resourceType = "Microsoft.Web/sites/slots/config"
$resourceName = "$webAppName/$slotName/publishingcredentials"
}
$publishingCredentials = Invoke-AzureRmResourceAction -ResourceGroupName
$resourceGroupName -ResourceType $resourceType -ResourceName
$resourceName -Action list -ApiVersion 2015-08-01 -Force
Write-Host $publishingCredentials
return $publishingCredentials
}
function Get-KuduApiAuthorisationHeaderValue($resourceGroupName,$webAppName, $slotName = $null){
$publishingCredentials = Get-AzureRmWebAppPublishingCredentials $resourceGroupName $webAppName $slotName
Write-Host $publishingCredentials.Properties.PublishingUserName
Write-Host $publishingCredentials.Properties.PublishingPassword
return ("Basic {0}" -f [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $publishingCredentials.Properties.PublishingUserName, $publishingCredentials.Properties.PublishingPassword))))
}
function Delete-WebAppLogFiles($resourceGroupName, $webAppName, $slotName = ""){
$kuduApiAuthorisationToken = Get-KuduApiAuthorisationHeaderValue
$resourceGroupName $webAppName $slotName
if ($slotName -eq ""){
$kuduApiUrl = "https://$webAppName.scm.azurewebsites.net/api/command"
}
else{
$kuduApiUrl = "https://$webAppName`-$slotName.scm.azurewebsites.net/api/command"
}
$kudoApiCommand = #{
command='del * /S /Q'
dir='d:\\home\\LogFiles'
}
Write-Output $kuduApiUrl
Write-Output $kuduApiAuthorisationToken
Invoke-RestMethod -Uri $kuduApiUrl `
-Headers #{"Authorization"=$kuduApiAuthorisationToken;"If-Match"="*"} `
-Method POST
-Body $kudoApiCommand
}
Delete-WebAppLogFiles $resourceGroupName $webAppName $slotName
Got it. This is what worked for me:
param(
[string]$resourceGroupName,
[string]$webAppName,
[string]$slotName=""
)
function Get-AzureRmWebAppPublishingCredentials($resourceGroupName, $webAppName, $slotName = $null){
if ([string]::IsNullOrWhiteSpace($slotName)){
$resourceType = "Microsoft.Web/sites/config"
$resourceName = "$webAppName/publishingcredentials"
}
else{
$resourceType = "Microsoft.Web/sites/slots/config"
$resourceName = "$webAppName/$slotName/publishingcredentials"
}
$publishingCredentials = Invoke-AzureRmResourceAction -ResourceGroupName $resourceGroupName -ResourceType $resourceType -ResourceName $resourceName -Action list -ApiVersion 2015-08-01 -Force
Write-Host $publishingCredentials
return $publishingCredentials
}
function Get-KuduApiAuthorizationHeaderValue($resourceGroupName, $webAppName, $slotName = $null){
$publishingCredentials = Get-AzureRmWebAppPublishingCredentials $resourceGroupName $webAppName $slotName
Write-Host $publishingCredentials.Properties.PublishingUserName
Write-Host $publishingCredentials.Properties.PublishingPassword
return ("Basic {0}" -f [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $publishingCredentials.Properties.PublishingUserName, $publishingCredentials.Properties.PublishingPassword))))
}
function Delete-WebAppLogFiles($resourceGroupName, $webAppName, $slotName = ""){
$apiAuthorizationToken = Get-KuduApiAuthorizationHeaderValue $resourceGroupName $webAppName $slotName
if ($slotName -eq ""){
$apiUrl = "https://$webAppName.scm.azurewebsites.net/api/command"
}
else{
$apiUrl = "https://$webAppName`-$slotName.scm.azurewebsites.net/api/command"
}
$apiCommand = #{
#command='del *.* /S /Q /F'
command = 'powershell.exe -command "Remove-Item -path d:\\home\\LogFiles\\* -recurse"'
dir='d:\\home\\LogFiles'
}
Write-Output $apiUrl
Write-Output $apiAuthorizationToken
Write-Output $apiCommand
Invoke-RestMethod -Uri $apiUrl -Headers #{"Authorization"=$apiAuthorizationToken;"If-Match"="*"} -Method POST -ContentType "application/json" -Body (ConvertTo-Json $apiCommand)
}
Delete-WebAppLogFiles $resourceGroupName $webAppName $slotName