简体   繁体   English

如何访问数组中的标头信息

[英]How Do I access header information in an array

First off I am new to PowerShell and have written code in many different languages. 首先,我是PowerShell的新手,并用许多不同的语言编写了代码。 Most of my experience was from java so typically I format my code from that background so I apologize it if it looks crazy. 我的大部分经验都来自Java,因此通常我会从该背景格式化我的代码,因此如果它看上去很疯狂,我会道歉。

I am trying to make a script that I can run against a CSV that will load each of the objects into an array. 我正在尝试制作一个可以针对CSV运行的脚本,该脚本会将每个对象加载到数组中。 I want to then check each object to make sure it has an e-mail address, Firstname, and Lastname attached to it. 然后,我想检查每个对象以确保它具有电子邮件地址,名字和姓氏。 We are currently deleting invalid entries by hand. 我们目前正在手动删除无效条目。

I have the following code below that I am working with. 我在下面使用以下代码。 I am attempting to import the CSV. 我正在尝试导入CSV。 I want to read each element in the array and remove the entries that are not meeting my criteria, I then want to export that to a CSV. 我想读取数组中的每个元素,并删除不符合我的条件的条目,然后将其导出到CSV。 I am confused on what the objects look like in the array, how to check if the information in each object is null. 我对数组中对象的外观,如何检查每个对象中的信息是否为null感到困惑。 Any assistance on logic would be nice. 关于逻辑的任何帮助都将是不错的。 The CSV has the headers listed below, but I only care about the onces listed above. CSV具有下面列出的标题,但是我只关心上面列出的内容。 Thanks, ' $csv = Read-Host "enter file path to CSV" 谢谢,'$ csv =读取主机“输入CSV的文件路径”

$CsvArray = Import-Csv $csv -Header Lastname,Firstname,Department,Phonenumber,Email

  foreach ($i in $CsvArray)
   {
     Write-Host $CsvArray($i)

  if(Lastname -eq $NULL) -or (firstname -eq $NULL) -or (Email -eq $NULL)
     {
       Delete $i from array 
      }
  else
     {
     $i
     }
 } 

 $csvArray | Export-Csv c:\emaillist.txt -force


 Write-Host "Press any key to continue ..."

$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")

` `

$CsvArray = Import-Csv $csv -Header Lastname,Firstname,Department,Phonenumber,Email |
Where-Object { $_.Lastname -ne $null -and $_.Firstname -ne $null -and $Email -ne $null } |
Export-Csv C:\emaillist.txt -force

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM