2017-03-31 69 views
1

我不知道是否有使用使用PowerShell的選擇對象cmdlet如何使用PowerShell選擇對象

這裏的時候,做一個子的方式時使用的子是我想要做榜樣

$sapFile = "C:\MyFile*.XML" 

# Get the newest xml file 
$newestFile = Get-ChildItem -Path $sapFile | Sort CreationTime | Select -Last 1 

# get the contents of the xml file 
[XML]$sapContent = Get-Content -Path $newestFile.FullName 

$sapContent.DATA.CP | Select -Property CP_NO,MEN.Substring(0,4) 

但是這不起作用。

感謝

回答

3

您可以創建使用Select有使用哈希表中的計算值飛的屬性。在以下示例中,我選擇'CP_NO'屬性,並使用散列表創建'MENSubString'屬性。

Select -Property CP_NO,@{label='MENSubString';expression={$_.MEN.Substring(0,4)}} 

標籤可以縮短到只是 'L' 和表達可縮短至僅僅 'E',這將如下所示:

Select -Property CP_NO,@{l='MENSubString';e={$_.MEN.Substring(0,4)}} 
+0

謝謝,像魅力一樣工作 –

0

其他方法:

$sapContent.DATA.CP | foreach { 
     [pscustomobject]@{ 
     CP_NO=$_.CP_NO 
     PartOfMen=MEN.Substring(0,4)   
     } 
     }