2017-12-07 1 views
0

우리는 우리 환경에서 RMS를 사용하여 파일을 보호하기 위해 노력하고 있습니다. PowerShell을 사용해야합니다.base64로 인코딩 된 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 권한만큼 간단하다고 생각하여 소유자와 관리자 권한으로이 폴더 구조의 모든 권한을 대체했습니다. 오류가 변경되지 않았습니다.

팁이 있습니까? 나는 간단한 것을 간과하고 있는가?

+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 

는 I 별명을 이용했는데 코드를 짧게 할 파라미터 위치 적 바인딩.

+0

고맙습니다. 재 작성이 작동 중입니다. 투표했지만 너무 새롭기 때문에 투표하십시오. 그것이 내가 가진 모든 것을 완전히 이해하지 않고 스크립트를 복사 할 때 얻을 수있는 것입니다. .Context.PreContext는 인코딩 전에 분명히 기능했습니다. 어떻게 말해 줄 수 있니? 나는 나아 지려고 노력하고 있습니다. – Mike

+0

@Mike 어느 부분을 이해하려고합니까? – TheIncorrigible1

+0

@Mike'.Context.PreContext'는 (-Context 1,0' 때문에) 캡처 이전의 줄입니다. 캡처가 여러 개이기 때문에 배열이므로 배열을 반복하여 각 배열과 상호 작용해야합니다. 당신이 수행 할 수있는 대답에 대체 편집을 추가했습니다. – TheIncorrigible1

관련 문제