How can I source variables from a .bat file into a PowerShell script?

后端 未结 5 1436
春和景丽
春和景丽 2020-12-10 02:25

I\'m replacing parts of a .bat script with PowerShell. Configuration for the batch files is done via files that set appropriate environment variables. I\'m look

相关标签:
5条回答
  • 2020-12-10 02:51

    If you are using the PowerShell Community Extensions, it has a Invoke-BatchFile that does this. I use with the Visual Studio vcvarsall.bat file to configure my PowerShell session to use the Visual Studio tools.

    0 讨论(0)
  • 2020-12-10 03:00

    I'd parse them (just skip all lines that don't start with set and split them with first = character. You can do it from o small C# cmdlet or directly with a small PowerShell script:

    CMD /c "batchFile.bat && set" | .{process{
        if ($_ -match '^([^=]+)=(.*)') {
            Set-Variable $matches[1] $matches[2]
        }
    }}
    

    I have this code and I'm sure it comes from somewhere but credits have been lost, I suppose it comes from Power Shell Community Extensions for an Invoke-Batch script.

    0 讨论(0)
  • 2020-12-10 03:03

    Assuming the .bat is called test.bat, define testps.ps1:

    $lines = cat "test.bat"
    $output = @{};
    
    foreach ($line in $lines) {
        $bits = $line.Split("=");
        $name = $bits[0].Split(" ")[1];
        $val = $bits[1];
        $output[$name] = $val
    }
    
    return $output
    

    Then the result is something like:

    C:\temp> .\testps.ps1
    
    Name                           Value
    ----                           -----
    VAR_TWO                        /other-value
    VAR_ONE                        some_value
    
    
    C:\temp> $x = .\testps.ps1
    C:\temp> $x
    
    Name                           Value
    ----                           -----
    VAR_TWO                        /other-value
    VAR_ONE                        some_value
    
    
    C:\temp> $x["VAR_ONE"]
    some_value
    

    There is probably a nicer way of doing the splits (will edit if I find it)

    0 讨论(0)
  • 2020-12-10 03:07

    The preferred option would be to change the configuration to a .ps1 file and change the variable definitions to PowerShell syntax:

    $VAR_ONE = 'some_value'
    $VAR_TWO = '/other-value'
    

    Then you'll be able to dot-source the file:

    . filename.ps1
    

    If you want to stick with the format you currently have, you'll have to parse the values, e.g. like this:

    Select-String '^set ([^=]*)=(.*)' .\filename.bat | ForEach-Object {
        Set-Variable $_.Matches.Groups[1].Value $_.Matches.Groups[2].Value
    }
    

    Note: The above won't work in PowerShell versions prior to v3. A v2-compatible version would look like this:

    Select-String '^set ([^=]*)=(.*)' .\filename.bat | ForEach-Object {
        $_.Matches
    } | ForEach-Object {
        Set-Variable $_.Groups[1].Value $_.Groups[2].Value
    }
    
    0 讨论(0)
  • 2020-12-10 03:09

    You can do that via a Batch file that first call the configuration file and then execute the PowerShell script.

    0 讨论(0)
提交回复
热议问题