1

I'm really hoping somebody can put me out of hours of searching for an answer on this one.

I'm running a script against a CSV to locate a set value (servers) in column B and delete the row if they are found. The script works when I pass a single parameter through it, but when I try with multiple strings or a variable it doesn't work.

$H = 1..40
$Data = Get-Content "Pre-Test.csv" | Select-Object -Skip 1 | ConvertFrom-Csv -Header $h
$Data | Where-Object {$_.B -ne 'Server1' -or 'Server2' -or 'Server3'} | Export-Csv "Post-Test.csv"

I've also tried the following to no avail:

$H = 1..40
$Data = Get-Content "Pre-Test.csv" | Select-Object -Skip 1 | ConvertFrom-Csv -Header $h
$Data | Where-Object {$_.B -cin 'Server1', 'Server2', 'Server3'} | Export-Csv "Post-Test.csv"

and:

$H = 1..40
$Data = Get-Content "Pre-Test.csv" | Select-Object -Skip 1 | ConvertFrom-Csv -Header $h
$Data | Where-Object {$_.B -ne 'Server1', $_.B -ne 'Server2', $_.B -ne 'Server3'} | Export-Csv "Post-Test.csv"
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
neatdoody
  • 11
  • 2
  • I should also add that I don't get any errors after the script runs – neatdoody Aug 03 '22 at 01:28
  • The answers to the linked post explain why `$_.B -ne 'Server1' -or 'Server2' -or 'Server3'` doesn't work as you expect. In short: use `$_.B -in 'Server1', 'Server2', 'Server3'` – mklement0 Aug 03 '22 at 01:31
  • I tried the linked answer however my Post-test.csv is now blank. I've appended my question since I'm still left without an answer :( – neatdoody Aug 03 '22 at 01:50
  • Sorry. that should have been `-notin`, i.e, `$_.B -notin 'Server1', 'Server2', 'Server3'`, assuming that you won't to exclude all servers named. – mklement0 Aug 03 '22 at 02:10
  • No worries, I appreciate the follow up! I tried adding the -notin parameter, and while it brought back data in the post-test.csv, the rows with the values still are not getting deleted – neatdoody Aug 03 '22 at 02:45
  • Your headers are named `1`, `2`, .... Therefore, there is no `B` property (column). Did you mean `$_.2 -notin ...`? – mklement0 Aug 03 '22 at 02:47
  • YES this was it. Oh mklement0, whoever you are I am deeply deeply grateful to you! What a silly mistake but thank you a million times over. I changed to $_.2 and its working now! D'Oh! – neatdoody Aug 03 '22 at 02:52
  • Glad to hear it - an easy mistake to make. My pleasure. – mklement0 Aug 03 '22 at 02:53

0 Answers0