简体   繁体   English

输出数组为CSV

[英]Output arrays to CSV

I have two CSVs: 我有两个CSV:

  1. Total 19_01_16.csv : Total 19_01_16.csv

     hostname,user,path,size,creation,LastAccess,Copied,NewName,Completed 主机名,用户,路径,大小,创建,LastAccess,已复制,新名称,已完成\ncomp1,user1,\\\\comp1\\users1\\file.pst,100,17/02/2015,17/01/2016,Yes,file_user1_.pst, comp1,user1,\\\\ comp1 \\ users1 \\ file.pst,100,17 / 02 / 2015,17 / 01/2016,是,file_user1_.pst,\ncomp1,user1,\\\\comp1\\users1\\file2.pst,200,17/02/2015,17/01/2016,Yes,file2_user1_.pst, comp1,user1,\\\\ comp1 \\ users1 \\ file2.pst,200,17 / 02 / 2015,17 / 01/2016,是,file2_user1_.pst,\ncomp2,user2,\\\\comp2\\users2\\file.pst,100,17/02/2015,17/01/2016,Yes,file_user2_.pst, comp2,user2,\\\\ comp2 \\ users2 \\ file.pst,100,17 / 02 / 2015,17 / 01/2016,是,file_user2_.pst, 
  2. PST Passwords.csv : PST Passwords.csv

     user,Path,Password1,Password2,Password3,Error 用户,路径,密码1,密码2,密码3,错误\nuser1,\\\\comp1\\users1\\file.pst,openme,openme,openme, user1,\\\\ comp1 \\ users1 \\ file.pst,openme,openme,openme, 

I'm trying to merge the two with different headers and additional content. 我正在尝试将两个具有不同标题和其他内容的内容合并。 This is what I have so far: 这是我到目前为止的内容:

$a = Import-Csv "$PST_PARENT\DailyReports\Total 19_01_16.csv"
"Hostname,User,PST_Name,Original_Path,New_Path,Size,AcceptableLoss,Password1,Password2,Password3" |
  Set-Content "$PST_PARENT\DailyReports\New Build.csv"

$a | foreach {
  $HOSTNAME  = $_.hostname
  $USER      = $_.User 
  $PATH      = $_.path
  $NEW_NAME  = $_.NewName
  $NEWPATH   = "$PST_SHARE\$USER\$NEW_NAME"
  $SIZE      = $_.Size
  $SIZE_FAIL = ( [convert]::ToSingle( $SIZE ) / 2 )

  $b = Import-Csv "$PST_PARENT\DailyReports\PST Passwords.csv"

  $b | foreach {
    if ( $USER -like $b.user ) {
      $PASSWORD1 = $b.password1 
      $PASSWORD2 = $b.password2
      $PASSWORD3 = $b.password3
    } else {
      $PASSWORD1 = "none"
      $PASSWORD2 = "none"
      $PASSWORD3 = "none"
    }
  }

  $HOSTNAME,$USER,$NEW_NAME,$PATH,$NEWPATH,$SIZE,$SIZE_FAIL,$PASSWORD1,$PASSWORD2,$PASSWORD3 | 
    Add-Content "$PST_PARENT\DailyReports\New Build.csv"
}

The output of New Build.csv looks like this: New Build.csv的输出如下所示:

Hostname,User,PST_Name,Original_Path,New_Path,Size,AcceptableLoss,Password1,Password2,Password3
comp1
user1
file.pst
\\comp1\users1\file.pst
\\share\PST_Storage\file_user1_.pst
100
5
none
none
none

In essence the output is working, it's just not scrolling for each line, it's putting each array onto a new line. 本质上,输出是有效的,它只是不滚动每一行,而是将每个数组放到新行中。 I tried adding | ConvertTo-Csv -NoTypeInformation 我尝试添加| ConvertTo-Csv -NoTypeInformation | ConvertTo-Csv -NoTypeInformation but all that did was convert the arrays to numbers, they still went down not across. | ConvertTo-Csv -NoTypeInformation但所做的只是将数组转换为数字,但它们仍然没有下降。

Any ideas? 有任何想法吗? Am I on the right line or doing the whole thing so very wrong? 我是在正确的路线上还是在做整个事情时非常错误?

$HOSTNAME,$USER,... defines an array, which is written to the output file one element per line. $HOSTNAME,$USER,...定义一个数组,该数组每行一个元素写入输出文件。 You need to put the list in double quotes to turn it into a comma-separated string that you can write to the output file as a single line. 您需要将列表用双引号引起来,以将其转换为逗号分隔的字符串,然后可以将其作为一行写入输出文件。

"Hostname,User,PST_Name,Original_Path,New_Path,Size,AcceptableLoss,Password1,Password2,Password3" |
  Set-Content "$PST_PARENT\DailyReports\New Build.csv"

$a | foreach { 
  ...
  "$HOSTNAME,$USER,$NEW_NAME,$PATH,$NEWPATH,$SIZE,$SIZE_FAIL,$PASSWORD1,$PASSWORD2,$PASSWORD3"
} | Add-Content "$PST_PARENT\DailyReports\New Build.csv"

or you construct a custom object from the elements that you can export via Export-Csv . 或者,您可以根据可以通过Export-Csv的元素构造自定义对象。

$a | foreach { 
  ...
  New-Object -Type PSCustomObject -Property @{
    'Hostname'  = $HOSTNAME
    'User'      = $USER
    'NewName'   = $NEW_NAME
    'Path'      = $PATH
    'NewPath'   = $NEWPATH
    'Size'      = $SIZE
    'SizeFail'  = $SIZE_FAIL
    'Password1' = $PASSWORD1
    'Password2' = $PASSWORD2
    'Password3' = $PASSWORD3
  }
} | Export-Csv "$PST_PARENT\DailyReports\New Build.csv" -NoType

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

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