开始使用 PowerShell 生成商店寄存器组的格式化报告时遇到问题
Trouble getting started using PowerShell to generate formatted report of store registers group by store then lane
尽可能使用 PowerShell。
希望生成一份报告,我可以将其保存到 CSV
,显示 POS
按商店和车道登记 CPUs。
后端是一个 SQL
我正在查询的数据库
select STORE,REG,CPU from MY_REGISTERS;
我可以根据需要随意更改查询。
- STORE 是 Int32 4 位零前缀值; 0007、0010、0234、1234。\
- REG为Int32个位数; 1 - 10 是寄存器所在的“通道”。
- CPU 是可变长度的字符串 10-24 个字符。
由于这里的限制 space,我正在截断格式,但这是通用格式。
一个寄存器通道可能是空的,我想要一个空白字符串来保留这个位置。
STORE REG001 REG002 REG003 REG004 REG005 REG006 REG007 REG008 REG009 REG010
0007 i5 i7 i7 i3 <blnk> <blnk> i6 <blnk> <blnk> <blnk>
0018 i6 <blnk> i3 i6 i5 i6 <blnk> <blnk> <blnk> <blnk>
0256 i7 i7 i5 i7 i4 <blnk> <blnk> <blnk> <blnk> <blnk>
1234 i5 i7 i7 i3 i7 i7 i5 i7 i4
我已经开始尝试多种方法,但似乎没有任何效果。我无法想象通往有用解决方案的路径。
已经尝试了多个嵌套的 for 循环,寻找变化的值并将它们拉出。
我试过数组和散列操作。
我试过分组功能。
我的一般方法都是围绕下面这个基本想法,但我一直无法实现它。
- 在数据库中查询数据并存储在 $Return
- 获取唯一的商店列表($UniqueListOfStores = $Returned.store | Get-Unique)
- Foreach $store in $UniqueStoreList 获取 $registerLaneValues
- Foreach $reg in $registerLaneValues 得到 CPU
- 构建存储、注册和 CPU 值的对象
- 将该对象添加到更高级别的容器(另一个数组?)
- 显示整个报告并能够导出 CSV
从数据库查询中获取有关我的 $Returned 对象的成员信息。
TypeName: System.Data.DataRow
Name MemberType Definition
---- ---------- ----------
AcceptChanges Method void AcceptChanges()
BeginEdit Method void BeginEdit()
.
.
.
ParameterizedProperty System.Object Item(int columnIndex) {get;set;}, System.Object Item(string columnName) {get;set;}, System.Object Item(System.Data.D...
CPU Property string CPU {get;set;}
REG Property int REG {get;set;}
STORE Property int STORE {get;set;}
一些示例数据:
PS E:\Repos\process-cmdb> $Returned
STORE REG CPU
----- --- ---
7 1 3500 MHz Core i5-7600
7 2 3500 MHz Core i5-7600
7 3 3500 MHz Core i5-7600
7 4 3500 MHz Core i5-7600
7 5 3500 MHz Core i5-7600
7 6 3500 MHz Core i5-7600
7 7 3500 MHz Core i5-7600
8 1 3500 MHz Core i5-7600
8 2 3500 MHz Core i5-7600
8 3 2500 MHz Celeron
8 4 3500 MHz Core i5-7600
8 5 3500 MHz Core i5-7600
8 6 2500 MHz Celeron
12 1 2500 MHz Celeron
12 2 2900 MHz Pentium
12 3 2500 MHz Celeron
12 4 2500 MHz Celeron
12 5 2500 MHz Celeron
12 6 2500 MHz Celeron
23 1 3500 MHz Core i5-7600
23 2 3500 MHz Core i5-7600
23 3 3500 MHz Core i5-7600
23 4 3500 MHz Core i5-7600
23 5 3500 MHz Core i5-7600
23 6 3500 MHz Core i5-7600
25 1 3500 MHz Core i5-7600
25 2 3500 MHz Core i5-7600
25 3 2500 MHz Celeron
25 4 3500 MHz Core i5-7600
25 5 3500 MHz Core i5-7600
25 6 2500 MHz Celeron
33 1 3500 MHz Core i5-7600
33 2 3500 MHz Core i5-7600
33 3 2500 MHz Celeron
33 4 3500 MHz Core i5-7600
33 5 3500 MHz Core i5-7600
33 6 2500 MHz Celeron
我希望更好地理解如何分解这个问题以及如何将其转化为 PowerShell
脚本。
提前致谢!
也许这就是您的想法。首先,我按 'Store' 对数据进行分组,然后遍历每个组并构建一个哈希表,其中 reg 值作为键,CPU 值作为值。最后,我将这些哈希表输出为 pscustomobjects,当导出到 csv 时,它们应该是您想要的格式。
$ConvertedData = $Return |
Group-Object -Property Store |
Sort-Object {[int]$_.Name} |
ForEach-Object {
# Create hashtable to build object
$out = [ordered]@{}
# Create STORE property
$out.add('STORE', $_.Name.PadLeft(4, '0'))
# Build each of the REG properties from 1 to 10
foreach ($reg in 1..10) {
# Look for a matching reg entry and set it to CPU value
if ($matchReg = $_.group | Where-Object {$_.Reg -eq $reg}) {
$out.add("REG$($reg.ToString().PadLeft(3,'0') )", $matchReg.Cpu)
}
else {
$out.add("REG$($reg.ToString().PadLeft(3,'0'))", '<blank>')
}
}
[pscustomobject]$out
}
# Output to screen
$ConvertedData | Format-Table
# Export to csv
$ConvertedData | Export-Csv -Path .\output.csv # Export to csv
输出
STORE REG001 REG002 REG003 REG004 REG005 REG006 REG007 REG008 REG009 REG010
----- ------ ------ ------ ------ ------ ------ ------ ------ ------ ------
0007 3500 MHz Core i5-7600 3500 MHz Core i5-7600 3500 MHz Core i5-7600 3500 MHz Core i5-7600 3500 MHz Core i5-7600 3500 MHz Core i5-7600 3500 MHz Core i5-7600 <blank> <blank> <blank>
0008 3500 MHz Core i5-7600 3500 MHz Core i5-7600 2500 MHz Celeron 3500 MHz Core i5-7600 3500 MHz Core i5-7600 2500 MHz Celeron <blank> <blank> <blank> <blank>
0012 2500 MHz Celeron 2900 MHz Pentium 2500 MHz Celeron 2500 MHz Celeron 2500 MHz Celeron 2500 MHz Celeron <blank> <blank> <blank> <blank>
0023 3500 MHz Core i5-7600 3500 MHz Core i5-7600 3500 MHz Core i5-7600 3500 MHz Core i5-7600 3500 MHz Core i5-7600 3500 MHz Core i5-7600 <blank> <blank> <blank> <blank>
0025 3500 MHz Core i5-7600 3500 MHz Core i5-7600 2500 MHz Celeron 3500 MHz Core i5-7600 3500 MHz Core i5-7600 2500 MHz Celeron <blank> <blank> <blank> <blank>
0033 3500 MHz Core i5-7600 3500 MHz Core i5-7600 2500 MHz Celeron 3500 MHz Core i5-7600 3500 MHz Core i5-7600 2500 MHz Celeron <blank> <blank> <blank> <blank>
尽可能使用 PowerShell。
希望生成一份报告,我可以将其保存到 CSV
,显示 POS
按商店和车道登记 CPUs。
后端是一个 SQL
我正在查询的数据库
select STORE,REG,CPU from MY_REGISTERS;
我可以根据需要随意更改查询。
- STORE 是 Int32 4 位零前缀值; 0007、0010、0234、1234。\
- REG为Int32个位数; 1 - 10 是寄存器所在的“通道”。
- CPU 是可变长度的字符串 10-24 个字符。
由于这里的限制 space,我正在截断格式,但这是通用格式。
一个寄存器通道可能是空的,我想要一个空白字符串来保留这个位置。
STORE REG001 REG002 REG003 REG004 REG005 REG006 REG007 REG008 REG009 REG010
0007 i5 i7 i7 i3 <blnk> <blnk> i6 <blnk> <blnk> <blnk>
0018 i6 <blnk> i3 i6 i5 i6 <blnk> <blnk> <blnk> <blnk>
0256 i7 i7 i5 i7 i4 <blnk> <blnk> <blnk> <blnk> <blnk>
1234 i5 i7 i7 i3 i7 i7 i5 i7 i4
我已经开始尝试多种方法,但似乎没有任何效果。我无法想象通往有用解决方案的路径。
已经尝试了多个嵌套的 for 循环,寻找变化的值并将它们拉出。
我试过数组和散列操作。
我试过分组功能。
我的一般方法都是围绕下面这个基本想法,但我一直无法实现它。
- 在数据库中查询数据并存储在 $Return
- 获取唯一的商店列表($UniqueListOfStores = $Returned.store | Get-Unique)
- Foreach $store in $UniqueStoreList 获取 $registerLaneValues
- Foreach $reg in $registerLaneValues 得到 CPU
- 构建存储、注册和 CPU 值的对象
- 将该对象添加到更高级别的容器(另一个数组?)
- 显示整个报告并能够导出 CSV
从数据库查询中获取有关我的 $Returned 对象的成员信息。
TypeName: System.Data.DataRow
Name MemberType Definition
---- ---------- ----------
AcceptChanges Method void AcceptChanges()
BeginEdit Method void BeginEdit()
.
.
.
ParameterizedProperty System.Object Item(int columnIndex) {get;set;}, System.Object Item(string columnName) {get;set;}, System.Object Item(System.Data.D...
CPU Property string CPU {get;set;}
REG Property int REG {get;set;}
STORE Property int STORE {get;set;}
一些示例数据:
PS E:\Repos\process-cmdb> $Returned
STORE REG CPU
----- --- ---
7 1 3500 MHz Core i5-7600
7 2 3500 MHz Core i5-7600
7 3 3500 MHz Core i5-7600
7 4 3500 MHz Core i5-7600
7 5 3500 MHz Core i5-7600
7 6 3500 MHz Core i5-7600
7 7 3500 MHz Core i5-7600
8 1 3500 MHz Core i5-7600
8 2 3500 MHz Core i5-7600
8 3 2500 MHz Celeron
8 4 3500 MHz Core i5-7600
8 5 3500 MHz Core i5-7600
8 6 2500 MHz Celeron
12 1 2500 MHz Celeron
12 2 2900 MHz Pentium
12 3 2500 MHz Celeron
12 4 2500 MHz Celeron
12 5 2500 MHz Celeron
12 6 2500 MHz Celeron
23 1 3500 MHz Core i5-7600
23 2 3500 MHz Core i5-7600
23 3 3500 MHz Core i5-7600
23 4 3500 MHz Core i5-7600
23 5 3500 MHz Core i5-7600
23 6 3500 MHz Core i5-7600
25 1 3500 MHz Core i5-7600
25 2 3500 MHz Core i5-7600
25 3 2500 MHz Celeron
25 4 3500 MHz Core i5-7600
25 5 3500 MHz Core i5-7600
25 6 2500 MHz Celeron
33 1 3500 MHz Core i5-7600
33 2 3500 MHz Core i5-7600
33 3 2500 MHz Celeron
33 4 3500 MHz Core i5-7600
33 5 3500 MHz Core i5-7600
33 6 2500 MHz Celeron
我希望更好地理解如何分解这个问题以及如何将其转化为 PowerShell
脚本。
提前致谢!
也许这就是您的想法。首先,我按 'Store' 对数据进行分组,然后遍历每个组并构建一个哈希表,其中 reg 值作为键,CPU 值作为值。最后,我将这些哈希表输出为 pscustomobjects,当导出到 csv 时,它们应该是您想要的格式。
$ConvertedData = $Return |
Group-Object -Property Store |
Sort-Object {[int]$_.Name} |
ForEach-Object {
# Create hashtable to build object
$out = [ordered]@{}
# Create STORE property
$out.add('STORE', $_.Name.PadLeft(4, '0'))
# Build each of the REG properties from 1 to 10
foreach ($reg in 1..10) {
# Look for a matching reg entry and set it to CPU value
if ($matchReg = $_.group | Where-Object {$_.Reg -eq $reg}) {
$out.add("REG$($reg.ToString().PadLeft(3,'0') )", $matchReg.Cpu)
}
else {
$out.add("REG$($reg.ToString().PadLeft(3,'0'))", '<blank>')
}
}
[pscustomobject]$out
}
# Output to screen
$ConvertedData | Format-Table
# Export to csv
$ConvertedData | Export-Csv -Path .\output.csv # Export to csv
输出
STORE REG001 REG002 REG003 REG004 REG005 REG006 REG007 REG008 REG009 REG010
----- ------ ------ ------ ------ ------ ------ ------ ------ ------ ------
0007 3500 MHz Core i5-7600 3500 MHz Core i5-7600 3500 MHz Core i5-7600 3500 MHz Core i5-7600 3500 MHz Core i5-7600 3500 MHz Core i5-7600 3500 MHz Core i5-7600 <blank> <blank> <blank>
0008 3500 MHz Core i5-7600 3500 MHz Core i5-7600 2500 MHz Celeron 3500 MHz Core i5-7600 3500 MHz Core i5-7600 2500 MHz Celeron <blank> <blank> <blank> <blank>
0012 2500 MHz Celeron 2900 MHz Pentium 2500 MHz Celeron 2500 MHz Celeron 2500 MHz Celeron 2500 MHz Celeron <blank> <blank> <blank> <blank>
0023 3500 MHz Core i5-7600 3500 MHz Core i5-7600 3500 MHz Core i5-7600 3500 MHz Core i5-7600 3500 MHz Core i5-7600 3500 MHz Core i5-7600 <blank> <blank> <blank> <blank>
0025 3500 MHz Core i5-7600 3500 MHz Core i5-7600 2500 MHz Celeron 3500 MHz Core i5-7600 3500 MHz Core i5-7600 2500 MHz Celeron <blank> <blank> <blank> <blank>
0033 3500 MHz Core i5-7600 3500 MHz Core i5-7600 2500 MHz Celeron 3500 MHz Core i5-7600 3500 MHz Core i5-7600 2500 MHz Celeron <blank> <blank> <blank> <blank>