0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

PowerShellのif文は$?がTrueでもelseになる

Last updated at Posted at 2021-03-13

Azure PowerShellをインストールしようとドキュメントを見ていたところ、以下のようにモジュールをインストールせよと書いてあった。

if (Get-Module -Name AzureRM -ListAvailable) {
    Write-Warning -Message ('Az module not installed. Having both the AzureRM and ' +
      'Az modules installed at the same time is not supported.')
} else {
    Install-Module -Name Az -AllowClobber -Scope CurrentUser
}

PowerShell初心者なため、個々のコマンドレットを打ちながら動作を確認しようと最初のGet-Module -Name AzureRM -ListAvialbleを直接PowerShellで実行する。

PS C:\> Get-Module -Name AzureRM -ListAvailable
PS C:\> $?
True

私の環境にはAzureRMモジュールはないのでFalseになるかと思ったがTrueになった。
コマンドレット自体は正常終了しているのでTrueになるということか。(Unixのシェルの感覚だとFalseだが)

次にお試しにif文を書いたところelseになった。$?がTrueにも関わらず!

PS C:\> if (Get-Module -Name AzureRM -ListAvailable) { echo "true" } else { echo "false" }
false

おそらくコマンドレットの出力が空なのでfalseになるのだろう。
PowerShellではif文で終了コードは見ないのだろうか?
よくわからない。。。

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?