Convertfrom-csv

Hi,

I am looking for some explanation about the cmd convertfrom-csv.
Thsi part is realy new to me, and I try to find it on microsoft and onthers, but I do not get very far.

I see this line " Convertfrom-csv" in a script however to me it makes no sense yet.

MS tells me" converts character-separated value (CSV) data to PSObject type"
I understand it if the data is already a csv file, only how does it work in a case like this…:

type or paste c#create spn
@'
MSSQLSvc/W1092T0071.domainl:tst_01
MSSQLSvc/W1092T0097.domain.nl:1433
MSSQLSvc/W1092T0097.domain.nl:tst_01
'@ | ConvertFrom-Csv -Header "Name" | Select -first 1 | ForEach-Object {
    $spn = $_.Name
    $command = "setspn.exe -D $spn sacicappsql"
    Write-Host "Executing command: $command"
    Invoke-Expression $command
}

Hopefully I understand it well with the answer.

Powershell is a very object oriented scripting language, so recommend trying to understand that concept:

Understanding PowerShell Objects | Petri IT Knowledgebase

A CSV is a flat-file data structure. The main purpose is getting external data into Powershell so that cmdlets can use it. All the developer is doing is trying to get data converted into something that Powershell can use, a PSObject, these all do the same thing:

#Older method
$psObject = @()
$psObject += New-Object -TypeName psobject -Property @{FirstName='John';LastName='Smith'}
$psObject += New-Object -TypeName psobject -Property @{FirstName='Sally';LastName='Wu'}


#Newer method with pscustomobject accelerator
$psObject = @()
$psObject += [pscustomobject]@{FirstName='John';LastName='Smith'}
$psObject += [pscustomobject]@{FirstName='Sally';LastName='Wu'}

#csv method
$psObject = @"
FirstName,LastName
John,Smith
Sally,Wu
"@ | ConvertFrom-Csv

In that example provided, they are using a Here-String with just the data and adding a Name header (Same thing as FirstName and LastName above):

#Same thing, different way of doing it
@'
Name
MSSQLSvc/W1092T0071.domainl:tst_01
MSSQLSvc/W1092T0097.domain.nl:1433
MSSQLSvc/W1092T0097.domain.nl:tst_01
'@  | ConvertFrom-CSV

Here is another more structured way of doing the same thing (except removed Select -First 1 to process all rows, not just the first):

$myInstances = @'
Name
MSSQLSvc/W1092T0071.domainl:tst_01
MSSQLSvc/W1092T0097.domain.nl:1433
MSSQLSvc/W1092T0097.domain.nl:tst_01
'@ | ConvertFrom-Csv 

foreach ($spn in $myInstances) {
    $command = "setspn.exe -D $($spn.Name) sacicappsql"
    Write-Host "Executing command: $command"
    # Invoke-Expression $command
}

Results in the same end result (except removed Select -First 1 to process all rows, not just the first):

Executing command: setspn.exe -D MSSQLSvc/W1092T0071.domainl:tst_01 sacicappsql
Executing command: setspn.exe -D MSSQLSvc/W1092T0097.domain.nl:1433 sacicappsql
Executing command: setspn.exe -D MSSQLSvc/W1092T0097.domain.nl:tst_01 sacicappsql

Hello Rob Simers,

Thank you, it is what I expected as in its converts data into something that is usefull for PS.
I am confused by the fact that it states ConvertFrom-csv, since CSV is a commaseparated file and I do not see any kind of csv format in this data.

As in your answer : Convertto-csv would make sense to me, only that seems to be a misunderstanding