powershell 한글 출력 문제 (UTF8 without BOM)

woonuk의 이미지

시스템 모니터링 툴로 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 형식으로 출력할 수 없는지 궁금합니다.

woonuk의 이미지

출력문을 [Console]::WriteLine()으로 수정하면 BOM(Byte Order Mark) 없이 출력이 가능하네요.

$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
[Console]::WriteLine($RetStr)

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.