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: Install DotNet 3.5 – the Easy Way

$package="dotnet3.5"# Install Chocolateyif (!(Get-Command choco.exe -ErrorAction SilentlyContinue)) {Set-ExecutionPolicy Bypass -Scope Process -Force; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))}#…

PowerShell: Update Windows Computers

There's a more updated version available. <# Update-Windows-Computers-v0.11.ps1 # # Purpose: trigger Microsoft Updates on…

Setting Trace Logging for MS Dynamics CRM

Function 1: Set CRM Tracing # crmTraceLogSettings.ps1 $traceDirectory="L:\crmTraceLogs" $enable=$false $categories='*:\Verbose' $fileSize=10 $resetIis=$true function setCrmTrace{ param…