question

suryavamsi-4743 avatar image
0 Votes"
suryavamsi-4743 asked Monalla-MSFT commented

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

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

windows-server-powershellazure-automation
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hello @suryavamsi-4743 - Did the below answer help resolve your issue,
If so, please feel free to "Accept as Answer" so it can be beneficial to the community.

0 Votes 0 ·
RichMatheisen-8856 avatar image
0 Votes"
RichMatheisen-8856 answered

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
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

LimitlessTechnology-2700 avatar image
0 Votes"
LimitlessTechnology-2700 answered

Hello @suryavamsi-4743

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://docs.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--

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.