PowerShell Foreach Loop iteration issue

Tagged: 

This topic contains 1 reply, has 2 voices, and was last updated by Profile photo of Peter Jurgens Peter Jurgens 8 months, 3 weeks ago.

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #33665
    Profile photo of Abdul Abdul
    Abdul Abdul
    Participant

    I have put together a routing that does the following
    1. foreach ($AzureAccount in (Get-AzureAccount).ID) {Remove-AzureAccount $AzureAccount -Force} – This removes any Subscription session to my machine it works

    2.$subscription = Get-Content "C:\subscriptionsList.txt" – Reads a txt file for the name of the subscription to connect to. The file has a list of 4 Azure subscription and the read routine works.

    3.foreach ($Line In $Subscription)

    {

    $Fields = $Line -Split " "
    $subName = $Fields[3]

    write-host "Subscription Name is: "$subName
    Write-Host ""
    }
    The routine above simply splits the line and defines the separator which is a tab as each line has other data and it declares the $subscription variable. Finally, displays the list of subscriptions the read routine pulls from the text file

    4. Add-AzureAccount – This line pops up a screen which requires the login ID and password – It also does come up and works.

    5. Get-AzureService | Select-Object @{"Label"="ServiceName";Expression={$_.Label}} | ForEach-Object {Remove-AzureDeployment -ServiceName $_.ServiceName -Slot Production -DeleteVHD -Force -ErrorVariable a -ErrorAction silentlycontinue
    Remove-AzureService $_.ServiceName -Force
    } – This cmdlet basically gets a list of services then pipes to the next routine which deletes them and it works

    6. Remove-AzureVNetconfig – Finally this cmdlet removed all Vitual Networs in the subscription it also works

    THE QUESTION:
    My code does not loop through and does the whole script for the next subscription as read from the text file so that bit is not working and is what I need help with. Kindly assist

    ________________THE Full SCRIPT ___________________
    foreach ($AzureAccount in (Get-AzureAccount).ID) {Remove-AzureAccount $AzureAccount -Force}

    $subscription = Get-Content "C:\subscriptionsList.txt"

    foreach ($Line In $Subscription)

    {

    $Fields = $Line -Split " "
    $subName = $Fields[3]

    write-host "Subscription Name is: "$subName
    Write-Host ""
    }

    Add-AzureAccount

    Get-AzureService | Select-Object @{"Label"="ServiceName";Expression={$_.Label}} | ForEach-Object {Remove-AzureDeployment -ServiceName $_.ServiceName -Slot Production -DeleteVHD -Force -ErrorVariable a -ErrorAction silentlycontinue
    Remove-AzureService $_.ServiceName -Force
    }
    Remove-AzureVNetconfig

    #33667
    Profile photo of Peter Jurgens
    Peter Jurgens
    Participant

    Read over your code again logically. Inside your ForEach loop the only action you're doing is writing each '$subName' to the host. If you want to do things with each item beyond writing it to the host, you need to add the code inside of your ForEach loop. eg:

    ForEach($itm in $itms){
      Do-Something $itm
    }
    
Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.