大家好,我是資深菜鳥工程師,前一篇有人打醒了我,沒有程式經驗就乖乖的利用現有的指令,所以目前寫了一個超直覺無腦的 Script ,目前是直接整個程式碼貼進去執行,要寫成 Script 之類的嗎?
目前暫時剩一個美化的小問題來請教大家如何解決,如果要教我怎麼重個改寫,我也是很歡迎。
$OUTPUT_FILE = "D:\20240806.csv"
$User1 = Get-ADUser -Filter * -SearchBase "OU=Accounts,DC=nam,DC=aaaa,DC=bbbb,DC=ccc" -properties SamAccountName
$SG1 = $User1.SamAccountName
Foreach($UserNo in $SG1)
{
$User2 = Get-ADUser $UserNo -properties department,title,displayname,mailNickname,mail,memberof,enabled
Write-Output """" | Out-File -FilePath $OUTPUT_FILE -NoNewline -Append -Encoding "UTF8"
Write-Output $User2.department | Out-File -FilePath $OUTPUT_FILE -NoNewline -Append -Encoding "UTF8"
Write-Output """,""" | Out-File -FilePath $OUTPUT_FILE -NoNewline -Append -Encoding "UTF8"
Write-Output $User2.title | Out-File -FilePath $OUTPUT_FILE -NoNewline -Append -Encoding "UTF8"
Write-Output """,=""" | Out-File -FilePath $OUTPUT_FILE -NoNewline -Append -Encoding "UTF8"
Write-Output $UserNo | Out-File -FilePath $OUTPUT_FILE -NoNewline -Append -Encoding "UTF8"
Write-Output """,""" | Out-File -FilePath $OUTPUT_FILE -NoNewline -Append -Encoding "UTF8"
Write-Output $User2.displayname | Out-File -FilePath $OUTPUT_FILE -NoNewline -Append -Encoding "UTF8"
Write-Output """,""" | Out-File -FilePath $OUTPUT_FILE -NoNewline -Append -Encoding "UTF8"
Write-Output $User2.enabled | Out-File -FilePath $OUTPUT_FILE -NoNewline -Append -Encoding "UTF8"
Write-Output """,""" | Out-File -FilePath $OUTPUT_FILE -NoNewline -Append -Encoding "UTF8"
Write-Output $User2.mailNickname | Out-File -FilePath $OUTPUT_FILE -NoNewline -Append -Encoding "UTF8"
Write-Output """,""" | Out-File -FilePath $OUTPUT_FILE -NoNewline -Append -Encoding "UTF8"
Write-Output $User2.mail | Out-File -FilePath $OUTPUT_FILE -NoNewline -Append -Encoding "UTF8"
Write-Output """,""" | Out-File -FilePath $OUTPUT_FILE -NoNewline -Append -Encoding "UTF8"
$SG2 = $User2.MemberOf
Foreach($group in $SG2)
{
$thisgroup1 = $group.split(",")[0].split("=")[1]
$thisgroup2 = $group.split(",")[2].split("=")[1]
if($thisgroup2 -match 'Microsoft Exchange Security Groups')
{
Write-Output $thisgroup1 | Out-File -FilePath $OUTPUT_FILE -Append -Encoding "UTF8"
}
}
Write-Output """" | Out-File -FilePath $OUTPUT_FILE -Append -Encoding "UTF8"
}
因為 CSV 的特性,一組雙引號就是一個儲存格,然後所以資料必須都在同一行,在最後的雙引號後換行,就是第二列的資料,但是若是換行符號在一組雙引號裡面,則表示這個儲存格有多行資料。
問題出在最後三個 Out-File ,為了達成多筆資料在一個儲存格,一筆一行,所以第一個 Out-File 有用上 -NoNewLine ,再配上第二個沒用 -NoNewLine 的 Out-File ,輸出效果就會是
"aaaa
因為第二個 Out-File 是在 Foreach 裡面,所以若是輸出三筆資料,則會是
"aaaa
bbbb
cccc
現在問題是,要如何在最後一筆的尾巴加上雙引號?目前寫法的輸出是
"aaaa
bbbb
cccc
"
這樣檔案在用 Excel 開的時候,儲存格的資料會是四行 (多一個空白行) ,而不是三行。
謝謝!
使用字串加法
$s = ""
$s += """"
$s += $User2.department
$s += ""","""
# 進入foreach迴圈,加入多個$thisgroup1 和換行
$s += $thisgroup1 + "`n"
# 離開foreach迴圈之後,刪除最後一個換行
if ($s[-1] -eq "`n"){
$s = $s.Substring(0, $s.Length - 1)
}
$s += """"
Write-Output $s | Out-File -FilePath $OUTPUT_FILE -Append -Encoding "UTF8"
另外一種方式
$OUTPUT_FILE = "D:\20240806.csv"
$User1 = Get-ADUser -Filter * -SearchBase "OU=Accounts,DC=nam,DC=aaaa,DC=bbbb,DC=ccc" -properties SamAccountName
$SG1 = $User1.SamAccountName
Foreach($UserNo in $SG1)
{
$User2 = Get-ADUser $UserNo -properties department,title,displayname,mailNickname,mail,memberof,enabled
$SG2 = $User2.MemberOf
$arr = @() # 空陣列
Foreach($group in $SG2)
{
$thisgroup1 = $group.split(",")[0].split("=")[1]
$thisgroup2 = $group.split(",")[2].split("=")[1]
if($thisgroup2 -match 'Microsoft Exchange Security Groups')
{
$arr += $thisgroup1 # 加入陣列
}
}
$o = [PSCustomObject]@{
department = $User2.department
title = $User2.title
UserNo = $UserNo
displayname = $User2.displayname
enabled = $User2.enabled
mailNickname = $User2.mailNickname
mail = $User2.mail
group = $arr -join "`n" # 陣列轉字串,以換行連接
}
$o | Export-Csv -path $OUTPUT_FILE -NoTypeInfo -Encoding UTF8 -Append
}