我有一个脚本,里面有很多行。我可以轻松地将此脚本粘贴到脚本块参数中,而无需对其进行编辑(例如,在脚本中的引号前面放置反斜杠)。然后,我可以对脚本进行编码,以便将其作为编码参数传递给powershell:
$myscript = {
#paste of simplified script
$calc = 6 + 9
echo $calc
}
# Convert script to a string
$command = $carvingScript.ToString()
# Convert string to base64 encoded command
$bytes = [System.Text.Encoding]::Unicode.GetBytes( $command )
$encodedCommand = [Convert]::ToBase64String( $bytes )
我希望能够在脚本中传递一个转换 base64 的参数。喜欢这个:
$parameter = 9
$myscript = {
$calc = 6 + $parameter
echo $calc
}
有什么想法可以解决这个问题吗?我知道脚本块可以包含参数,但是为了解析参数,需要解析整个脚本,而不仅仅是一个参数
如何向脚本块添加变量的直接答案是:
$parameter = 9
$myscript = @'
$calc = 6 + {0}
echo $calc
'@ -f $parameter
$scriptblock = [scriptblock]::Create($myscript)
基本上将其构建为字符串并使用 create 方法从 [scriptblock]
进行转换。
但是您可以跳过创建脚本块,因为之后您将直接将其转换回字符串。
这是一个旧帖子,但我发现这篇文章对我有用,所以我想与你分享亲爱的社区:)
您可以在脚本块中使用带有强制参数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:tempencodedCommand.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
如果您尝试使用 list (array) 参数并将值列表传递给编码命令,那么您需要记住最后一个数组元素必须是空字符串 - 这就是您欺骗交互式输入模式设置列表参数的方式。
您还需要记住不要将列表参数标记为ValueFromPipeline
否则,它将无法正确使用值。
$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:temptest.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
我希望它能在未来帮助某人。和平!