理论上,现在“2023”文件夹中的所有文件在文件名中具有相同数量的元素,我可以获取清单。 请参阅我之前的帖子以供参考顺便说一句,谢谢@mklement0,它说不要发表评论,谢谢,所以这是我的解决方法。
对于每个计算机ID/进程ID,应该有4个不同的国家文件(加拿大、意大利、墨西哥、美国)。除了输出“2023”文件夹中的文件名之外,我还需要根据 4 个国家/地区输出丢失的文件,因为这些文件是不变的。我根据“.”分割文件名并为每个创建一列。
#splitFile_v2
$numberedFolder = 'C:\Users\cthor\OneDrive\Documents\PowerShell\Test'
$outputFile = "C:\Users\cthor\OneDrive\Documents\PowerShell\Results\results_$(Get-Date -f yyyy-MM-dd-hh-mm-ss).csv"
Get-ChildItem -Directory -Recurse -LiteralPath $numberedFolder -Filter '2023' |
Get-ChildItem -File |
Select-Object @{name='computer id';Expression={$_.Name.Split('.')[0]}},
@{name='process id';Expression={"`t{0:d3}" -f [int] $_.Name.Split('.')[1]}}, #preverse leading 0's.
@{name='computer name';Expression={$_.Name.Split('.')[2]}},
@{name='city';Expression={$_.Name.Split('.')[3]}},
@{name='country';Expression={$_.Name.Split('.')[4]}},
@{name='date';Expression={$_.Name.Split('.')[5]}},
@{name='file extension';Expression={$_.Name.Split('.')[6]}} |
Group 'computer name','process id' -PipelineVariable 'group'|
ForEach-Object {
# Pass all objects that exist
$_.group
# Check for missing objects, and create a 'Missing item' object for any missing ones
If($_.Count -ne 4){
switch($_.group){
{'Canada' -notin $_.country} {[PSCustomObject]@{'computer id' = $_.'computer id'; 'process id' = $_.'process id'; 'computer name' = $_.'computer name'; 'country' = 'Missing Canada'};break}
{'Italy' -notin $_.country} {[PSCustomObject]@{'computer id' = $_.'computer id'; 'process id' = $_.'process id'; 'computer name' = $_.'computer name'; 'country' = 'Missing Italy'};break}
{'Mexico' -notin $_.country} {[PSCustomObject]@{'computer id' = $_.'computer id'; 'process id' = $_.'process id'; 'computer name' = $_.'computer name'; 'country' = 'Missing Mexico'};break}
{'USA' -notin $_.country} {[PSCustomObject]@{'computer id' = $_.'computer id'; 'process id' = $_.'process id'; 'computer name' = $_.'computer name'; 'country' = 'Missing USA'};break}
} # End Switch
} # End If
} |
Export-Csv -Path $outputFile -Append -NoTypeInformation
csv 文件中的当前输出
计算机 ID | 进程ID | 计算机名称 | 城市 | 国家 | 日期 | 文件 |
---|---|---|---|---|---|---|
12345 | 001 | 克里斯_comp | 墨西哥城 | 墨西哥 | 2023_08_20 | rtf |
12345 | 001 | 克里斯_comp | 米兰 | 意大利 | 2023_08_22 | bmp |
12345 | 001 | 克里斯_comp | 纽约 | 美国 | 2023_08_22 | xlsx |
12345 | 001 | 克里斯_comp | 多伦多 | 加拿大 | 2023_08_22 | txt |
12345 | 002 | 克里斯_comp | 墨西哥城 | 墨西哥 | 2023_05_05 | rtf |
12345 | 002 | 克里斯_comp | 米兰 | 意大利 | 2023_05_05 | bmp |
12345 | 002 | 克里斯_comp | 纽约 | 美国 | 2023_05_05 | xlsx |
12345 | 002 | 克里斯_comp | 多伦多 | 加拿大 | 2023_05_05 | txt |
15555 | 001 | andrew_comp | 艾伯塔省 | 加拿大 | 2023_02_22 | txt |
15555 | 001 | andrew_comp | 华雷斯 | 墨西哥 | 2023_02_22 | rtf |
15555 | 001 | andrew_comp | 纽约 | 美国 | 2023_02_22 | rtf |
15555 | 001 | andrew_comp | 缺少“意大利” | |||
42567 | 001 | 迈克_comp | 坎昆 | 墨西哥 | 2023_03_12 | rtf |
42567 | 001 | 迈克_comp | 蒙特利尔 | 加拿大 | 2023_03_11 | txt |
42567 | 001 | 迈克_comp | 缺少“加拿大” |
12345 001 组 - 所有文件都在那里并输出。
12345 002 组 - 所有文件都在那里并输出。
15555 001 组 - 3/4 个文件存在并与丢失的文件一起输出。
42567 001 组 - 2/4 个文件存在并被输出,但是 2 个丢失的文件意大利和美国不会被输出。雪上加霜的是,它输出了“失踪加拿大”,而这并不是真正失踪。
您可以按 ComputerId 和 ProcessID 进行分组,然后在
ForEach-Object
循环中您可以检查丢失的国家/地区,如下所示:
#splitFile
$numberedFolder = 'C:\Users\Documents\My Documents\PowerShell\Test'
$outputFile = "C:\Users\Documents\My Documents\PowerShell\Results_$(Get-Date -f yyyy-MM-dd-hh-mm-ss).csv"
Get-ChildItem -Directory -Recurse -LiteralPath $numberedFolder -Filter '2023' |
Get-ChildItem -File |
Select-Object @{name='computer id';Expression={$_.Name.Split('.')[0]}},
@{name='process id';Expression={ '{0:d3}' -f [int]$_.Name.Split('.')[1]}}, #trying to preverse leading 0's. will always be 3 digits long.
@{name='computer name';Expression={$_.Name.Split('.')[2]}},
@{name='city';Expression={$_.Name.Split('.')[3]}},
@{name='country';Expression={$_.Name.Split('.')[4]}},
@{name='date';Expression={$_.Name.Split('.')[5]}},
@{name='file extension';Expression={$_.Name.Split('.')[6]}} |
Group 'computer name','process id' -PipelineVariable 'Group'|
ForEach-Object {
# Pass all objects that exist
$_.group
# Check for missing objects, and create a 'Missing item' object for any missing ones
If($_.Count -ne 4){
switch($_.group){
{'Mexico' -notin $_.Country}{[PSCustomObject]@{'Computer id'="Missing 'Mexico'"}}
{'Canada' -notin $_.country}{[PSCustomObject]@{'computer id'="Missing 'Canada'"}}
{'Italy' -notin $_.country}{[PSCustomObject]@{'computer id'="Missing 'Italy'"}}
{'USA' -notin $_.country}{[PSCustomObject]@{'computer id'="Missing 'USA'"}}
} # End Switch
} # End If
} |
Export-Csv -Path $outputFIle -Append -NoTypeInformation