首页 > 解决方案 > 将字符串值传递给 Powershell 函数

问题描述

这可能是一个非常简单的问题,但我对 Powershell 完全陌生,还无法解决这个问题。

我想使用 Daniel BohannonInvoke-Obfuscation来混淆用于机器学习目的的大量 Powershell 脚本。混淆脚本文件的首选用法如下:

Invoke-Obfuscation -ScriptPath .\my_script.ps1 -Command 'String\2' -Quiet

但这不会像预期的那样混淆脚本,并且可能是该工具的问题。但我发现直接调用子函数是有效的。要混淆脚本文字,我可以执行以下操作(假设我站在工具的根文件夹中):

Import-Module ./Invoke-Obfuscation.ps1
Import-Module ./Invoke-Obfuscation.psd1
Import-Module ./Out-CompressedCommand.ps1
Import-Module ./Out-EncodedAsciiCommand.ps1
Import-Module ./Out-EncodedBXORCommand.ps1
Import-Module ./Out-EncodedBinaryCommand.ps1
Import-Module ./Out-EncodedHexCommand.ps1
Import-Module ./Out-EncodedOctalCommand.ps1
Import-Module ./Out-EncodedSpecialCharOnlyCommand.ps1
Import-Module ./Out-EncodedWhitespaceCommand.ps1
Import-Module ./Out-ObfuscatedAst.ps1
Import-Module ./Out-ObfuscatedStringCommand.ps1
Import-Module ./Out-ObfuscatedTokenCommand.ps1
Import-Module ./Out-PowerShellLauncher.ps1
Import-Module ./Out-SecureStringCommand.ps1

Out-ObfuscatedStringCommand {Write-Host 'Hello World!' -ForegroundColor Green; Write-Host 'Obfuscation Rocks!' -ForegroundColor Green} 1

它按预期打印出混淆的脚本。但是,我不想使用字符串文字,而是想从脚本中读取内容,而不是执行以下操作:

$ConsoleCommand = Get-Content -Path '.\my_script.ps1' -Raw
Out-ObfuscatedStringCommand $ConsoleCommand 1

但这会导致如下错误:

Get-ChildItem : Cannot find a provider with the name [...]

如何将文件的内容传递给在 Powershell 中正确接受字符串输入的函数?

标签: powershell

解决方案


推荐阅读