Как вызвать команду из строки? - PullRequest
0 голосов
/ 08 января 2019

Всякий раз, когда я пытаюсь вызвать команду из строки, я получаю синтаксическую ошибку.

Это для временной консоли для определенных пользователей, но я хочу разрешить только определенные пользовательские команды.

Я уже пробовал следующее: & $ functionName $ paramater1 $ paramater2 Invoke-Command -ScriptBlock {$ functionName $ paramater1 $ paramater2}

Write-Host 'READY>' -NoNewline -ForegroundColor Magenta;$inputx = Read-Host;$input = $inputx.Split()
if (Get-Command $cmdName -errorAction SilentlyContinue){ # Checks if function is available
    # Execute exact string
}

Что ожидается:

Скажем, мы вводим "Write-Host 'Hello World!' -NoNewLine" Команда для выполнения: Write-Host 'Hello World!' -NoNewLine

Что возвращается:

At line:1 char:44 + Invoke-Command -ScriptBlock {$functionName $paramater1 $paramater2} + ~~~~~~~~~~~ Unexpected token '$paramater1' in expression or statement. At line:1 char:56 + Invoke-Command -ScriptBlock {$functionName $paramater1 $paramater2} + ~~~~~~~~~~~ Unexpected token '$paramater2' in expression or statement. + CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException + FullyQualifiedErrorId : UnexpectedToken

1 Ответ

0 голосов
/ 08 января 2019

Решение: создать объединенную строку из разделенной строки и использовать команду $ Invoke-Expression $.

Это действительно очень сложное и ужасное программирование с моей стороны, которое можно легко упростить, но вот оригинальное исправление блока.

Write-Host 'READY>' -NoNewline -ForegroundColor Magenta;$inputx = Read-Host;$input = $inputx.Split()
if (Get-Command $cmdName -errorAction SilentlyContinue){
    if($input[0] -eq $null){
        write error 'Empty command'
    }else{
    if($input[1] -eq $null){
        $command = $input[0]
        Invoke-Expression $command
    }else{
    if($input[2] -eq $null){
        $command = $input[0]+" "+$input[1]
        Invoke-Expression $command
    }else{
    if($input[3] -eq $null){
        $command = $input[0]+" "+$input[1]+" "+$input[2]
        Invoke-Expression $command
    }else{
    if($input[4] -eq $null){
        $command = $input[0]+" "+$input[1]+" "+$input[2]+" "+$input[3]
        Invoke-Expression $command
    }else{
        $command = $input[0]+" "+$input[1]+" "+$input[2]+" "+$input[3]+" "+$input[4]
        Invoke-Expression $command
    if($input[5] -eq $null){
        $command = $input[0]+" "+$input[1]+" "+$input[2]+" "+$input[3]+" "+$input[4]+" "+$input[5]
        Invoke-Expression $command
    }else{
    if($input[6] -eq $null){
        $command = $input[0]+" "+$input[1]+" "+$input[2]+" "+$input[3]+" "+$input[4]+" "+$input[5]+" "+$input[6]
        Invoke-Expression $command
    }else{
    if($input[7] -eq $null){
        $command = $input[0]+" "+$input[1]+" "+$input[2]+" "+$input[3]+" "+$input[4]+" "+$input[5]+" "+$input[6]+" "+$input[7]
        Invoke-Expression $command
    }else{
    if($input[8] -eq $null){
        $command = $input[0]+" "+$input[1]+" "+$input[2]+" "+$input[3]+" "+$input[4]+" "+$input[5]+" "+$input[6]+" "+$input[7]+" "+$input[8]
        Invoke-Expression $command
    }else{
    if($input[9] -eq $null){
        $command = $input[0]+" "+$input[1]+" "+$input[2]+" "+$input[3]+" "+$input[4]+" "+$input[5]+" "+$input[6]+" "+$input[7]+" "+$input[8]+" "+$input[9]
        Invoke-Expression $command
    }else{
    if($input[10] -eq $null){
        $command = $input[0]+" "+$input[1]+" "+$input[2]+" "+$input[3]+" "+$input[4]+" "+$input[5]+" "+$input[6]+" "+$input[7]+" "+$input[8]+" "+$input[9]+" "+$input[10]
        Invoke-Expression $command
    }}}}}}}}}}}
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...