티스토리 뷰
[PowerShell] 문자열 포함 확인
How can you check if a PowerShell string contains a character or substring?
You might be tempted to try this:
PS> $s = ‘abcdefghijk’
PS> $s -contains ‘f’
False
But –contains is for working with the contents of arrays. So you could do this:
PS> ($s.ToCharArray()) -contains ‘f’
True
You’re implicitly converting the string ‘f’ to [char] to make the comparison. Your comparison is actually this
PS> ($s.ToCharArray()) -contains [char]’f’
True
That’s fine for a single character but if you want to test a substring
PS> $s -contains ‘def’
False
PS> ($s.ToCharArray()) -contains ‘def’
False
That approach won’t work.
You need to use the Indexof method
PS> $s.Indexof(‘f’)
5
PS> $s.Indexof(‘def’)
3
The value returned is the position of the FIRST character in the substring.
You can also test an an array of characters
PS> $a = ‘g’,’j’,’a’
PS> $s.IndexOfAny($a)
0
Again you get the FIRST character in this case the ‘a’
Remember PowerShell is .NET based so the first index is 0
Lets get some repetition into our target
PS> $s = $s * 3
PS> $s
abcdefghijkabcdefghijkabcdefghijk
You also have the option of picking the LAST occurrence of the substring
PS> $s.LastIndexOf(‘f’)
27
PS> $s.LastIndexOfAny($a)
31
This last one is the last ‘j’ in the string – its the last occurrence of any of the characters you wanted to match.
If there isn’t a match you get –1 returned
PS> $s.IndexOf(‘z’)
-1
PS> $s.LastIndexOf(‘z’)
-1
출처 : https://richardspowershellblog.wordpress.com/2018/08/31/powershell-string-contains/
- Total
- Today
- Yesterday
- 조던1 사틴 블랙토
- 이지 700 모브
- 웹쉘 해결
- 이지 부스트 700
- 웹쉘 예방
- 나이키 켄드릭라마
- linux bridge 설정
- 파워쉘 문자열 포함 조건
- 조던1 사틴
- selinux 설명
- 리눅스 모니터링
- 조던1 사틴 블랙토 개봉기
- 리니지m 격수 팁
- 이지 350 지브라
- 피파온라인4
- end 응모
- 파워쉘 문자열 포함
- 이지 부스트 700 모브
- Java
- troijan
- selinux 사용법
- 웹쉘 탐지
- selinux 정책설정
- CloudStack
- 파워쉘 문자열
- 배트멍 할인
- 케니4
- 리눅스 hostname 변경
- 나이키 코르테즈
- 매치스패션 할인
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |