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.
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. Csv representation of the array
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.
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.
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
}
CodePudding user response:
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.".
So for this you can use a 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
}
Technically you could use a hardcoded Json too, but adding new objects to process may be harder in this case. This method uses 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
}
CodePudding user response:
Thank you for your help @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
}