2017-12-07 9 views
0

私たちの環境では、RMSを使ってファイルを保護しています。 PowerShellを使用する必要があります。base64 encoded powershell "null配列にインデックスできません。" ISEから実行します

使用しているpsxmlファイルが署名されていないため、MDTをドメインに参加させたWDSサーバーを使用することさえできません。

私は、単一の行がコマンドや$code = {}に包まれた私のスクリプトで [convert]::ToBase64String([Text.Encoding]::Unicode.GetBytes($code)) ラインを使用してエンコードされたコマンドとしてとしてPSスクリプトを実行する必要があります。

このスクリプトは、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 "[email protected]" | 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のアクセス許可と同じくらい簡単だと思って、私は所有者を取って、このフォルダ構造のすべてのアクセス許可をadminと私のフルコントロールに置き換えました。エラーに変更はありません。

ヒント?私は簡単なものを見落としていますか?

+0

権限の問題だった場合は、 'Get-Content'呼び出しでエラーが発生しました。あなたのロジックはパターンマッチングに悪いので、 '$ Lines'は' $ Null'を終了します。 – TheIncorrigible1

答えて

0

お客様の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 '[email protected]' | 
     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 '[email protected]' | 
    Out-File 'E:\folder\logs\results.log' -Append 

この例では、私はコードを短くするエイリアスと位置が結合したパラメータを使用してきました。

+0

ありがとうございます。あなたの書き換えは機能しています。議決されましたが、それは数え切れないほど新しいものです。それは私がそれらの中のすべてを完全に理解することなくスクリプトのコピーを得るために得るものです。 .Context.PreContextは、明らかにエンコード前に機能していました。どうやって教えてくれますか?私はより良くしようとしています。 – Mike

+0

@Mikeあなたはどちらの部分を理解しようとしていますか? – TheIncorrigible1

+0

@Mike '.Context.PreContext'はキャプチャ前の行です(' -Context 1,0'のため)。複数のキャプチャが存在するため、配列であるため、それぞれをキャプチャするためにループする必要があります。私はあなたが同じでなければならない実行することができる私の答えに別の編集を追加しました。 – TheIncorrigible1

関連する問題