Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

PowerShell 7 – how to run a command on windows?

I just want to run a docker command with parameters from PowerShell 7 script. When I just use the command:

if ($isRunning -eq $false) {
   docker run -it --rm -p 8080:8080 -v <some_path> <image_name>
}

it works. But I need to use a variable in this command, so I changed it to:

& "docker run -it --rm -p 8080:8080 -v $script:myPath\:/usr/local/whatever"

And it runs but then it tells me The term ‘docker run -it …’ is not recognized as a name of a cmdlet…. It seems like PowerShell script does not have the same $PATH environment variable value as cmd.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

What’s wrong here?

>Solution :

The following might work, passing the arguments to your docker command as an array of arguments instead of embedding all the expression in a string which is currently failing because PowerShell is trying to find a command with the name of the entire expression (docker run -it ...).

if (-not $isRunning) {
    docker @(
        'run'
        '-it'
        '--rm'
        '-p'
        '8080:8080'
        '-v'
        "$script:myPath\:/usr/local/whatever"
    )
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading