无法展开PSObject

时间:2017-02-12 21:17:38

标签: powershell

我使用系统的DNS名称从数据库中提取信息。获得信息后,我将其设置为替换我不再需要的计算机名称部分。

然而,问题在于,一旦执行了替换,我就再也无法选择对象了 - 如果选择DnsName它只返回空白,或者如果你看变量它只会显示{{1} }}。我尝试了@{Dnsname=computer123},但这也无效。

-ExpandProperty

当我使用下面的代码从SCCM中提取信息时,我也遇到了同样的问题,在function ExecuteSqlQuery($SQLQuery) { $Server = "localhost" $Database = "DB123" $Array = @() #Create and open a database connection $Connection = New-Object System.Data.SQLClient.SQLConnection $Connection.ConnectionString = "server='$Server';database='$Database';trusted_connection=true;" $Connection.Open() #Create a command object $Command = New-Object System.Data.SQLClient.SQLCommand $Command.Connection = $Connection $Command.CommandText = $SQLQuery #Execute the Command $Reader = $Command.ExecuteReader() $Counter = $Reader.FieldCount #Parse the records while ($Reader.Read()) { for ($i =0; $i -lt $Counter; $i++) { $Array += $Reader.GetValue($i) } } # Close the database connection $Connection.Close() return $Array } $DNSsys = "SELECT DnsName FROM Systems" $DNSName = ExecuteSqlQuery ($DNSsys) $DNSName = $DNSName -replace '.123.com', '' -replace '.abc.com', '' 运行之前一切正常:

-replace

1 个答案:

答案 0 :(得分:0)

这是接近它的方法,因此您不会运行字符串修饰符 - 替换整个对象,而只是该属性:

$Array += $Reader.GetValue($i) | Select-Object *,@{N="HostName";E={$_.DNSName -replace '.123.com', '' -replace '.abc.com', ''}}

相关问题