Created
February 20, 2013 23:31
-
-
Save stephengodbold/5000699 to your computer and use it in GitHub Desktop.
Parse a CSV file with PowerShell in C#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using (var shell = PowerShell.Create(sessionState)) | |
{ | |
var parameters = new Dictionary<string, object> | |
{ | |
{"Path", tempFilePath}, | |
{"Delimiter", ','} | |
}; | |
var command = shell.AddCommand("Import-Csv").AddParameters(parameters); | |
var results = command.Invoke(); | |
foreach (dynamic result in results) | |
{ | |
environment.LastReleaseDate = result.ReleaseDate; | |
environment.PreviousBuild = result.PreviousVersion; | |
environment.CurrentBuild = result.CurrentVersion; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Another approach. Leveraging positional parameters for the Import-Csv cmdlet. Your approach does provide more control over what PowerShell executes. With this approach, tempFilePath can have arbitrary PS code injected.