使用PowerShell Azurerm命令时Azure Profile.Model混入SQL服务器数组问题
Let me break down why you're seeing an Azure Profile.Model object mixed in with your SQL Server results, and how to fix it:
The Root Cause
This almost always happens because unintended output from another cmdlet is being appended to your results array. In PowerShell, functions return all uncaught output by default—not just the value you explicitly return. So if you have a cmdlet like Select-AzureRmProfile, Get-AzureRmContext, or even a stray variable reference that outputs a profile object before/after your Get-AzureRmSqlServer call, that object gets added to the final array alongside your SQL servers.
For example, if your function looks something like this:
function Get-SqlServersInRG { param([string]$ResourceGroupName) # This cmdlet outputs an Azure Profile object by default Select-AzureRmProfile -Name "MyProfile" $sqlServers = Get-AzureRmSqlServer -ResourceGroupName $ResourceGroupName return $sqlServers }
The Select-AzureRmProfile output gets tacked onto your $sqlServers array, resulting in that extra Profile.Model entry.
Fixes to Try
Suppress Unwanted Output
Add| Out-Nullto any cmdlets that produce output you don't need. For the example above, modify it to:Select-AzureRmProfile -Name "MyProfile" | Out-NullThis stops the profile object from being added to your function's return output.
Filter Results by Object Type
If you want to safeguard against accidental mixed outputs, explicitly filter your results to only include SQL Server objects:$sqlServers = Get-AzureRmSqlServer -ResourceGroupName $ResourceGroupName | Where-Object { $_ -is [Microsoft.Azure.Commands.Sql.Server.Model.AzureSqlServerModel] }This ensures only valid SQL Server objects stay in your array, regardless of stray outputs.
Audit Your Function for Stray Outputs
Double-check your function code for any lines that might output variables or cmdlet results without assigning them to a variable. Even a line like$currentContext(where$currentContextholds your Azure profile) will output the object if it's not assigned or suppressed.
Verify the Fix
After making these changes, re-run your function and check the array contents again. The extra Azure Profile.Model entry should be gone, leaving only your 4 SQL Server objects as expected.
内容的提问来源于stack exchange,提问作者itzJustKranker

