为什么这个计数不正确 |电源外壳
Why Isn't This Counting Correctly | PowerShell
现在,我有一个包含 3,800 多条记录的 CSV 文件。此文件包含服务器名称列表,后跟一个缩写,说明服务器是 Windows 服务器、Linux 服务器等。该文件还包含注释或文档,其中每一行以“#”开头",说明这是一条评论。目前我的情况如下。
$file = Get-Content .\allsystems.csv
$arraysplit = @()
$arrayfinal = @()
[int]$windows = 0
foreach ($thing in $file){
if ($thing.StartsWith("#")) {
continue
}
else {
$arraysplit = $thing.Split(":")
$arrayfinal = @($arraysplit[0], $arraysplit[1])
}
}
foreach ($item in $arrayfinal){
if ($item[1] -contains 'NT'){
$windows++
}
else {
continue
}
}
$windows
此脚本的目标是计算 Windows 服务器的总数。我的问题是第一个 "foreach" 块工作正常,但第二个导致“$Windows”为 0。老实说,我不确定为什么这不起作用。两行示例数据如下:
example:LNX
example2:NT
$arrayfinal = @($arraysplit[0], $arraysplit[1])
这将替换每个 运行 的数组。
将其更改为 +=
又出现了另一个问题。它只是附加了每个单独的元素。我用这个 post 的信息来修复它,有点强制二维数组:How to create array of arrays in powershell?.
$file = Get-Content .\allsystems.csv
$arraysplit = @()
$arrayfinal = @()
[int]$windows = 0
foreach ($thing in $file){
if ($thing.StartsWith("#")) {
continue
}
else {
$arraysplit = $thing.Split(":")
$arrayfinal += ,$arraysplit
}
}
foreach ($item in $arrayfinal){
if ($item[1] -contains 'NT'){
$windows++
}
else {
continue
}
}
$windows
1
我还更改了文件并添加了更多 NT 和其他随机垃圾的实例。似乎它工作正常。
我会避免创建另一个 ForEach 循环来增加出现次数。你的 $arrayfinal 每次都会重写,所以我使用了 ArrayList。
$file = Get-Content "E:\Code\PS\myPS18\Jun\allSystems.csv"
$arrayFinal = New-Object System.Collections.ArrayList($null)
foreach ($thing in $file){
if ($thing.StartsWith("#")) {
continue
}
else {
$arraysplit = $thing -split ":"
if($arraysplit[1] -match "NT" -or $arraysplit[1] -match "Windows")
{
$arrayfinal.Add($arraysplit[1]) | Out-Null
}
}
}
Write-Host "Entries with 'NT' or 'Windows' $($arrayFinal.Count)"
我不确定你是否想保留 'Example'、'example2'... 所以我跳过了将它们添加到 arrayfinal,假设目标是计算 "NT" 或"Windows" 次出现
如果目标是计算 windows 个服务器,为什么需要数组?
你就不能像这样说吗
foreach ($thing in $file)
{
if ($thing -notmatch "^#" -and $thing -match "NT") { $windows++ }
}
The goal of this script is to count the total number of Windows servers.
我建议使用简单的方法:使用为此构建的 cmdlet。
$csv = Get-Content -Path .\file.csv |
Where-Object { -not $_.StartsWith('#') } |
ConvertFrom-Csv
@($csv.servertype).Where({ $_.Equals('NT') }).Count
# Compatibility mode:
# ($csv.servertype | Where-Object { $_.Equals('NT') }).Count
将 servertype
和 'NT'
替换为任何 header/value 的名称。
现在,我有一个包含 3,800 多条记录的 CSV 文件。此文件包含服务器名称列表,后跟一个缩写,说明服务器是 Windows 服务器、Linux 服务器等。该文件还包含注释或文档,其中每一行以“#”开头",说明这是一条评论。目前我的情况如下。
$file = Get-Content .\allsystems.csv
$arraysplit = @()
$arrayfinal = @()
[int]$windows = 0
foreach ($thing in $file){
if ($thing.StartsWith("#")) {
continue
}
else {
$arraysplit = $thing.Split(":")
$arrayfinal = @($arraysplit[0], $arraysplit[1])
}
}
foreach ($item in $arrayfinal){
if ($item[1] -contains 'NT'){
$windows++
}
else {
continue
}
}
$windows
此脚本的目标是计算 Windows 服务器的总数。我的问题是第一个 "foreach" 块工作正常,但第二个导致“$Windows”为 0。老实说,我不确定为什么这不起作用。两行示例数据如下:
example:LNX
example2:NT
$arrayfinal = @($arraysplit[0], $arraysplit[1])
这将替换每个 运行 的数组。
将其更改为 +=
又出现了另一个问题。它只是附加了每个单独的元素。我用这个 post 的信息来修复它,有点强制二维数组:How to create array of arrays in powershell?.
$file = Get-Content .\allsystems.csv
$arraysplit = @()
$arrayfinal = @()
[int]$windows = 0
foreach ($thing in $file){
if ($thing.StartsWith("#")) {
continue
}
else {
$arraysplit = $thing.Split(":")
$arrayfinal += ,$arraysplit
}
}
foreach ($item in $arrayfinal){
if ($item[1] -contains 'NT'){
$windows++
}
else {
continue
}
}
$windows
1
我还更改了文件并添加了更多 NT 和其他随机垃圾的实例。似乎它工作正常。
我会避免创建另一个 ForEach 循环来增加出现次数。你的 $arrayfinal 每次都会重写,所以我使用了 ArrayList。
$file = Get-Content "E:\Code\PS\myPS18\Jun\allSystems.csv"
$arrayFinal = New-Object System.Collections.ArrayList($null)
foreach ($thing in $file){
if ($thing.StartsWith("#")) {
continue
}
else {
$arraysplit = $thing -split ":"
if($arraysplit[1] -match "NT" -or $arraysplit[1] -match "Windows")
{
$arrayfinal.Add($arraysplit[1]) | Out-Null
}
}
}
Write-Host "Entries with 'NT' or 'Windows' $($arrayFinal.Count)"
我不确定你是否想保留 'Example'、'example2'... 所以我跳过了将它们添加到 arrayfinal,假设目标是计算 "NT" 或"Windows" 次出现
如果目标是计算 windows 个服务器,为什么需要数组?
你就不能像这样说吗
foreach ($thing in $file)
{
if ($thing -notmatch "^#" -and $thing -match "NT") { $windows++ }
}
The goal of this script is to count the total number of Windows servers.
我建议使用简单的方法:使用为此构建的 cmdlet。
$csv = Get-Content -Path .\file.csv |
Where-Object { -not $_.StartsWith('#') } |
ConvertFrom-Csv
@($csv.servertype).Where({ $_.Equals('NT') }).Count
# Compatibility mode:
# ($csv.servertype | Where-Object { $_.Equals('NT') }).Count
将 servertype
和 'NT'
替换为任何 header/value 的名称。