我從 Powershell 開始,但沒有使用 foreach 回圈匯出 csv。
在幫助下,我有一個沒有匯出 csv 的功能腳本:(
$computers= Get-ADComputer -Filter * -Properties * | Select Description
foreach($computer in $computers)
{
if ($computer.Description -ne $null)
{
$descriptions = $computer.Description -split "\("
# Write-Host $descriptions
$accountname = $descriptions[0]
$pcinfos = $descriptions[1] -split ";"
$serial = $pcinfos[0]
Write-Host "$accountname;$serial"
}
}
#| Export-Csv -Encoding UTF8 -Delimiter "," -Path "**********\exportaddesciption.csv" -NoTypeInformation
你能幫我嗎 ?
謝謝
uj5u.com熱心網友回復:
使用ForEach-Object和Where-Objectcmdlet,并確保創建具有兩個屬性的物件,而不是嘗試手動構造 CSV 行:
$computers |Where-Object { -not [string]::IsNullOrEmpty($_.Description) } |ForEach-Object {
$descriptions = $computer.Description -split "\("
$accountname = $descriptions[0]
$pcinfos = $descriptions[1] -split ";"
$serial = $pcinfos[0]
[pscustomobject]@{
AccountName = $accountname
Serial = $serial
}
} |Export-Csv -Encoding UTF8 -Delimiter ";" -Path "**********\exportaddesciption.csv" -NoTypeInformation
uj5u.com熱心網友回復:
你有幾個問題。您的回答將如下所示。我冒昧地制作您的代碼,就像我在這里學習閱讀和提問一樣。祝你好運!
$computers = Get-ADComputer -Filter * -Properties * | Select-Object Description
$result = $computers | ForEach-Object {
if ($null -ne $computer.Description) {
$descriptions = $computer.Description -split "\("
# Write-Host $descriptions
$accountname = $descriptions[0]
$pcinfos = $descriptions[1] -split ";"
$serial = $pcinfos[0]
Write-Host "$accountname;$serial"
[PSCustomObject]@{
Name = (Enter what you want to export here with variable name)
}
}
}
$result | Export-Csv (Path to report in quotes) -NoTypeInformation
仔細查看其他答案,您會得到一些我沒有提供的擴展答案。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/379972.html
