将System.Object []'转换为类型'System.String

问题描述

我正在尝试创建一个脚本,该脚本显示Azure sql服务器的防火墙中的IP设置,为此,我正在使用以下代码

$sqlservers = Get-AzsqlServer 

foreach ($server in $sqlservers)
{

 $sqlserver = $sqlservers.ServerName
 $sqlRg = $sqlservers.ResourceGroupName
 $firewallIP = Get-AzsqlServerFirewallRule -ServerName $sqlserver -ResourceGroupName $sqlRg
  
}

但是我收到此错误

Get-AzsqlServerFirewallRule : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'ServerName'. Specified method is not supported.
At line:11 char:56
+ ... rewallIP = Get-AzsqlServerFirewallRule -ServerName $sqlserver -Resour ...
+                                                        ~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Get-AzsqlServerFirewallRule],ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgument,Microsoft.Azure.Commands.sql.FirewallRule.Cmdlet.GetAzuresqlServerFirewallRule



Get-AzsqlServerFirewallRule : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'ServerName'. Specified method is not supported.
At line:11 char:56
+ ... rewallIP = Get-AzsqlServerFirewallRule -ServerName $sqlserver -Resour ...
+                                                        ~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Get-AzsqlServerFirewallRule],Microsoft.Azure.Commands.sql.FirewallRule.Cmdlet.GetAzuresqlServerFirewallRule

家里有人可以帮助我

预先感谢

解决方法

您使用服务器数组($ sqlservers)分配了$ sqlserver和$ sqlRg,需要将其更改为$ server

$sqlservers = Get-AzSqlServer 

foreach ($server in $sqlservers)
{

 $sqlserver = $server.ServerName
 $sqlRg = $server.ResourceGroupName
 $firewallIP = Get-AzSqlServerFirewallRule -ServerName $sqlserver -ResourceGroupName $sqlRg
  
}