script is working in azure free account but it's not working in pay as you go subscription

surya vamsi 1 Reputation point

when I am trying to use the below script in azure free account it's working where as when i tried in pay as you go subscription,, which has a lot of subscriptions and many VM's there is no output and no error as well.
executing the script remotely

Connect-AzAccount -UseDeviceAuthentication

$vms = Get-Content D:\azure.txt
$outputCollection = @()


foreach($vmname in $vms){

Write-Host $vmname

$azvm =Get-AzVM -Name $vmname

$subid = ($azvm.Id -split '/')[2]
$subname=(Get-AzSubscription -SubscriptionId $subid).Name

$outputObject = "" | Select Name, ResourceGroupName, Subscription

$outputObject.Name = $vmname
$outputObject.ResourceGroupName = $azvm.ResourceGroupName
$outputObject.Subscription = $subname

$outputCollection += $outputObject
$outputCollection | Export-csv -Path D:\output.csv

$results | Export-csv -Path D:\output.csv

Azure Automation
Azure Automation
An Azure service that is used to automate, configure, and install updates across hybrid environments.
1,184 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,448 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Rich Matheisen 45,671 Reputation points

    Your example code is confusing! You never place anything in the variable $results and, at the end of the script you export it to the same file you exported $output to!

    The code below doesn't change what your example does. It's simply trying to understand what you're trying to do. Is $results supposed to have any contents? You're using "ForEach (<expression>)" to manage the loop, but that statement doesn't allow for any pipelining, and because you never write anything (you're always assigning the results to a variable), $results won't have any content!

    Connect-AzAccount -UseDeviceAuthentication
    $result = @()           # I'm guessing here becasue there's no reference
                            # to this variable except when it's exported to a CSV!
    Get-Content D:\azure.txt | 
            Write-Host $_
            $azvm = Get-AzVM -Name $_
            $subid = ($azvm.Id -split '/')[2]
            $subname = (Get-AzSubscription -SubscriptionId $subid).Name
            # Should $results be updated here as well ??????
                Name = $vmname
                ResourceGroupName = $azvm.ResourceGroupName
                Subscription = $subname
    } | Export-Csv -Path D:\output.csv -NoTypeInformation
    # Why is $results written to the same file???
    $results | Export-Csv -Path D:\output.csv -NoTypeInformation
    0 comments No comments

  2. Limitless Technology 39,506 Reputation points

    Hello @surya vamsi

    I suspect some authentication issue. Since Powershell is not an interactive prompt there may be some authentication error that just stops the run or data output.

    My recommendation would be to use directly the Azure Cloud Shell for this:

    Hope this helps with your query,


    --If the reply is helpful, please Upvote and Accept as answer--

    0 comments No comments