Powershell 修改 CSV

Powershell modify CSV

我在这里阅读了很多关于此的问题,但我没有找到我搜索的内容...

我开始使用 powershell 编写脚本(仅供参考:p)

我想修改一个 CSV 文件,该文件是从数据库中导出的,其中包含“FirstName,LastName,OtherMail,IDAurion,Department”等信息。我必须向 HEADER 添加一些标题,并且我必须获取 CSV 中的信息以连接一些信息并将它们放入另一个 CSV 文件中。

我不知道我解释得好不好,我把我的脚本给你:


    $OriginalCSV = "$PSScriptRoot\original.csv"

function Remove-StringLatinCharacters
{
    PARAM ([string]$String)
    [Text.Encoding]::ASCII.GetString([Text.Encoding]::GetEncoding("Cyrillic").GetBytes($String))
}

Import-Csv -path $OriginalCSV -Encoding Default | ForEach-Object { 

    $OtherMail = $_.OtherMail
    $IDAurion = $_.IDAurion
    $Department = $_.Department

    $FirstnameCSV = $_.FirstName
    $FirstName = $FirstnameCSV -replace '(^\s+|\s+$)',''
    $FirstnameNoLatin = Remove-StringLatinCharacters $FirstName
    $FirstnameNoLatinNoSpace = $FirstnameNoLatin -replace '\s','-'
    $FirstnameFirstLetterUpper = $Firstname.Substring(0,1).ToUpper()

    $LastNameCSV = $_.LastName
    $LastName = $LastnameCSV -replace '(^\s+|\s+$)',''
    $LastnameNoLatin = Remove-StringLatinCharacters $LastName
    $LastnameNoLatinNoSpace = $LastnameNoLatin -replace '\s',''
    $LastnameFirstLetterUpper = $Lastname.Substring(0,1).ToUpper()

    $UserPrincipalNameCSV = $FirstnameCSV + "." + $LastNameCSV + "@campus.ocellia.fr"
    $UserPrincipalNameConcatene = $FirstnameNoLatinNoSpace + "." + $LastNameNoLatinNoSpace + "@email.fr"
    $UserPrincipalName = $UserPrincipalNameConcatene.ToLower()

    $MailNickName = $FirstnameNoLatinNoSpace.substring(0,1).toupper() + $($FirstnameNoLatin.substring(1).tolower() -replace '\s','') + $LastnameNoLatinNoSpace.toupper()

    $Password = $FirstnameFirstLetterUpper + $LastnameFirstLetterUpper + $IDAurion + "$"
    
    $Number = 0

    Get-Content $OriginalCSV |
    ForEach-Object{ 

        If($Number -eq 0){
            $_ + ",MailNickName" + ",Password" + ",UserPrincipalName"
            $Number = 1
        } 
        Else {
            $_ + "," + $MailNickName + "," + $Password + "," + $UserPrincipalName
        }
    } | Out-File $PSScriptRoot\Modified-CSV.csv

}

这适用于 1 行,但不适用于多行 ...

编辑:

这是输入 CSV 的示例以及我希望得到的输出 CSV 的方式:

Input csv :

Firstname,Lastname,OtherMail,ID,Department
Pierre,DUPONT,pierre.dupont@mail.com,123456,Paris
Marie,CHANTAL,marie.chantal@mail.com,456789,Marseille

Output csv:
Firstname,Lastname,OtherMail,ID,Department,MailNickName,Password,UserPrincipalName
Pierre,DUPONT,pierre.dupont@mail.com,123456,Paris,PierreDupont,PD123456,p.dupont@entreprise.com
Marie,CHANTAL,marie.chantal@mail.com,456789,Marseille,MarieCHANTAL,MC456789,m.chantal@entreprise.com

我猜这就是你想要做的,但因为我们没有 minimal reproducible example 所以很难说。我也尝试改进你的代码,有很多冗余。

$OriginalCSV = "$PSScriptRoot\original.csv"
$ModifiedCSV = "$PSScriptRoot\modified.csv"
$txtInfo = (Get-Culture).TextInfo

function Remove-StringLatinCharacters
{
    PARAM ([string]$String)
    [Text.Encoding]::ASCII.GetString([Text.Encoding]::GetEncoding("Cyrillic").GetBytes($String))
}

(Import-Csv -Path $OriginalCSV -Encoding Default | ForEach-Object { 

    $OtherMail = $_.OtherMail.Trim()
    $IDAurion = $_.IDAurion.Trim()
    $Department = $_.Department.Trim()
    $FirstnameCSV = $_.FirstName.Trim()
    $LastNameCSV = $_.LastName.Trim()

    $firstName = (Remove-StringLatinCharacters $FirstName) -replace '\s','-'
    $lastName = (Remove-StringLatinCharacters $LastName) -replace '\s'
    
    $UserPrincipalName = "$FirstnameCSV.$LastNameCSV@campus.ocellia.fr"
    $UserPrincipalNameConcat = "$firstName.$lastName@email.fr".ToLower()

    $MailNickName = $txtInfo.ToTitleCase($firstName) + $lastName.ToUpper()
    $Password = "{0}{1}$IDAurion$" -f $firstName.ToUpper()[0],$lastName.ToUpper()[0]

    [pscustomobject]@{
        FirstNameCSV = $FirstnameCSV
        LastNameCSV = $LastNameCSV
        OtherMail = $OtherMail
        IDAurion = $IDAurion
        Department = $Department
        MailNickName = $MailNickName
        Password = $Password
        UserPrincipalNameCAMPUS = $UserPrincipalName
        UserPrincipalNameEMAILFR = $UserPrincipalNameConcat
    }

} | ConvertTo-Csv -NoTypeInformation) -replace '"' |
Out-File $ModifiedCSV