r/PowerShell • • 2h ago

Script Sharing [ Removed by moderator ]

[removed] — view removed post

0 Upvotes

10 comments sorted by

View all comments

1

u/MonkeyNin 1h ago

Safety tip: Remember don't run code like https://getshell.pw/sh | bash or https://getshell.pw/sh | iex` unless you trust the author.

for docker.ps1 you don't need Invoke-Expression or Join-String to run docker here

Build an array where spaces create a new element:

$commandToInvoke = @(
    "docker",
    "run",
    "--rm",
    "--name", 
    "$( (New-Guid).ToString() )",
    "--interactive",
    "--tty",
    
    # ... 
    
    "--env", "TERM=xterm-256color",
    "--env", "DOTFILES_DEBUG=true",
    "--network=host"
)

& $commandToInvoke

instead of:

$commandToInvoke = @(
    "docker run",
    "--rm",
    "--name $((New-Guid).ToString())",

You can make it safer by ignoring functions named docker. Otherwise you could end up calling that instead.

$cmd = Get-Command 'docker' -CommandType Application -TotalCount 1 -ErrorAction Stop

$CmdArgs = @( 'docker', 'run', '--rm' ) # ...
& $cmd @CmdArgs

1

u/sashag90 1h ago

Thanks, I'll apply it in my scripts