Quantcast
Channel: The Official Scripting Guys Forum! forum
Viewing all articles
Browse latest Browse all 15028

Formatting output to CSV

$
0
0

I'm looking for some help with my script that I have.

The process in how it works is as follows,

1. Gather all computer objects within a domain (dnshostname)

2. Ping test to see which ones are up and active.

3. For each active computer

 3.1 Print the hostname

 3.2 query the objects Sid,sAMAccountName, etc information

 3.3 Print the output from Get-LocalAcctInfo into a text file.

4. Repeat

Here's the code I'm using, I know it's not perfect but it's a work in progress.

function Get-LocalAcctInfo {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory=$true)]
        $ComputerName
    )
    Process {
        Foreach ($Computer in $ComputerName) {
            Try {
                Add-Type -AssemblyName System.DirectoryServices.AccountManagement
                $PrincipalContext = New-Object System.DirectoryServices.AccountManagement.PrincipalContext([System.DirectoryServices.AccountManagement.ContextType]::Machine, $Computer)
                $UserPrincipal = New-Object System.DirectoryServices.AccountManagement.UserPrincipal($PrincipalContext)
                $Searcher = New-Object System.DirectoryServices.AccountManagement.PrincipalSearcher
                $Searcher.QueryFilter = $UserPrincipal
                $computer
                $Searcher.FindAll() | Format-List -Property Sid,SamAccountName,Enabled,LastPasswordSet,PasswordNotRequired,PasswordNeverExpires,UserCannotChangePassword
                }
            Catch {
                Write-Warning -Message "$($_.Exception.Message)"
            }
        }
    }
}


$rtn = $null
$count = 0

Get-ADComputer -Filter * |
ForEach-Object {
    if((Test-Connection -CN $_.dnshostname -Count 1 -Buffersize 16 -quiet) -match 'True') 
    {
    write-host -ForegroundColor green $_.dnshostname
    Get-LocalAcctInfo $_.dnshostname | Out-File C:\Users\UserAccount\Desktop\results.txt -append
    $count = $count + 1
    }
    else
    {
    write-host -ForegroundColor red $_.dnshostname
    }  
}

write-host $count " hosts are active and were enumerated on the domain."


What I'm trying to accomplish is to have the computer name along with the account information formatted nicely within a csv file. 

I have working code that is able to place all the information about the computer object within a csv when I use the following within my function and code.

$Searcher.FindAll() | Where-Object {$_.Sid -Like "*-*"}

...

Get-SWLocalAdmin $_.dnshostname | Export-Csv C:\Users\adsecure_rstrong\Desktop\text.csv


The output of the code that I pasted in the code block above is the following

"computername.domain.com

Sid                      : S-1-5-21-1213413474-1213434134760-84413413436-500
SamAccountName           : Administrator
Enabled                  : True
LastPasswordSet          : 1/1/2014 8:27:20 PM
PasswordNotRequired      : False
PasswordNeverExpires     : False
UserCannotChangePassword : False"

I'd like the computername to be placed with the rest of the output and in csv format


Any suggestions or recommendations are welcome. I'm new to Powershell and learning/teaching myself along the way. Note, that my original code works but I'd rather put it into a csv file so I can easily filter out different columns.

Thanks,

Rob.


Viewing all articles
Browse latest Browse all 15028

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>