需要帮助将本地用户日期导出到 csv ... powershell

Need help exporting localuser date to a csv... powershell

我写了一个 PowerShell 脚本来获取每台机器的所有本地用户数据。我需要将这些数据导出到一个漂亮的 .csv 文件中,其中包含列 ComputerName、ComputerStatus、LocalUserName、LocalUserenabled 和 Localuserdescription...

Import-Module ActiveDirectory
$ErrorActionPreference = "SilentlyContinue"
$Computers = Get-ADComputer -Filter {Enabled -eq $True}
$OutputPath = "$Env:Temp"
$OutputFile = Join-Path $OutputPath "LocalAdminsReport.csv"
Add-Content -Path $OutputFile -Value "ComputerName,ComputerStatus,LocalUserName,LocalUserEnabled,LocalUserDescription"

Foreach ($Computer in $Computers) {
    $ComputerName = $Computer.DNSHostName
    $Connectivity = Test-Connection -Computername $ComputerName -Count 1 -Quiet
    if ($Connectivity) {
        Write-Host "Getting local users from $ComputerName"
        $LocalUsers = Invoke-Command -ComputerName $ComputerName {Get-WmiObject -Class Win32_UserAccount -Filter  "LocalAccount='True'"}
        ForEach ($LocalUser in $LocalUsers){
            Add-Content -Path $OutputFile -Value $ComputerName, "Online", -Value $LocalUser.Name, -Value $LocalUser.Enabled, -Value $LocalUser.Description
        }
    } else { 
        Write-Host "$ComputerName Status Offline"
        Add-Content -Path $OutputFile -Value $ComputerName, "Offline", $null, $null, $null
    }
}

Write-Host " "
Write-Host "========================================"
Write-Host "Report Path: $OutputFile "
Write-Host "========================================"

当前的导出是将所有内容都放入第 1 列,我知道这是因为 add-content 但我真的不知道如何使用 export-csv 来完成。

确实,Add-Content 并不是创建 CSV 文件的最佳工具。

尝试

Import-Module ActiveDirectory
$ErrorActionPreference = "SilentlyContinue"

$OutputFile = Join-Path -Path $env:Temp -ChildPath "LocalAdminsReport.csv"
$Computers  = Get-ADComputer -Filter "Enabled -eq 'True'"  # should be a string

$result = foreach ($Computer in $Computers) {
    $ComputerName = $Computer.DNSHostName
    $Connectivity = Test-Connection -Computername $ComputerName -Count 1 -Quiet

    if ($Connectivity) {
        Write-Host "Getting local users from $ComputerName"
        Invoke-Command -ComputerName $ComputerName -ScriptBlock {Get-WmiObject -Class Win32_UserAccount -Filter "LocalAccount='True'"} | 
            # output an object
            Select-Object @{Name = 'ComputerName'; Expression = {$ComputerName}},
                          @{Name = 'ComputerStatus'; Expression = {'Online'}},
                          @{Name = 'LocalUserName'; Expression = {$_.Name}},
                          @{Name = 'LocalUserEnabled'; Expression = {!$_.Disabled}},
                          @{Name = 'LocalUserDescription'; Expression = {$_.Description}}
    }
    else { 
        Write-Host "$ComputerName Status Offline"
        # output a similar object for failed computers too
        Select-Object @{Name = 'ComputerName'; Expression = {$ComputerName}},
                      @{Name = 'ComputerStatus'; Expression = {'Offline'}},
                      @{Name = 'LocalUserName'; Expression = {$null}},
                      @{Name = 'LocalUserEnabled'; Expression = {$null}},
                      @{Name = 'LocalUserDescription'; Expression = {$null}}
    }
}

# output to csv file
$result | Export-Csv -Path $OutputFile -NoTypeInformation