powershell 한글 출력 문제 (UTF8 without BOM)
글쓴이: woonuk / 작성시간: 수, 2017/07/26 - 10:35오전
시스템 모니터링 툴로 zabbix 3.0 을 사용하고 있습니다.
윈도우즈 서버 네트워크 사용량을 체크하기 위해 디바이스 장치명이 구분자로 사용됩니다.
최근 서버 모델이 동일한데도 불구하고 Public, Private 가 구분하기 힘들어서, 네트워크 네임으로 구분하기 위해 커스텀 스크립트를 작성하여 LLD(Low Level Discovery)로 등록하려고 하였습니다.
디바이스 장치명에 한글이 포함되어 있어서, EUC-KR 과 UTF-8 이슈가 있어서 다음처럼 처리하였습니다.
$adapter = Gwmi win32_networkadapter -Filter 'NetConnectionID != NULL' $RetStr = [string]::Empty $RetStr = "{`"data`":[" $row = 1 Foreach ($objItem in $adapter) { $RetStr = $RetStr + "{`"{#IFNAME}`":`"" + $objItem.Name + "`"," $RetStr = $RetStr + "`"{#IFALIAS}`":`"" + $objItem.NetConnectionID + "`"}" If ( $row -lt $adapter.Count ) { $RetStr = $RetStr + "," } Else { $RetStr = $RetStr + "]" } $row = $row + 1 } $RetStr = $RetStr + "}" $host.UI.RawUI.BufferSize = new-object System.Management.Automation.Host.Size(512,50); [Console]::OutputEncoding = [System.Text.Encoding]::UTF8 echo $RetStr
zabbix_agentd.conf에는 다음처럼 UserParameter를 등록하였습니다.
UserParameter=my.net.if.discovery,PowerShell.exe -ExecutionPolicy Bypass -File D:\_monitor\zabbix\scripts\network_discovery.ps1
zabbix_get -s 1.2.3.4 -k my.net.if.discovery 명령으로 값을 확인해 보면, UTF-8 형식으로 잘 보이지만,
zabbix에서 "value should be a json object"라는 에러가 나면서 아이템 추가가 안되고 있습니다.
확인해 보니 powershell 출력문에 BOM이 포함되어 있어서 에러가 발생한 것입니다.
지금은 powershell에서 출력하는 대신 UTF-8 without BOM 형식으로 파일로 저장하고, 해당 파일을 뿌리는 식으로
처리하였습니다.
UserParameter=my.net.if.discovery,PowerShell.exe -ExecutionPolicy Bypass -File D:\_monitor\zabbix\scripts\network_discovery.ps1 && type D:\_monitor\zabbix\scripts\myNetwork.txt
$adapter = Gwmi win32_networkadapter -Filter 'NetConnectionID != NULL' $RetStr = [string]::Empty $RetStr = "{`"data`":[" $row = 1 Foreach ($objItem in $adapter) { $RetStr = $RetStr + "{`"{#IFNAME}`":`"" + $objItem.Name + "`"," $RetStr = $RetStr + "`"{#IFALIAS}`":`"" + $objItem.NetConnectionID + "`"}" If ( $row -lt $adapter.Count ) { $RetStr = $RetStr + "," } Else { $RetStr = $RetStr + "]" } $row = $row + 1 } $RetStr = $RetStr + "}" $host.UI.RawUI.BufferSize = new-object System.Management.Automation.Host.Size(512,50); $invocation = (Get-Variable MyInvocation).Value $directorypath = Split-Path $invocation.MyCommand.Path $output = $directorypath + ".\myNetwork.txt" [System.IO.File]::WriteAllLines($output, $RetStr)
이렇게 임시파일을 거치지 않고, powershell 스크립트에서 바로 UTF-8 withou BOM 형식으로 출력할 수 없는지 궁금합니다.
Forums:
[해결] echo 대신 [Console]::WriteLine() 사용
출력문을 [Console]::WriteLine()으로 수정하면 BOM(Byte Order Mark) 없이 출력이 가능하네요.
댓글 달기