1

I have a INI file on the web. I want to parse this INI file using Get-IniContent. However switch -regex -file doesn't support URL.

Tried to download file and use switch without -file and didn't work:

$content = (Invoke-WebRequest URL).Content
switch -regex $content {
}

Tried to add (?m) in the regex pattern without success.

Any idea of how can I simulate behavior of -file switch with multiline string? One other option would be to make the webcontent return a "on memory file". I just doesn't want to create a temp file to use in the switch statement.

Ty

  • Just evaluate every line via the basic PS method: pipeline? `$content -split "\`n" | %{ switch -regex $_ { ..... } }` Using the switch statement from [INI file parsing in PowerShell](http://stackoverflow.com/a/422529) – wOxxOm Aug 25 '16 at 00:09

1 Answers1

0

Instead of creating a temporary file you may use a memory stream. Here is the example.

$webClient = New-Object System.Net.WebClient
$data = $webClient.DownloadData("http://site/file.ini")
$memoryStream = New-Object System.IO.MemoryStream($data, [System.Text.Encoding]::Default)
$streamReader = New-Object System.IO.StreamReader($memoryStream)

# Original script content here

while (($line = $streamReader.ReadLine()) -ne $null) {
    switch -Regex ($line) {

    # Original script content here

    }
}

# Original script content here
t1meless
  • 504
  • 2
  • 9