Alias a Property Name In Powershell 3

后端 未结 3 636
北恋
北恋 2021-01-03 22:16

Using the example below:

Get-Service | ConvertTo-HTML -Property Name, Status > C:\\services.htm

I was wondering if it is possible to ali

相关标签:
3条回答
  • 2021-01-03 22:59

    You could do an intermediate step of creating objects with the property names you want using the new-object cmdlet.

    Get-Service | foreach{ new-object PSObject -property @{newname=($_.Name); newstatus=($_.Status)}} | ConvertTo-Html > .\services.htm
    
    0 讨论(0)
  • 2021-01-03 23:03

    How about using select-object?

    get-service | select-object -property @{N='MyNewStatus';E={$_.Status}}, @{N='MyNewName';E={$_.Name}} | ConvertTo-HTML > C:\services.htm
    
    0 讨论(0)
  • 2021-01-03 23:11

    The way to alias a property name is to add an AliasPropery to the object.

    Get-Service | 
    foreach {
    $_ | Add-Member -MemberType AliasProperty -Name MYNEWSTATUSNAME -Value Status -PassThru
    } |
    Select Name,MYNEWSTATUSNAME
    
    0 讨论(0)
提交回复
热议问题