Select 个基于名称的数组值
Select values from Array based on name
我使用以下方法提取了 SQL 实例的一些详细信息:
$SQLInstance1 = Get-DbaInstanceProperty -SqlInstance SQL | Select-Object ComputerName, Name, Value | Where-Object Name -In ("FullyQualifiedNetName", "Edition", "PhysicalMemory", "Processors", "VersionString", "Collation", "IsClustered", "ResourceLastUpdateDateTime")
将这些值分配给数组输出中的变量的最佳方法是什么:
ComputerName 名称值
SQL版企业版(64位)
SQL 物理内存 1571485
SQL 处理器 40
SQL 版本字符串 11.0.7493.4
SQL 排序规则 Latin1_General_CI_AS
SQL IsClustered 为真
SQL ResourceLastUpdateDateTime 24/12/2019 9:58:55 下午
SQL FullyQualifiedNetName PTHSQL13.FMG.local
所以我想从上面分配 $ComputerName = ComputerName
、$Edition = Edition
等,然后我可以用它来更新 SQL table $ComputerName
好的。首先,您需要将结果转换为字典,然后使用该属性字典创建一个新对象。
例如:
$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
计数。
Dot Notation 是您的朋友。由于您存储的是序列化的实际对象,因此可以使用 .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])
}
}
我使用以下方法提取了 SQL 实例的一些详细信息:
$SQLInstance1 = Get-DbaInstanceProperty -SqlInstance SQL | Select-Object ComputerName, Name, Value | Where-Object Name -In ("FullyQualifiedNetName", "Edition", "PhysicalMemory", "Processors", "VersionString", "Collation", "IsClustered", "ResourceLastUpdateDateTime")
将这些值分配给数组输出中的变量的最佳方法是什么:
ComputerName 名称值
SQL版企业版(64位)
SQL 物理内存 1571485
SQL 处理器 40
SQL 版本字符串 11.0.7493.4
SQL 排序规则 Latin1_General_CI_AS
SQL IsClustered 为真
SQL ResourceLastUpdateDateTime 24/12/2019 9:58:55 下午
SQL FullyQualifiedNetName PTHSQL13.FMG.local
所以我想从上面分配 $ComputerName = ComputerName
、$Edition = Edition
等,然后我可以用它来更新 SQL table $ComputerName
好的。首先,您需要将结果转换为字典,然后使用该属性字典创建一个新对象。
例如:
$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
计数。
Dot Notation 是您的朋友。由于您存储的是序列化的实际对象,因此可以使用 .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])
}
}