Search code examples
azurepowershellazure-web-app-serviceazure-automation

Azure Powershell Script Force FTPS Set-AzWebApp : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter


I am currently trying to run a script in Azure that would go through all of our Web Apps and turn on FTPS.

This is what I currently have

$Subscriptions = Get-AzSubscription
   foreach ($sub in $Subscriptions) {
       Get-AzSubscription -SubscriptionName $sub.Name | Set-AzContext
       $GetName = (Get-AzWebApp).Name
       $GetRG = (Get-AzWebApp).ResourceGroup
     Set-AzWebapp -Name $GetName -ResourceGroupName $GetRG -FtpsState FtpsOnly
       }

Set-AzWebApp : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'Name'. Specified method is not supported.

I currently am getting this error, which I dont understand as .Name and .ResourceGroup, from my understanding are already strings. I am very new to powershell so any help would be greatly appreciated. Thanks everyone!


Solution

  • Your example calls Az-WebApp with no parameters, which gets all apps in the subscription - this is a collection - and then tries to get the Name of that result, which is what causes your error.

    You need to loop through each app in the subscription as well as looping through each subscription, as in:

       # Get all subscriptions and iterate them
       Get-AzSubscription | ForEach-Object {
           Set-AzContext -SubscriptionName $_.Name
           
           # Get all web apps in the subscription and iterate them
           Get-AzWebApp | ForEach-Object {
               Set-AzWebApp -Name $_.Name -ResourceGroupName $_.ResourceGroup -FtpsState FtpsOnly
           }
    
       }