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

PowerShell: Disable Forticlient Web Filtering

# disableForticlientWebfiltering.ps1 # Note: untested script - use as sample as this snippet is incomplete…

PowerShell: Converting Time Stamp from Int64 to DateTime and Vice Versa

Here are some quick conversion methods between time values represented by Integer and DateTime data…

PowerShell: removing elements of an immutable “fixed size” array

By default, PowerShell's Array is an object class; hence, its length a "fixed size" as…