将参数传递给powershell编码命令

时间:2017-01-08 18:16:55

标签: powershell encode scriptblock

我有一个脚本,它有很多行。 我可以轻松地将此脚本粘贴到scriptblock参数中,而无需对其进行编辑(例如,在脚本的引号前放置反斜杠)。然后我可以对脚本进行编码,以便它可以作为编码参数传递给powershell:

$parameter = 9
$myscript = {
$calc = 6 + $parameter
echo $calc
}

我希望能够在转换为base64的脚本中传递一个参数。像这样:

{{1}}

任何想法如何解决这个问题?我知道scriptblock可以包含参数,但是为了解析参数,需要解析整个脚本,而不仅仅是一个参数

2 个答案:

答案 0 :(得分:1)

如何将变量添加到脚本块的直接答案是:

$parameter = 9

$myscript = @'
$calc = 6 + {0}
echo $calc
'@ -f $parameter

$scriptblock = [scriptblock]::Create($myscript)

基本上将其构建为字符串,并使用[scriptblock]中的create方法进行转换。

但您可以跳过创建脚本块,因为之后您将直接将其转换回字符串。

答案 1 :(得分:0)

这是一篇旧帖子,但我发现 this 篇文章对我有用,所以我想与您亲爱的社区分享它:)

您可以在脚本块中使用带有强制参数的 param 块:

$myscript = {
    param
    (
        [Parameter(Mandatory)]
        [decimal]
        $First,

        [Parameter(Mandatory)]
        [decimal]
        $Second
    )
    [decimal]($First + $Second)
}

$bytes = [System.Text.Encoding]::Unicode.GetBytes($myscript)
$encodedCommand = [Convert]::ToBase64String($bytes)
$encodedCommand | Set-Content 'C:\temp\encodedCommand.txt' -Encoding UTF8

然后通过管道在两个 powershell.exe 调用之间传递参数:

powershell.exe -noprofile -command "3.3, 2.7" | powershell.exe -encodedcommand DQAKACAAIAAgACAAcABhAHIAYQBtAA0ACgAgACAAIAAgACgADQAKACAAIAAgACAAIAAgACAAIABbAFAAYQByAGEAbQBlAHQAZQByACgATQBhAG4AZABhAHQAbwByAHkAKQBdAA0ACgAgACAAIAAgACAAIAAgACAAWwBkAGUAYwBpAG0AYQBsAF0ADQAKACAAIAAgACAAIAAgACAAIAAkAEYAaQByAHMAdAAsAA0ACgANAAoAIAAgACAAIAAgACAAIAAgAFsAUABhAHIAYQBtAGUAdABlAHIAKABNAGEAbgBkAGEAdABvAHIAeQApAF0ADQAKACAAIAAgACAAIAAgACAAIABbAGQAZQBjAGkAbQBhAGwAXQANAAoAIAAgACAAIAAgACAAIAAgACQAUwBlAGMAbwBuAGQADQAKACAAIAAgACAAKQANAAoAIAAgACAAIABbAGQAZQBjAGkAbQBhAGwAXQAoACQARgBpAHIAcwB0ACAAKwAgACQAUwBlAGMAbwBuAGQAKQANAAoA

这是使用 Powershell 交互式输入模式,在整体输出中可见,因此请注意是否传递任何密码或机密:

cmdlet  at command pipeline position 1
Supply values for the following parameters:
First: 3.3
Second: 2.7
6.0

如果您曾经尝试使用列表(数组)参数并将值列表传递给编码命令,那么您需要记住最后一个数组元素必须是空字符串 - 这就是您如何欺骗交互式输入模式来设置列表参数。

$command = {
    param
    (
        [Parameter(Mandatory)]
        [string[]]
        $MyList
    )
    $MyList | ForEach-Object { Write-Host $_ }
}

$bytes = [System.Text.Encoding]::Unicode.GetBytes($command)
$encodedCommand = [Convert]::ToBase64String($bytes)
"powershell.exe -noprofile -command `"'test1', 'test2', 'test3', ''`" | powershell.exe -encodedcommand $encodedCommand" | Set-Content 'C:\temp\test.txt' -Encoding UTF8
PS C:\temp> powershell.exe -noprofile -command "'test1', 'test2', 'test3', ''" | powershell.exe -encodedcommand DQAKACAAIAAgACAAcABhAHIAYQBtAA0ACgAgACAAIAAgACgADQAKACAAIAAgACAAIAAgACAAIABbAFAAYQByAGEAbQBlAHQAZQByACgATQBhAG4AZABhAHQAbwByAHkAKQBdAA0ACgAgACAAIAAgACAAIAAgACAAWwBzAHQAcgBpAG4AZwBbAF0AXQANAAoAIAAgACAAIAAgACAAIAAgACQATQB5AEwAaQBzAHQADQAKACAAIAAgACAAKQANAAoAIAAgACAAIAAkAE0AeQBMAGkAcwB0ACAAfAAgAEYAbwByAEUAYQBjAGgALQBPAGIAagBlAGMAdAAgAHsAIABXAHIAaQB0AGUALQBIAG8AcwB0ACAAJABfACAAfQANAAoA


cmdlet  at command pipeline position 1
Supply values for the following parameters:
MyList[0]: test1
MyList[1]: test2
MyList[2]: test3
MyList[3]:
test1
test2
test3

我希望它会在未来对某人有所帮助。和平!