Powershell
-
[PowerShell] Publish-Script 명령어로 배포시 Failed to generate the compressed file for script 'Cannot index into a null array.' 오류 현상Shell & CMD/PowerShell 2023. 3. 25. 23:23
파워셸로 스크립트를 배포하는 과정에서 Failed to generate the compressed file for script 'Cannot index into a null array.' 가 발생했다. 원인은 배포할 스크립트를 Publish-Script가 배포할 스크립트를 nupkg 형식으로 만드는 과정에서 나타나는 오류인데, 스크립트를 잘못 작성해서 문제가 된게 아니라 OS가 영어가 아닌 다른 언어일 경우 나타나는 환경적인 이슈이다. 관련 내용은 Github issue 에서 확인 가능하다. https://github.com/PowerShell/PowerShellGetv2/issues/606#issuecomment-686660440 Failed to generate the compressed file f..
-
[PowerShell] $ErrorActionPreference 설정으로 ps1 스크립트 오류 발생 시 다음 코드 실행 막기Shell & CMD/PowerShell 2022. 12. 20. 22:38
파워셸 스크립트는 기본적으로 오류가 나도 왠만하면(?) 다음 코드 줄을 실행하게 된다. 아래 코드는 에러를 유발하는 코드와 다음 단계에서 "Work is done." 메시지를 출력하는 스크립트 예제이다. $err = 1 / 0 Write-Host "Work is done." 실행하면 runtimeerror가 발생하게 되는데, 문제는 다음 코드인 Write-Host "Work is done." 도 같이 실행된다는 점이다. 스크립트 동작에서 반드시 정상 선행되어야 하는 코드인 경우 이와 같은 동작은 문제가 발생할 수 있다. 이 경우 $ErrorActionPreference = "Stop" 값을 추가하면 된다. $ErrorActionPreference = "Stop" $err = 1 / 0 Write-Host..
-
[PowerShell 예제] ps1 에서 다른 ps1 파일 참조하기Shell & CMD/PowerShell 2022. 12. 20. 21:32
파워셀 스크립트(.ps1) 파일은 다른 스크립트 파일을 참조할 수 있다. 아래와 같이 각각 env.ps1 과 deploy.ps1 파일을 생성한 뒤 deploy.ps1을 실행해보자 env.ps1 파일 $dev = "is dev env"; $prod = "is prod env"; function Get-Environment([bool]$isDebug = $true) { if ($isDebug) { return $dev; } else { return $prod; } } env.ps1 파일 . .\env.ps1 # 파일의 경로 Get-Environment($true) | Write-Host 실행결과 > .\deploy.ps1 is dev env
-
[PowerShell Tips] .ps1 또는 함수실행시 정해진 파라메터만 받을 수 있도록 처리하는 ValidateSet 속성Shell & CMD/PowerShell 2022. 12. 19. 20:34
소개 ps1 파일을 통한 스크립트 실행 혹은 함수 실행시 특정 파라메터만 받아 처리할 수 있도록 셋을 정의하고 싶을 때가 존재한다. 그럴 땐 정의된 파라메터에 ValidateSet 을 붙이도록 하자 아래 예제는 스크립트 실행시 아래 동작 [CmdletBinding()] param ( [Parameter(Mandatory)] [ValidateNotNullOrEmpty()] [ValidateSet('Development','QA','Staging', 'Production')] [string]$Environment ) Write-Host $Environment 파라메터 $Environmnet 를 받아 작업을 처리하는 ps1 파일 실행시, 필수로 'Development','QA','Staging', 'Produ..
-
[PowerShell 예제] Start-ThreadJob을 이용한 동시 처리 스크립트 구현Shell & CMD/PowerShell 2022. 7. 30. 20:39
지난 시간에 where, foreach 를 이용하여 특정 폴더내에서 txt 파일을 EUC-KR로 읽어 UTF-8로 변환했었다. 주소 기반 데이터 정제작업에서 유용하긴 하지만 압축을 해제 한 후, 해당 폴더에 스크립트파일을 담은 뒤 실행해야 하는 선행 작업이 필요하기 때문에 자동화 작업이라 보긴 어려웠다. 그래서 스크립트를 아래와 같이 수정했다. # Parameter help description # Specifies a path to one or more locations. [CmdletBinding()] param ( [string[]] $zipfiles ) Write-Host "Convert Start :" ([DateTime]::Now).ToString("yyyy-MM-dd hh:mm:ss") $p..
-
[PowerShell] 파워셸에서 한글 파일명 깨지지 않게 압축 풀기Shell & CMD/PowerShell 2022. 7. 30. 17:17
파워셸에선 Compress-Archive 라는 압축 명령어와 Expand-Archive 라는 이름의 압축해제 명령어가 존재한다. 상당히 유용한 명령어지만 파일제목이 한글 인코딩(euc-kr)으로 된 경우, 명칭이 깨져나오는 불편함이 존재한다. 이를 해결하려면 아래와 같이 .NET 내장함수를 이용해야 한다. [System.IO.Compression.ZipFile]::ExtractToDirectory('202206_상세주소DB_전체분.zip', "out", [System.Text.Encoding]::GetEncoding("EUC-KR")) [Expand-Archive 이용시] [ExtractToDirectory 이용시]
-
[Powershell 예제] foreach, Where를 사용하여 파일 일괄 인코딩 변경Shell & CMD/PowerShell 2022. 7. 13. 09:08
아래소스는 주소기반산업지원서비스 (juso.go.kr) 에서 제공하는 도로명주소 데이터 텍스트 파일의 인코딩정보를 EUC-KR에서 UTF-8로 일괄 변환하는 스크립트이다. $directory = [System.IO.DirectoryInfo]::new(".") $files = $directory.GetFiles().Where{ $_.Name -ne "converter.ps1" } # 해당 스크립트파일인 converter.ps1는 인코딩작업에서 제외 # Get-ItemProperty $files foreach ($file in $files) { # Get-ItemProperty $file $outPath = Join-Path "converted" $file.Name #converted 폴더 실행위치 하위에 ..