3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

ローカル作成したShellを実行したら文字化け問題

3
Posted at

はじめに

Azure Automation Runbook で PowerShell スクリプトを作成・実行した際、以下のようなエラーに遭遇したことはありませんか?

コードに文法的な誤りはないはずなのに、なぜかパースエラーになる…

発生場所 ps1:73 文字:9
+     try {
+         ~
ステートメント ブロックまたは型定義に終わりの '}' が存在しません。
発生場所 C:\Users\xxxxx\Desktop\project\xxxxx\xxxxxx\runbooks\shells\xxxxxx.ps1:87 文字:44
+             throw "譛溷セ・☆繧九せ繝・・繧ソ繧ケ繧ウ繝シ繝・200)縺ィ逡ー縺ェ繧翫∪縺吶ょ叙蠕怜、: $stat
 ...
+                                            ~
Try ステートメントに Catch ブロックまたは Finally ブロックがありません。

原因はファイルエンコードにあります。

本記事では、Runbook作成でハマりやすいエンコード問題の解説と設定方法についてご紹介します。

ハマりポイント:UTF-8 BOM問題

日本語文字列を含むスクリプトをVS Codeで UTF-8 として保存すると、PowerShell 5.1 がファイルを Shift-JIS として読み込もうとします。

その結果、日本語文字列が文字化けし、PowerShellが 構文として解釈できず パースエラーが発生します。

# 本来のコード
throw "期待するステータスコード(200)と異なります"

# PowerShell 5.1がShift-JISとして読んだ結果(文字化け)
throw "譛溷セ・☆繧九せ繝・・繧ソ...

解決方法

VS Code での変換手順:

  1. 右下のエンコード表示(UTF-8)をクリック
    image.png

  2. Save with Encoding を選択
    image.png

  3. UTF-8 with BOM を選択して保存
    image.png

注意事項

本ブログに掲載している内容は、個人の見解であり、所属組織の立場や戦略・意見を代表するものではありません。
あくまでエンジニアとしての経験や考えを発信していますので、ご了承ください。

3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?