如何使用 Powershell 只读取 csv 文件中的一列
How to read only one column from csv file using Powershell
我正在处理 powershell 脚本,我试图从我的 csv 文件中只读取“用户”列,然后将其转换为他们的 Microsoft AzureAD 显示名称,但不确定如何操作,所以我会如果我能得到任何帮助或建议,我将不胜感激。
我的 csv 文件是这样的
C:\AuditLogSearch$($CurDate) Final Audit-Log-Records.csv
User Date &Time Activity
-------------------------------------
abc@gmail.com 11/22/2021 play soccer
kei@gmail.com 10/22/2021 play football
def@gmail.com 11/22/2021 play soccer
def@gmail.com 09/22/2021 play Baseball
xyz@gmail.com 08/22/2021 play soccer
klm@gmail.com 19/22/2021 play football
function Connect-AzureActiveDirectory {
$pso = New-PSSessionOption -SkipCACheck:$true -SkipCNCheck:$true -SkipRevocationCheck:$true -OperationTimeout 180000 -ProxyAccessType AutoDetect
Connect-AzureAD -Credential $credential -ConnectionUri https://outlook.office365.com/PowerShell-LiveID -PSSessionOption $pso
$host.ui.RawUI.WindowTitle = "Exchange Online - NAM Production Beta MFA"
Write-Host "Connected to Azure AD"
}
function Convert-UserColumn {
$ImportFile = "C:\AuditLogSearch$($CurDate) Final Audit-Log-Records.csv"
$ExportFile = "C:\AuditLogSearch\FinalFile.csv"
// I'm struggling with the logic here
$list = @() foreach ($u in $ImportFile.User) {
$list += Get-AzureDisplayName -mail $u
}
Get-AzureADUser -ObjectId
| Export-Csv $ExportFile -NoTypeInformation
}
尝试制作最终导出的 csv 文件应该如下所示
C:\AuditLogSearch\FinalFile.csv
User Date &Time Activity
-------------------------------------
Jonathan Sparkle 11/22/2021 play soccer
Randy Mod 10/22/2021 play football
Hanah P 11/22/2021 play soccer
Hanah P 09/22/2021 play Baseball
Cristiano Ronaldo 08/22/2021 play soccer
Leo Messi 19/22/2021 play football
您可以只更新导入的 CSV User
属性 上的值,无需创建 $list = @()
来保存结果。
假设 $ImportFile.User
包含有效的 UserPrincipalNames
Azure AD 用户,您遇到的问题将如下所示(绝对不需要函数):
$ImportFile = Import-Csv "C:\AuditLogSearch$($CurDate) Final Audit-Log-Records.csv"
$ExportFile = "C:\AuditLogSearch\FinalFile.csv"
foreach ($u in $ImportFile) {
$u.User = (Get-AzureADUser -ObjectId $u.User).DisplayName
}
$ImportFile | Export-Csv $ExportFile -NoTypeInformation
我正在尝试更改 Activity 列 value/text。例如,如果 text = "play soccer" 然后将其更改为 "play futball" 或者如果 text = "play football" 然后将其更改为 "play rugby" 等等..
这需要 if
个条件或 switch
。即:
# Using this as an Example
$csv = @'
User,Date & Time,Activity
abc@gmail.com,11/22/2021,play soccer
kei@gmail.com,10/22/2021,play football
def@gmail.com,11/22/2021,play soccer
def@gmail.com,09/22/2021,play Baseball
xyz@gmail.com,08/22/2021,play soccer
klm@gmail.com,19/22/2021,play football
'@ | ConvertFrom-Csv
foreach($line in $csv)
{
$line.Activity = switch($line.Activity)
{
'play soccer' { 'play futball'; break }
'play football' { 'play rugby'; break }
Default { $_ } # If no matches, leave it as is
}
}
$csv | Format-Table
User Date & Time Activity
---- ----------- --------
abc@gmail.com 11/22/2021 play futball
kei@gmail.com 10/22/2021 play rugby
def@gmail.com 11/22/2021 play futball
def@gmail.com 09/22/2021 play Baseball
xyz@gmail.com 08/22/2021 play futball
klm@gmail.com 19/22/2021 play rugby
我正在处理 powershell 脚本,我试图从我的 csv 文件中只读取“用户”列,然后将其转换为他们的 Microsoft AzureAD 显示名称,但不确定如何操作,所以我会如果我能得到任何帮助或建议,我将不胜感激。
我的 csv 文件是这样的
C:\AuditLogSearch$($CurDate) Final Audit-Log-Records.csv
User Date &Time Activity
-------------------------------------
abc@gmail.com 11/22/2021 play soccer
kei@gmail.com 10/22/2021 play football
def@gmail.com 11/22/2021 play soccer
def@gmail.com 09/22/2021 play Baseball
xyz@gmail.com 08/22/2021 play soccer
klm@gmail.com 19/22/2021 play football
function Connect-AzureActiveDirectory {
$pso = New-PSSessionOption -SkipCACheck:$true -SkipCNCheck:$true -SkipRevocationCheck:$true -OperationTimeout 180000 -ProxyAccessType AutoDetect
Connect-AzureAD -Credential $credential -ConnectionUri https://outlook.office365.com/PowerShell-LiveID -PSSessionOption $pso
$host.ui.RawUI.WindowTitle = "Exchange Online - NAM Production Beta MFA"
Write-Host "Connected to Azure AD"
}
function Convert-UserColumn {
$ImportFile = "C:\AuditLogSearch$($CurDate) Final Audit-Log-Records.csv"
$ExportFile = "C:\AuditLogSearch\FinalFile.csv"
// I'm struggling with the logic here
$list = @() foreach ($u in $ImportFile.User) {
$list += Get-AzureDisplayName -mail $u
}
Get-AzureADUser -ObjectId
| Export-Csv $ExportFile -NoTypeInformation
}
尝试制作最终导出的 csv 文件应该如下所示 C:\AuditLogSearch\FinalFile.csv
User Date &Time Activity
-------------------------------------
Jonathan Sparkle 11/22/2021 play soccer
Randy Mod 10/22/2021 play football
Hanah P 11/22/2021 play soccer
Hanah P 09/22/2021 play Baseball
Cristiano Ronaldo 08/22/2021 play soccer
Leo Messi 19/22/2021 play football
您可以只更新导入的 CSV User
属性 上的值,无需创建 $list = @()
来保存结果。
假设 $ImportFile.User
包含有效的 UserPrincipalNames
Azure AD 用户,您遇到的问题将如下所示(绝对不需要函数):
$ImportFile = Import-Csv "C:\AuditLogSearch$($CurDate) Final Audit-Log-Records.csv"
$ExportFile = "C:\AuditLogSearch\FinalFile.csv"
foreach ($u in $ImportFile) {
$u.User = (Get-AzureADUser -ObjectId $u.User).DisplayName
}
$ImportFile | Export-Csv $ExportFile -NoTypeInformation
我正在尝试更改 Activity 列 value/text。例如,如果 text = "play soccer" 然后将其更改为 "play futball" 或者如果 text = "play football" 然后将其更改为 "play rugby" 等等..
这需要 if
个条件或 switch
。即:
# Using this as an Example
$csv = @'
User,Date & Time,Activity
abc@gmail.com,11/22/2021,play soccer
kei@gmail.com,10/22/2021,play football
def@gmail.com,11/22/2021,play soccer
def@gmail.com,09/22/2021,play Baseball
xyz@gmail.com,08/22/2021,play soccer
klm@gmail.com,19/22/2021,play football
'@ | ConvertFrom-Csv
foreach($line in $csv)
{
$line.Activity = switch($line.Activity)
{
'play soccer' { 'play futball'; break }
'play football' { 'play rugby'; break }
Default { $_ } # If no matches, leave it as is
}
}
$csv | Format-Table
User Date & Time Activity
---- ----------- --------
abc@gmail.com 11/22/2021 play futball
kei@gmail.com 10/22/2021 play rugby
def@gmail.com 11/22/2021 play futball
def@gmail.com 09/22/2021 play Baseball
xyz@gmail.com 08/22/2021 play futball
klm@gmail.com 19/22/2021 play rugby