Hello,
I'm a beginner at Powershell and am writing a script to gather the deployment details on all Azure Cloud Services attached to a Azure Subscription, including the info for any service that does not have a deployment.
I understand that the way I'm going about this may be wrong and I am NOT looking for any help creating a final script. Some of my script is written specifically so that I can better understand what it's doing (for learning purposes).
What I am looking for is help understand why my current script behaves the way it does.
Here's my current script:
### #Create a report that shows the created date and latest deployment date for all #hosted services (cloud services)deployments in MyAzureSubscription ### Select-AzureSubscription MyAzureSubscription $ServiceNames = Get-azureservice foreach ($service in $ServiceNames){ $ServiceName = $service | select -ExpandProperty ServiceName $ServiceDateModified = $service | select -ExpandProperty DateModified $service | get-azuredeployment -ErrorAction SilentlyContinue -ErrorVariable NoDeployment | Select @{Name="ServiceName";Expression={$ServiceName}}, DeploymentName, Status, CreatedTime, LastModifiedTime, @{Name="ServiceDateModified";Expression={$ServiecDateModified}}, @{Name="NoDeployment";Expression={$NoDeployment}} | Sort-Object -Propert DeploymentName | export-csv "outputfile.csv" -notypeinformation -append #Test statements below #Write-Host "NoDeployment Below:" #$Service | select ServiceName | Write-Host #Write-Error $NoDeployment Write-Host $ServiceName }
The output file only contains a list of servicenames and deployment details for services that do have a deployment. None of the services that have no deployments are added/exported to the csv file. However, the "write-host $ServiceName" writes ALL the service names in the powershell window, regardless of whether there's a deployment or not.
What I don't understand is why the Select @{Name="ServiceName";Expression={$ServiceName}} only outputs the service names of services that have deployments instead of ALL service names regardless of whether they have a deployment or not.
I've already proven that the variable $ServiceName does, at some point, save the ServiceName into the $ServiceName variable by writing it to host on each iteration of the foreach loop. So why is it ignoring some ServiceNames when outputting/exporting to the csv file?
Thanks!