2014-05-15 35 views
1

我已經使用Invoke-Restmethod將Powershell存儲在PSCustomObject中的一些數據下載到名爲data的屬性中。在變量中使用Powershell noteproperty作爲文本字符串

我需要使用返回數據中的一個項目的值作爲另一個命令的變量。我已成功地選擇對象-expand我一路下跌到下面的輸出從獲取會員:

Name  MemberType Definition      
----  ---------- ----------      
Equals  Method  bool Equals(System.Object obj) 
GetHashCode Method  int GetHashCode()    
GetType  Method  type GetType()     
ToString Method  string ToString()    
id   NoteProperty System.Int32 id=999 

我需要做的就是抓住ID noteproperty的價值 - 999 - 並傳遞作爲的一部分一個字符串到一個新的變量,例如:

$newVar = "sometext" + 999 + "moretext" 

沒有數量的選擇字符串或超出字符串等幫助。腳本編寫不完全是我的強項,所以我不確定我甚至可以清楚說明我想要的內容 - 如果發生這種情況,我們表示歉意!

大加讚賞

回答

2

我不知道到底是什麼代碼看起來像,所以我創建從描述以下靜態近似任何援助:

$data = New-Object PSCustomObject 
$data | Add-Member -Type NoteProperty -Name Id -Value 999 
$restResponse = New-Object PSCustomObject 
$restResponse | Add-Member -Type NoteProperty -Name data -Value $data 

請澄清,如果這不是一個比賽。你可以得到的ID值如下

$restResponse.data.Id 

它分配給另一個變量

$newVar = "sometext" + $restResponse.data.Id + "moretext" 
$newVar 

如果你的REST響應是數據對象的集合,遍歷它們

$restResponse.data | Foreach-Object { "sometext" + $_.Id + "moretext" } 
+0

那做它,謝謝。我想我盯着這太久了! – ItsNotMyStrongPoint

相關問題