0

我使用以下方法提取了 SQL 实例的一些详细信息:

$SQLInstance1 = Get-DbaInstanceProperty -SqlInstance SQL | Select-Object ComputerName, Name, Value  | Where-Object Name -In ("FullyQualifiedNetName", "Edition", "PhysicalMemory", "Processors", "VersionString", "Collation", "IsClustered", "ResourceLastUpdateDateTime")

将这些值分配给数组输出中的变量的最佳方法是什么,如下所示:

计算机名称 名称 值


SQL Edition Enterprise Edition(64 位) SQL PhysicalMemory 1571485
SQL Processors 40
SQL VersionString 11.0.7493.4
SQL Collat​​ion Latin1_General_CI_AS
SQL IsClustered True
SQL ResourceLastUpdateDateTime 24/12/2019 9:58:55 PM
SQL FullyQualifiedNetName PTHSQL13.FMG.local

所以我想从上面分配$ComputerName = ComputerName$Edition = Edition,然后我可以用它来更新 SQL 表$ComputerName

4

2 回答 2

1

好的。首先,您需要将结果转换为字典,然后使用该属性字典创建一个新对象。

例如:

$dict = @{ }

(Get-DbaInstanceProperty -SqlInstance localhost | Select-Object ComputerName, Name, Value | Where-Object Name -In ("FullyQualifiedNetName", "Edition", "PhysicalMemory", "Processors", "VersionString", "Collation", "IsClustered", "ResourceLastUpdateDateTime")).GetEnumerator() | % { $dict.Add($_.Name, $_.Value) }

New-Object -TypeName PSObject -Property $dict

$result = New-Object -TypeName PSObject -Property $dict

$result.FullyQualifiedNetName

$result.Processors

上面的代码应该打印出FullyQualifiedNetName值和Processors计数。

于 2021-02-20T03:51:59.720 回答
0

点符号是你的朋友。由于您存储的是一个序列化的实际对象,因此您可以使用.propertiesname.

[array]SQLInstance1 = Get-DbaInstanceProperty -SqlInstance SQL | Select-Object ComputerName, Name, Value  | Where-Object Name -In ("FullyQualifiedNetName", "Edition", "PhysicalMemory", "Processors", "VersionString", "Collation", "IsClustered", "ResourceLastUpdateDateTime")

($SQLInstance1.ComputerName.Count + $SQLInstance1.Name.Count + $SQLInstance1.Value.Count); $i++){
[pscustomobject]@{
            ComputerName = $($SQLInstance1.ComputerName[$i])
            Name = $($SQLInstance1.Name[$i])
            Value = $($SQLInstance1.Value[$i])

                }
        }
于 2021-02-20T04:32:28.710 回答