我想将此报告导出到csv文件中,但数据不会延续。文本文件工作正常,但我更喜欢它是excel或至少csv格式。请指教。脚本如下。
$output = Read-Host "'Y' for output to file or any key for output in GUI table view" -foreground Cyan
$fqdn = Read-Host "Enter FQDN domain"
$cred = Get-Credential
Write-Host "Contacting $fqdn domain..." -ForegroundColor Yellow
$domain = (get-addomain $fqdn -Credential $cred | select distinguishedName, pdcEmulator, DNSroot, DomainControllersContainer)
Write-Host "Completed. Enumerating OUs.." -ForegroundColor Yellow
$OUlist = @(Get-ADOrganizationalUnit -filter * -Credential $cred -SearchBase $domain.distinguishedName -SearchScope Subtree -Server $domain.DNSroot)
Write-Host "Completed. Counting users..." -ForegroundColor Yellow
For ($i = 1; $i -le $oulist.Count; $i++)
{ write-progress -Activity "Collecting OUs" -Status "Finding OUs $i" -PercentComplete ($i/$OUlist.count * 100) }
$newlist = @{ }
foreach ($_objectitem in $OUlist)
{
$getUser = Get-ADuser -Filter * -Credential $cred -SearchBase $_objectItem.DistinguishedName -SearchScope OneLevel -Server $domain.pdcEmulator | measure | select Count
for ($i = 1; $i -le $getUser.Count; $i++)
{ write-progress -Activity "Counting users" -Status "Finding users $i in $_objectitem" -PercentComplete ($i/$getUser.count * 100) }
$newlist.add($_objectItem.DistinguishedName, $getUser.Count)
}
if ($output -eq "Y")
{
$newlist | ft -AutoSize | Out-File .\OUuserCount.txt
Write-Host "All done!" -ForegroundColor yellow
}
else
{
$newList | Out-GridView
}
答案 0 :(得分:0)
您的问题是$newlist
是哈希表。您可以使用$newlist.GetEnumerator() | Export-CSV ...
或者像下面一样使用对象而不是哈希表。 [pscustomobject]
的效率略低于哈希表,但功能更强大。我还提出了一些内联评论(样式/写入进度)
$output = Read-Host "'Y' for output to file or any key for output in GUI table view" -foreground Cyan
$fqdn = Read-Host "Enter FQDN domain"
$cred = Get-Credential
Write-Host "Contacting $fqdn domain..." -ForegroundColor Yellow
$domain = (get-addomain $fqdn -Credential $cred | select distinguishedName, pdcEmulator, DNSroot, DomainControllersContainer)
Write-Host "Completed. Enumerating OUs.." -ForegroundColor Yellow
$OUlist = @(Get-ADOrganizationalUnit -filter * -Credential $cred -SearchBase $domain.distinguishedName -SearchScope Subtree -Server $domain.DNSroot)
Write-Host "Completed. Counting users..." -ForegroundColor Yellow
#The write-progress here did nothing, because you already had the OU list before you even started the for loop
#PowerShell community style guide best practices are to use meaningful variable names rather than the older notation from vb era
$newlist = foreach ($OU in $OUlist) {
#The array will automatically have a count property, no need for measure
$Users = Get-ADuser -Filter * -Credential $cred -SearchBase $OU.DistinguishedName -SearchScope OneLevel -Server $domain.pdcEmulator
#Again you already have all of the user object before the loop, just write-progress for the OUlist that you are looping through
write-progress -Activity "Counting users" -Status "Finding users in $OU" -PercentComplete ([array]::indexof($OUlist,$OU)/$OUlist.count * 100)
[pscustomobject]@{OU = $OU.DistinguishedName; Count = $Users.Count)
}
if ($output -eq "Y") {
$newlist | Export-CSV .\OUuserCount.csv
Write-Host "All done!" -ForegroundColor yellow
} else {
$newList | Out-GridView
}
答案 1 :(得分:-1)
请替换$newlist | ft -AutoSize | Out-File .\OUuserCount.txt
与
$newlist | ft -AutoSize | Export-csv OUusercount.csv -append -NoTypeInformation