简体   繁体   English

使用 Powershell 将逗号分隔的字符串转换为数组

[英]Convert comma separated string to an array with Powershell

This is probably a simple thing I am missing but I am trying to convert a comma separated variable into a csv like formatted array ($myarray) using powershell.这可能是我缺少的一件简单的事情,但我正在尝试使用 powershell 将逗号分隔的变量转换为 csv 格式的数组($myarray)。

FROM:从:

$ftitemsname = "itemname"
$ftitemssrc= "\\server\folder\a"
$ftitemsdst = "/sftp/items/"
$ftitemstiming = "4" 
   # 4 AM Execution
$ftobjectsSrc = "/sftp/objects/"
$ftobjectsdst = "\\server\folder\b"
$ftobjectsTiming = "4" "\\server\folder\a","/sftp/objects/","4"

#<WINSCP File Transfer Snippet>
# Download files
       $transferResult1 = $session.GetFiles($ftitemsSrc, ($ftitemsdst + $itemsname), $true, $transferOptions)
       $transferResult.Check()
       Write-EventLog -LogName "Application" -Source "Winscp" -EventID 51221 -EntryType Information -Message "Upload of {0} succeeded -f $transfer.FileName" -Category 1 -RawData 10,20
  
   
# Download files
       $transferResult1 = $session.GetFiles($ftobjectsSrc, ($ftobjectsdst + $objectsname), $true, $transferOptions)
       $transferResult.Check()
       Write-EventLog -LogName "Application" -Source "Winscp" -EventID 51221 -EntryType Information -Message "Upload of {0} succeeded -f $transfer.FileName" -Category 1 -RawData 10,20

To make it look like this in my powershell array.让它在我的 powershell 数组中看起来像这样。 Csv representation of the array数组的 CSV 表示

I don't want it to actually export to a csv I want to use it in order to pipe those values into a powershell array.我不希望它实际导出到 csv 我想使用它来将这些值通过管道传输到 powershell 数组中。

I want to use that array to start an action to them.我想用那个数组对他们开始一个动作。

This is the manual way I was building it.. but I want to simplify it for others to edit a simply comma separated list versus creating all net new variables.这是我构建它的手动方式..但我想简化它以供其他人编辑简单的逗号分隔列表而不是创建所有净新变量。

Eventually, I will be using a foreach value to perform action.最终,我将使用一个 foreach 值来执行操作。

TO:至:

    $myheaders = "name, source, destination, timing" 
    $mylist1 = "objects","\\server\folder\a","/sftp/objects/","4"
    $mylist2= "items","\\server\folder\a","/sftp/items/","4"

foreach($item2do in $entries){
    $transferResult1 = $session.GetFiles($item2do.source, ($item2do.destination + $item2do.name), $true, $transferOptions)
        $transferResult.Check()
        Write-EventLog -LogName "Application" -Source "Winscp" -EventID 51221 -EntryType Information -Message "Upload of {0} succeeded -f $transfer.FileName" -Category 1 -RawData 10,20
        }

I'm not quite sure I understood the premise of the question but, what you're looking for might be accomplished by hardcoding the CSV in the code itself, this would solve the "but I want to simplify it for others to edit a simply comma separated list versus creating all net new variables."我不太确定我是否理解问题的前提,但是,您正在寻找的可能是通过在代码本身中硬编码CSV 来完成,这将解决“但我想简化它以供其他人简单地编辑逗号分隔列表与创建所有净新变量。” . .

So for this you can use a here-string combined with ConvertFrom-Csv :因此,为此您可以使用here-stringConvertFrom-Csv结合使用:

@'
"name","source","destination","timing"
"objects","\\server\folder\a","/sftp/objects/","4"
"items","\\server\folder\a","/sftp/items/","4"
'@ | ConvertFrom-Csv | ForEach-Object {
    $transferResult = $session.GetFiles($_.source, ($_.destination + $_.name), $true, $transferOptions)
    $transferResult.Check()
    Write-EventLog -LogName "Application" -Source "Winscp" -EventID 51221 -EntryType Information -Message "Upload of $($_.FileName) succeeded" -Category 1 -RawData 10,20
}

Technically you could use a hardcoded Json too, but adding new objects to process may be harder in this case.从技术上讲,您也可以使用硬编码的 Json,但在这种情况下,添加新对象进行处理可能会更难。 This method uses ConvertFrom-Json :此方法使用ConvertFrom-Json

@'
[
  {
    "name": "objects",
    "source": "\\\\server\\folder\\a",
    "destination": "/sftp/objects/",
    "timing": "4"
  },
  {
    "name": "items",
    "source": "\\\\server\\folder\\a",
    "destination": "/sftp/items/",
    "timing": "4"
  }
]
'@ | ConvertFrom-Json | ForEach-Object {
    $transferResult = $session.GetFiles($_.source, ($_.destination + $_.name), $true, $transferOptions)
    $transferResult.Check()
    Write-EventLog -LogName "Application" -Source "Winscp" -EventID 51221 -EntryType Information -Message "Upload of $($_.FileName) succeeded" -Category 1 -RawData 10,20
}

Thank you for your help @santiago-squarzon.感谢您的帮助@santiago-squarzon。 Here is my final code based on what you had.这是我根据您所拥有的最终代码。

$myheaders = "name, source, destination, timing"
$mylist1 = "objects,\\server\folder\a,/sftp/objects/,4"
$mylist2= "items,\\server\folder\a,/sftp/items/,4"

$mywork = @"
$myheaders
$mylist1
$mylist2
"@
$mywork | ConvertFrom-Csv | ForEach-Object {
    $transferResult = $session.GetFiles($_.source, ($_.destination + $_.name), $true, $transferOptions)
    $transferResult.Check()
    Write-EventLog -LogName "Application" -Source "Winscp" -EventID 51221 -EntryType Information -Message "Upload of $($_.FileName) succeeded" -Category 1 -RawData 10,20
}

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

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