简体   繁体   中英

How filter powershell stdout

Ansible: 2.9 PowerShell: 5 OS: w2k16 Server

Hi all!

I search the method to filter the stdout of Format-Hex from PowerShell launched by ansible.

code:

win_shell: |
 Format-Hex C:\test.txt
register: recorder


debug:
  msg: {{ recorder }}

I see:

            0  1  2  3  4  5  6  7  8  9  A  B  C  D  E  F

00000000   42 4D 5E 00 00 00 00 00 00 00 36 00 00 00 28 00  BM^.......6...(. 
00000010   00 00 0A 00 00 00 01 00 00 00 01 00 20 00 00 00  ............ ... 
00000020   00 00 00 00 00 00 C4 0E 00 00 C4 0E 00 00 00 00  ......Ä...Ä..... 
00000030   00 00 00 00 00 00 B7 59 71 FF B7 59 71 FF B7 59  ......•Yq.•Yq.•Y 
00000040   71 FF B7 59 71 FF B7 59 71 FF B7 59 71 FF B7 59  q.•Yq.•Yq.•Yq.•Y 
00000050   71 FF B7 59 71 FF B7 59 71 FF B7 59 71 FF        q.•Yq.•Yq.•Yq.

But I only desire:

the all lines contained Hexa data:

42 4D 5E 00 00 00 00 00 00 00 36 00 00 00 28 00
...
...

Thanks again!

Try this, It works for me:

PS /~> cat .\test.txt
            0  1  2  3  4  5  6  7  8  9  A  B  C  D  E  F

00000000   42 4D 5E 00 00 00 00 00 00 00 36 00 00 00 28 00  BM^.......6...(. 
00000010   00 00 0A 00 00 00 01 00 00 00 01 00 20 00 00 00  ............ ... 
00000020   00 00 00 00 00 00 C4 0E 00 00 C4 0E 00 00 00 00  ......Ä...Ä..... 
00000030   00 00 00 00 00 00 B7 59 71 FF B7 59 71 FF B7 59  ......•Yq.•Yq.•Y 
00000040   71 FF B7 59 71 FF B7 59 71 FF B7 59 71 FF B7 59  q.•Yq.•Yq.•Yq.•Y 
00000050   71 FF B7 59 71 FF B7 59 71 FF B7 59 71 FF        q.•Yq.•Yq.•Yq.

PS /~> cat .\test.txt|select -Skip 1|%{($_ -split '\s{2,}')[1]}
42 4D 5E 00 00 00 00 00 00 00 36 00 00 00 28 00
00 00 0A 00 00 00 01 00 00 00 01 00 20 00 00 00
00 00 00 00 00 00 C4 0E 00 00 C4 0E 00 00 00 00
00 00 00 00 00 00 B7 59 71 FF B7 59 71 FF B7 59
71 FF B7 59 71 FF B7 59 71 FF B7 59 71 FF B7 59
71 FF B7 59 71 FF B7 59 71 FF B7 59 71 FF

PS /~>
  • Skip first Line (Header)
  • For each line, split where there is 2 or more spaces which generates a 3 item array and we get the item on position 1 which is the output you're interested on.
  • The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

     
    粤ICP备18138465号  © 2020-2024 STACKOOM.COM