Posted On March 29, 2019

PowerShell: Convert String to Command

kimconnect 0 comments
blog.KimConnect.com >> Codes >> 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 *

Related Post

Common JavaScript Techniques

Common JavaScript Techniques:1. Demonstate your understanding of they keyword typeofconsole.log(typeof typeof 1);// this follows the…

Changing SMB Caching on Windows

# To turn OFF caching $rolesWithNoCaching="SHERVER01","SHERVER02" $rolesWithNoCaching | %{$shares=Get-SMBShare -scopename "$_"; foreach ($share in $shares){if(!($share.Name…

How to Import Files Into a Docker Container

1. Use SCP to copy files to the remote server while logged onto the local…