使用powershell select-object时如何使用substring

时间:2017-03-31 20:23:33

标签: xml powershell substring

我想知道在使用powershell的select-object cmdlet时是否有办法使用子字符串

以下是我正在尝试做的一个例子

$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)

然而,这不起作用。

由于

2 个答案:

答案 0 :(得分:4)

您可以使用具有计算值的Select动态创建属性,方法是使用哈希表。在下面的示例中,我选择'CP_NO'属性,并使用哈希表创建'MENSubString'属性。

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

标签可以缩短为'l',Expression可以缩短为'e',如下所示:

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

答案 1 :(得分:0)

其他方法:

has_many :asset_items dependent: :destroy