teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

1

サンプルコードが間違っていたので修正

2018/11/09 09:23

投稿

imihito
imihito

スコア2166

answer CHANGED
@@ -6,13 +6,23 @@
6
6
 
7
7
  例えば、以下のコードでは、`A`・`B`の二つのプロパティを持つPSObjectを作成し、`$nameOfProperty`の値によって参照するプロパティを変更しています。
8
8
  ```powershell
9
- $pso = New-Object -TypeName psobject -ArgumentList @{A=1;B=2}
9
+ [psobject]$pso = New-Object -TypeName psobject -Property @{A=1;B=2}
10
10
 
11
+ Write-Output -InputObject $pso
12
+
11
13
  [string]$nameOfProperty = 'a'
12
- $pso.$nameOfProperty # => 1
14
+ '{0} = {1}' -f $nameOfProperty , $pso.$nameOfProperty # => 1
13
15
 
14
16
  $nameOfProperty = 'B'
15
- $pso.$nameOfProperty # => 2
17
+ '{0} = {1}' -f $nameOfProperty , $pso.$nameOfProperty # => 2
18
+
19
+ <# 結果
20
+ A B
21
+ - -
22
+ 1 2
23
+ a = 1
24
+ B = 2
25
+ #>
16
26
  ```
17
27
 
18
28
  [別の質問](https://teratail.com/questions/156685)のコードのように`Select-Object`コマンドレットでプロパティを定義してるのであれば、