tech.guitarrapc.cóm

Technical updates

PowerShellでPSCustomObjectに複数のObjectを追加する

しれっとこれまで出してたんですが、一応まとめておきます。

PSCustomObjectはPowerShell 3.0から追加された、 新たにPSObjectを定義するための簡潔に利用できる型です。 詳しくはWebでということで、表題のやり方の一例を。

PSCustomObjectを作ってみる

連想配列の頭に[PSCustomObject]と型名漬けてあげるだけです。例えば、このような連想配列があります。

@{
  ContextMenus = "Open Windows PowerShellx64 as Administrator"
  commands = "$PSHOME\powershell.exe -NoExit -NoProfile -Command ""Set-Location '%V'"""
  versions = "PowerShellx64"
}
Name                           Value
----                           -----
commands                       C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -NoExit -NoProfile -Command "Set-Location '%V'"
ContextMenus                   Open Windows PowerShellx64 as Administrator
versions                       PowerShellx64

頭に[PSCustomObject]とつけるだけで新たなPSObjectが定義できます。

[PSCustomObject]@{
  ContextMenus = "Open Windows PowerShellx64 as Administrator"
  commands = "$PSHOME\powershell.exe -NoExit -NoProfile -Command ""Set-Location '%V'"""
  versions = "PowerShellx64"
}
ContextMenus                                                   commands                                                       versions
------------                                                   --------                                                       --------
Open Windows PowerShellx86 as Administrator                    c:\Windows\syswow64\powershell.exe -NoExit -NoProfile -Comm... PowerShellx86

PSCustomObjectに複数のオブジェクトを設定する

+は使えません。同じセッション内で当て込んであげればいいのです。例えば、もとの値 (例えば配列)をForeach-Objectで渡すのもいいでしょう。 このような2次元配列を考えてみます。

@(
  @(
    'Open Windows PowerShellx86 as Administrator',
    "c:\Windows\syswow64\powershell.exe -NoExit -NoProfile -Command ""Set-Location '%V'""",
    "PowerShellx86"
  ),
  @(
    'Open Windows PowerShellx64 as Administrator',
    "$PSHOME\powershell.exe -NoExit -NoProfile -Command ""Set-Location '%V'""",
    "PowerShellx64"
  )
)

それぞれのindexに合わせ、て次のように当てたいケースを考えます。

[PSCustomObject]@{
  ContextMenus = [0]
  commands = [1]
  versions = [2]
}

先の例と同様にForeach-Objectで渡してあげるだけです。

@(
  @(
    'Open Windows PowerShellx86 as Administrator',
    "c:\Windows\syswow64\powershell.exe -NoExit -NoProfile -Command ""Set-Location '%V'""",
    "PowerShellx86"
  ),
  @(
    'Open Windows PowerShellx64 as Administrator',
    "$PSHOME\powershell.exe -NoExit -NoProfile -Command ""Set-Location '%V'""",
    "PowerShellx64"
  )
) | %{
  [PSCustomObject]@{
    ContextMenus = $_[0]
    commands = $_[1]
    versions = $_[2]
  }
}

ContextMenus                                                   commands                                                       versions
------------                                                   --------                                                       --------
Open Windows PowerShellx86 as Administrator                    c:\Windows\syswow64\powershell.exe -NoExit -NoProfile -Comm... PowerShellx86
Open Windows PowerShellx64 as Administrator                    C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -... PowerShellx64

もちろん配列でなくてもいいので、自在にコマンドレット結果をパイプで受けて元データをいじったPSObjectを作れます。 Select-Object @{Name="";Expression={}}より自前オブジェクトを簡単に作れます。