更改哈希值表中列之间的列标题

时间:2016-01-25 18:19:36

标签: powershell powershell-v3.0

我有一个表变量,可以像csv文件一样对待......

"SamAccountName","lastLogonTimestamp","AccountExpires","givenname","sn","distinguishedName","employeeNumber","employeeID","Description","extensionattribute8","userAccountControl"
"value1","value1","value1","value1","value1","value1","value1","value1","value1","value1","value1"
"value2","value2","value2","value2","value2","value2","value2","value2","value2","value2","value2"
"value3","value3","value3","value3","value3","value3","value3","value3","value3","value3","value3"

我想要做的是将两个标题名称givenname更改为FirstName,将sn更改为LastName

注意:我还想更改lastLogonTimestampAccountExpires的值,但我已经有了执行此操作的工作代码。此代码如下......

$listOfBadDateValues = '9223372036854775807', '9223372036854770000', '0'
$maxDateValue = '12/31/1600 5:00 PM'

$tableFixed = $table | % { 
    if ($_.lastLogonTimestamp) {
        $_.lastLogonTimestamp = ([datetime]::FromFileTime($_.lastLogonTimestamp)).ToString('g')
    }; if (($_.AccountExpires) -and ($listOfBadDateValues -contains $_.AccountExpires)) {
        $_.AccountExpires = $null
    } else {
        if (([datetime]::FromFileTime($_.AccountExpires)).ToString('g') -eq $maxDateValue) {
            $_.AccountExpires = $null
        } Else {
            $_.AccountExpires = ([datetime]::FromFileTime($_.AccountExpires)).ToString('g')
        }
};$_}

如何编写代码,以便将两个标题名称更改为FirstName和LastName?

1 个答案:

答案 0 :(得分:0)

您可以简单地获取对象并将其传递给select语句,以“别名”属性名称。

$yourObject | Select-Object -Property @{N='MyNewName1';E={$_.ExistingName1}}, @{N='MyNewName2';E={$_.ExistingName2}}