0

good vs no result

I'm trying to make a dynamic multiple selection menu via powershell, based on data imported via a CSV file.

After importing objects via CSV, and try to add multiple filters on the SEL and RESTART fields and on their values. I see a strange behavior of the counter, to count the number of objects which are conform the different conditions/filters. It seems that counting 01 returns no display ... suggestion, ideas (I've it also done with one filtering ... same problem)

## MENU
$csvFilePathMENU=CheckFileExcist –FileName $csvFileMENU -FilePath $csvPathMENU
# import the objects ... and put it into a variable
$global:menus = Import-Csv $csvFilePathMENU
$global:nrElemsMENU=$menus.Count 
write-output "$($nrElemsMENU)"

I tried two different ways ... to filter

$nrRestarts=$($menus | Where-Object {( $_.RESTART  -as [int]  -eq 1 )}  | Where-Object {( $_.SEL  -as [int]  -eq 1 )} ).count
write-host  "nrRestarts : ", $nrRestarts
$nrRestarts2=$($menus | Where {( $_.RESTART  -as [int]  -eq 1 ) -and ( $_.SEL  -as [int]  -eq 1 )}).count
write-host  "nrRestarts : ", $nrRestarts2

[I've tried it with several combinations]

left side picture

INPUT CSV 01 : where SEL and RESTART are 1

MENU     : MEINBERG
SEL      : 1
RESTART  : 1
...
MENU     : NTP
SEL      : 1
RESTART  : 1

RESULT 01 :

  • nrRestarts : 2
  • nrRestarts : 2

right side picture

INPUT CSV 02 : where SEL and RESTART are 1

MENU     : NTP
SEL      : 1
RESTART  : 1

RESULT 02 :

  • nrRestarts :
  • nrRestarts :

WRONG/NO RESULT ... ??? 01 ??? any body a suggestion?


In the Windows Powershell ISE ... the feedback

PS C:\Users\Administrator> $nrElemsMENU
9

PS C:\Users\Administrator> $menus


MENU     : anti-virus : Windows-Defender
info     : Windows-Defender has to be uninstalled, before installing an other anti-virus program
function : Windows-Defender
status   : -1
SEL      : 0
RESTART  : 0
FUNC     : stopWindowsDefender
PARENT   : Anti-virus

MENU     : Windows 'ServerStandard'
info     : Windows has to be upgraded if working with an EVALUATION prod key
function : 
status   : -1
SEL      : 0
RESTART  : 1
FUNC     : 
PARENT   : 

MENU     : F-SEC
info     : F-SEC has to be configured as an isolated machine on the CSI server
function : F-SEC
status   : -1
SEL      : 0
RESTART  : 0
FUNC     : startFSec
PARENT   : 

MENU     : NTP
info     : disable default Windows NTP service
function : 
status   : -1
SEL      : 1
RESTART  : 0
FUNC     : 
PARENT   : NTP

MENU     : MEINBERG
info     : disable default Windows NTP service
function : 
status   : -1
SEL      : 0
RESTART  : 1
FUNC     : 
PARENT   : 

MENU     : NTP
info     : disable default Windows NTP service
function : 
status   : -1
SEL      : 1
RESTART  : 1
FUNC     : 
PARENT   : 

MENU     : openssl
info     : disable default Windows NTP service
function : 
status   : -1
SEL      : 0
RESTART  : 0
FUNC     : 
PARENT   : OpenSSL

MENU     : execute
info     : 
function : 
status   : -1
SEL      : 0
RESTART  : 1
FUNC     : 
PARENT   : 

MENU     : quite
info     : 
function : 
status   : -1
SEL      : 0
RESTART  : 1
FUNC     : 
PARENT   :
kris
  • 392
  • 4
  • 16
  • Please create a [mcve] with raw CSV data instead of the formatted output, so helpful users can copy/paste to try and reproduce. Also please state the PowerShell version as behaviour of `count` property has [changed between versions](https://stackoverflow.com/questions/26504589/how-does-the-count-property-work-in-powershell). – zett42 Apr 17 '21 at 13:46
  • 1
    Did you try `$nrRestarts = @($menus | Where-Object { [int]$_.RESTART -eq 1 -and [int]$_.SEL -eq 1 }).Count` ? or `$nrRestarts = @($menus | Where-Object { $_.RESTART -eq '1' -and $_.SEL -eq '1' }).Count` – Theo Apr 17 '21 at 13:55
  • @Theo, Thanks. It seems that your response is the answer : `@(` Could you also explain, why this works? and/or why the otherway doesn't work? -- To be honest I'm little confused because only 1 was not returned back. – kris Apr 17 '21 at 14:12
  • 1
    The `@(..)` forces the output to become an array, which has a valid `.Count` property. What you have tried is surrounding it with `$(..)` making it a sub expression, but that does not guarantee the result will be an array. – Theo Apr 17 '21 at 14:14

0 Answers0