티스토리 뷰

programming/Scripts

powershell

Jacob_baek 2019. 8. 14. 17:33

실행예제

powershell 실행을 위해서는 set-executionpolicy 를 변경해야 한다.

default로 restricted로 되어 있어 실행이 불가하다. 아래명령을 수행하여 powershell script를 수행할 수 있는 환경으로 변경한다.

set-executionpolicy unrestricted

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.security/set-executionpolicy?view=powershell-6

 

 

write-host를 통한 메세지 출력

$testval = 1
write-host("testval : {0}" -f $testval)

https://devblogs.microsoft.com/scripting/string-formatting-in-windows-powershell/

 

 

파일이름을 추출하여 비교하고 java 라는 문구가 들어간 파일을 확인하여 동일한 파일명에 class확장자를 가진 파일을 복사하는 script example 

foreach ($fname in $difffiles) {
	echo $fname
    if ($fname.contains("java")) {
		$rname = $fname -replace "\.java", ".class"
        Copy-Item $rname -Destination $dstpath
	}
}

https://devblogs.microsoft.com/scripting/use-powershell-to-replace-text-in-strings/

https://devblogs.microsoft.com/scripting/powertip-finding-letters-in-strings-with-powershell/

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/copy-item?view=powershell-6

 

 

디렉토리 미존재시 디렉토리 생성하는 script example

if (-not (Test-Path -Path ".deploy") {
	New-Item -ItemType directory -Force -Path deploy
}

 

Lastindexof or LastindexofAny를 사용한 문자열 자르기

$ptr = $fname.LastindexofAny(".")

 

 

indexof를 활용한 특정 문구를 제외한 string 출력

 $ptr = $fname.LastIndexOfAny("WEB-INF")
 $rname = $fname.Substring($ptr)

참고용

간혹 vim과 같은 editor를 powershell에서 실행하고자 할 경우 아래와 같이 bash -c 명령을 사용하여 vim을 이용할 수 있다. 이를 확장하면 linux command를 bash -c 명령을 통해 사용할 수 있다.

https://medium.com/powershell-explained/text-editor-inside-powershell-22d2f5e748b8

exe로 만들기

https://blogs.technet.microsoft.com/japete/2017/09/11/wrap-your-powershell-scripts-as-executables/

참고사이트

https://ss64.com/ps/syntax-compare.html

 

 

'programming > Scripts' 카테고리의 다른 글

Bash shell variables looks like an array  (0) 2022.12.19
wsgi with apache  (0) 2016.07.20
parsing xml  (0) 2013.07.12
python for ssh  (0) 2012.07.20
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/03   »
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
글 보관함