The output for $rgResults returns a few rows of Resource Groups and their respective servers. I’d like to pass that information into the next ‘foreach’ loop, but I’m receiving an error.
Here’s the code:
$rgResults = foreach ($rg in $rgs) {
$servers = Get-AzSqlServer -ResourceGroupName $rg -ErrorAction SilentlyContinue | Select-Object -ExpandProperty ServerName
foreach ($server in $servers) {
[PSCustomObject]@{
ResourceGroup = $rg
Server = $server
} # End object creation
} # end server loop
} # end resource group loop
$dbs = $rgResults
$dbResults = @()
foreach($db in $dbs) {
$dbs = Get-AzSqlDatabase -ServerName $dbs.Server -ResourceGroupName $dbs.ResourceGroup
$obj = New-Object -TypeName psobject
Add-Member -InputObject $obj -MemberType NoteProperty -Name ResourceGroup -Value $db.ResourceGroupName
Add-Member -InputObject $obj -MemberType NoteProperty -Name ServerName -Value $db.ServerName
Add-Member -InputObject $obj -MemberType NoteProperty -Name Database -Value $db.DatabaseName
Add-Member -InputObject $obj -MemberType NoteProperty -Name CreationDate -Value $db.CreationDate
$dbResults += $obj
} # EndForEach
$dbResults | Format-Table -AutoSize
Error:
Get-AzSqlDatabase : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'ServerName'. Specified method is not supported. At line:3 char:50 + $dbs = Get-AzSqlDatabase -ServerName $dbs.Server -Resourc ... + ~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [Get-AzSqlDatabase], ParameterBindingException + FullyQualifiedErrorId : CannotConvertArgument,Microsoft.Azure.Commands.Sql.Database.Cmdlet.GetAzureSqlDatabase