PowerShell: Convert String to Command

# Method 1 
$command =
@"
ping google.com
"@
$scriptBlock = [Scriptblock]::Create($command)
Invoke-Command -ComputerName localhost -ScriptBlock $scriptBlock

# Method 2
$xVariable="K1"
$yVariable="ping"
$commandString=$yVariable+" "+$xVariable
function runCommand($command) {
if ($command[0] -eq '"') { iex "& $command" }
else { iex $command }
}
runCommand $commandString

Leave a Reply

Your email address will not be published. Required fields are marked *