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

Convert comma separated string to an array with Powershell

这可能是我遗漏的一个简单的事情,但我正在尝试使用 powershell 将逗号分隔的变量转换为类似格式化数组 ($myarray) 的 csv。

发件人:

$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

让它在我的 powershell 数组中看起来像这样。 Csv representation of the array

我不希望它实际导出到 csv 我想使用它来将这些值通过管道传输到 powershell 数组中。

我想用那个数组对他们开始一个动作。

这是我构建它的手动方式..但我想简化它以供其他人编辑一个简单的逗号分隔列表而不是创建所有净新变量。

最终,我将使用 foreach 值来执行操作。

收件人:

    $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
        }

我不太确定我是否理解问题的前提,但是,您正在寻找的可能是通过 硬编码 代码本身中的 CSV 来完成的,这将解决 " 但我想简化它以供其他人编辑一个简单的逗号分隔列表而不是创建所有净新变量。".

所以为此你可以使用 here-string combined with ConvertFrom-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
}

从技术上讲,您也可以使用硬编码 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
}

感谢您的帮助@santiago-squarzon。这是我根据您所拥有的最终代码。

$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
}