替换Powershell中Select语句的一部分



我正在使用Powershell从DHCP服务器获取一些信息。我正在使用这个命令来获取我需要的信息。

Get-DhcpServerv4Lease -ScopeId 10.132.56.0 | select IPAddress,ClientId,HostName | sort hostname | format-table

结果如我所料:

| IPAddress | ClientId | HostName ||---------------|-------------------|-----------| | 10.132.56.121 |10.132.56.101 | 40-83-de-2b-64-fb || | 10.132.56.76 c-c9-5e-6d-f2-30 | 4 |(招牌)|

删除"-"from the ClientId.

我试过-replace "*","我试着

Get-DhcpServerv4Lease -ScopeId 10.132.56.0 | select IPAddress,$_.ClientId -replace "-","" ,HostName | sort hostname | format-table
*Select-Object : A parameter cannot be found that matches parameter name 'replace'.
At line:1 char:75
+ Get-DhcpServerv4Lease -ScopeId 10.132.56.0 | select IPAddress,$_.ClientId -repla ...
+                                                                           ~~~~~~
+ CategoryInfo          : InvalidArgument: (:) [Select-Object], ParameterBindingException
+ FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.SelectObjectCommand$_.ChildID.replace("-","")*

但是我得到不同的错误。

我希望我可以在一行中完成,而不需要存储数组的变量。

帮忙吗?也许这是不可能的,但我怀疑。

您需要将-replace操作包含在属性表达式:

... Select IPAddress,{$_.ClientId -replace '-'},...

如果希望结果属性保留名称ClientId,请将属性表达式包装在哈希表中(有时称为计算属性):

... Select IPAddress,@{Name='ClientId';Expression={$_.ClientId -replace '-'}},...

最新更新