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

surya vamsi 1 Reputation point
2021-10-04T10:25:01.94+00:00

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 = @()

$results=

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,114 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,363 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Rich Matheisen 44,776 Reputation points
    2021-10-04T15:01:54.463+00:00

    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 | 
        ForEach-Object{
            Write-Host $_
            $azvm = Get-AzVM -Name $_
            $subid = ($azvm.Id -split '/')[2]
            $subname = (Get-AzSubscription -SubscriptionId $subid).Name
    
            # Should $results be updated here as well ??????
    
            [PSCustomObject]@{
                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,351 Reputation points
    2021-10-06T19:37:54.38+00:00

    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: https://learn.microsoft.com/en-us/azure/cloud-shell/overview

    Hope this helps with your query,

    ----------

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

    0 comments No comments