【问题标题】:base64 encoded powershell "Cannot index into a null array." but runs from ISEbase64 编码的 powershell “无法索引到空数组。”但从 ISE 运行
【发布时间】:2017-12-07 20:18:37
【问题描述】:

我们正在努力在我们的环境中使用 RMS 保护文件。我们必须使用 PowerShell。

我什至不能使用 MDT 加入域的 WDS 服务器,因为它使用的 psxml 文件没有签名。

我必须将 PS 脚本作为单行命令或使用编码命令运行 [convert]::ToBase64String([Text.Encoding]::Unicode.GetBytes($code)) 符合我用$code = {} 包裹的脚本。

此脚本在从 PowerShell ISE 运行时有效。

$lines = Get-Content E:\folder\list.txt | Select-String -Pattern "Success Message" -AllMatches -Context 1,0 | % $_.Context.PreContext[0]
    foreach ( $line in $lines ) {
        $file = $line.ToString().TrimStart("chars. ")
        Protect-RMSFile -File $file -InPlace -DoNotPersistEncryptionKey All -TemplateID "template-ID" -OwnerEmail "admin@domain.com" | Out-File -FilePath E:\folder\logs\results.log -Append
        }

批处理脚本:

"e:\folder\command.exe -switches" > "E:\folder\list.txt" 

powershell.exe -EncodedCommand encodedBlob

输出:

Cannot index into a null array.
At line:3 char:1
+ $lines = Get-Content E:\folder\list.txt | Select-String      -Pattern "S ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : NullArray

我在另一个问题上看到了某种异常日志可能会有所帮助...

$Error.Exception | Where-Object -Property Message -Like "Cannot index into a null array." | Format-List -Force | Out-File -FilePath E:\folder\err.log

输出:

ErrorRecord                 : Cannot index into a null array.
StackTrace                  :    at CallSite.Target(Closure , CallSite , Object , Int32 )
                             at System.Dynamic.UpdateDelegates.UpdateAndExecute2[T0,T1,TRet](CallSite site, T0 arg0, T1 arg1)
                             at System.Management.Automation.Interpreter.DynamicInstruction`3.Run(InterpretedFrame frame)
                             at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
                             at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
WasThrownFromThrowStatement : False
Message                     : Cannot index into a null array.
Data                        : {System.Management.Automation.Interpreter.InterpretedFrameInfo}
InnerException              : 
TargetSite                  : System.Object CallSite.Target(System.Runtime.CompilerServices.Closure, System.Runtime.CompilerServices.CallSite, System.Object, 
                              Int32)
HelpLink                    : 
Source                      : Anonymously Hosted DynamicMethods Assembly
HResult                     : -2146233087

认为这就像 NTFS 权限一样简单,我取得了所有者并将此文件夹结构上的所有权限替换为管理员和我的完全控制权。错误没有变化。

有什么建议吗?我忽略了一些简单的事情吗?

【问题讨论】:

  • 如果是权限问题,您会在Get-Content 调用中遇到错误。你的逻辑不适合模式匹配,所以$Lines 最终会是$Null。

标签: powershell base64 rights-management


【解决方案1】:

您的 ForEach-Object 呼叫 (% $_) 没有任何意义,并导致了问题。

$Lines = Select-String -LiteralPath 'E:\folder\list.txt' -Pattern 'success\smessage' -AllMatches -Context 1,0

ForEach ($Line in $Lines.Context.PreContext)
{
    $File = $Line.TrimStart('chars. ')
    Protect-RMSFile -File $File -InPlace -DoNotPersistEncryptionKey 'All' -TemplateID 'template-ID' -OwnerEmail 'admin@domain.com' |
        Out-File -FilePath 'E:\folder\logs\results.log' -Append
}

单线式(适合编码):

SLS 'success\smessage' 'E:\folder\list.txt' -AllMatches -Context 1,0 |
  % { Protect-RMSFile -File $_.Context.PreContext.TrimStart('chars. ') -InPlace -DoNotPersistEncryptionKey 'All' -TemplateID 'template-ID' -OwnerEmail 'admin@domain.com' |
  Out-File 'E:\folder\logs\results.log' -Append

在本例中,我使用别名和位置绑定参数来缩短代码。

【讨论】:

  • 谢谢。你的重写工作正常。投票但太新了,无法计算。这就是我在没有完全理解其中所有内容的情况下复制一些脚本所得到的。 .Context.PreContext 在编码之前显然是有效的。你能告诉我怎么做吗?我正在努力变得更好。
  • @Mike 您想了解哪一部分?如果我知道你没有得到什么,我可以解释
  • @Mike .Context.PreContext 是捕获之前的行(由于-Context 1,0)。因为有多个捕获,它是一个数组,因此您需要循环它们以与每个捕获交互。我在我的答案中添加了一个替代编辑,你可以执行它应该是相同的。
猜你喜欢
  • 2018-09-14
  • 1970-01-01
  • 1970-01-01
  • 2022-01-09
  • 2022-01-23
  • 2017-04-17
  • 1970-01-01
  • 2017-07-01
  • 1970-01-01
相关资源
最近更新 更多