none
Output of PowerShell scripts to CSV files RRS feed

  • Question

  • Hi together!

    I would like to save the two issues (currently displayed in the PowerShell) of the PowerShell script below into two CSV files.
    How do I use the "Out-File" command correctly?

    Thanks in advance for the help!

    # to get Get-ADGroup etc...
    # Add-WindowsFeature RSAT-AD-PowerShell
    $allGroups = Get-Content groups.csv
    foreach ($sourceGroup in $allGroups)
    {
        $adGroupName = $sourceGroup -replace 'PROD\\', ''
       
        try
        {
            $sourceADGroup = Get-ADGroup $adGroupName -ErrorAction SilentlyContinue
            $destADGroup = Get-ADGroup -server axusr.net $adGroupName -ErrorAction SilentlyContinue
           
            $sourceMembers = Get-ADGroupMember $sourceADGroup
            $destMembers = Get-ADGroupMember -Server axusr.net $destADGroup
            $missingInSource = $destMembers.samaccountname | ? { $sourceMembers.samaccountname -notcontains $_ }
            $missingInDest = $sourceMembers.samaccountname | ? { $destMembers.samaccountname -notcontains $_ }
           
            # exclude service accounts
            $missingInDest = $missingInDest | ? { $_ -notmatch '^SVC[-_].*' }
            # exclude d-accounts (bad check)
            $missingInDest = $missingInDest | ? { $_ -notmatch '^SAPService.*' }
            <#
            # exclude dt-accounts
            $missingInDest = $missingInDest | ? { $_ -notmatch '^DT.*' }
            # exclude d-accounts (bad check)
            $missingInDest = $missingInDest | ? { $_ -notmatch '^D.*' }
            #>
            if ($missingInDest.Count -gt 0 -or $missingInSource.Count -gt 0)
            {
                "---------------------"
                $adGroupName + ": " + $sourceMembers.Count + "/" + $destMembers.Count
                if ($missingInDest.Count -gt 0)
                {
                    "missing in dest:"
                    $missingInDest
                }
                if ($missingInSource.Count -gt 0)
                {
                    "missing in source:"
                    $missingInSource
                }
            }
        }
        catch { Write-Warning "ad group '$adGroupName' does not exist in AXUSR" }

    }


    • Edited by Tomcat456 Friday, June 21, 2019 1:37 PM Update
    Friday, June 21, 2019 12:14 PM

All replies