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: Extract Root Domain from URL

Please note that this function requires an updated variable named $domainsDictionary. Since LTD's are being…

PowerShell: Function to Get Group Members as a Bypass Orphanated SID Errors

Problem: [TESTSERVER]: PS C:\Users\administrator> Get-LocalGroupMember 'Remote Desktop Users' Get-LocalGroupMember : Failed to compare two elements…

Basic CSS: Prioritize One Style Over Another

<style>body {background-color: black;font-family: monospace;color: green;}</style><h1>Hello World!</h1>