1

这似乎很基本,但无法在任何地方找到或弄清楚。

我有一个要导入的 .csv 文件,将具有不同 SID 的用户列更改为对应的用户名,然后导出回 .csv。我知道如何找到 SIDtoUser,但我不知道如何在 .csv 中进行替换。还有一些用户列是空白的或有“管理员”,我需要单独留下。

当前的

ComputerName,   TimeStamp,  Message,    User               
hp8440,         8/30/2012,  message1,   admin  
hp8440,         8/30/2012,  message2  
hp8440,         8/30/2012,  message3,   S-1-5-21...1105  
hp8440,         8/30/2012,  message4,   S-1-5-21...1255

想读

ComputerName,   TimeStamp,  Message,    User  
hp8440,         8/30/2012,  message1,   admin  
hp8440,         8/30/2012,  message2  
hp8440,         8/30/2012,  message3,   user1  
hp8440,         8/30/2012,  message4,   user2
4

2 回答 2

1
Import-Csv .\sid.csv | ForEach-Object{

    if($_.User -and $_.User -ne 'admin')
    {
        # User is not empty and not admin
        # perform translation and assign the results back to the User column
        # then return the current object back to the pipeline
        $_.User = (New-Object System.Security.Principal.SecurityIdentifier $_.User).Translate([System.Security.Principal.NTAccount]).Value
        $_
    }
    else
    {
        # Ignore empty User or admin 
        # and return the current object back to the pipeline
        $_
    }

} | Export-Csv .\users.csv
于 2012-08-30T15:26:33.320 回答
0

作为一个单行:

Import-Csv .\data.csv |%{ $_.User = ConvertSidToUserName $_.User; $_ } | Export-Csv .\updated.csv

有点可读性...

$data = Import-Csv .\data.csv

# replace the User Propery
$data |%{ $_.User = ConvertSidToUserName $_.User }

# export back out to csv
$data | Export-Csv .\updated.csv
于 2012-08-30T15:27:08.530 回答