-1

I would like to have a script that takes a txt file of AD user IDs, indicates which are "not found", provides the DisplayName property for ones that are found and exports the results to a file. Is that possible?

I've tried this, but it didn't return known valid user IDs.

$user = Get-Content "c:\users\oh75256\desktop\powershell learning\userids.txt" $user | foreach { $Name = "$_" $Searcher = [ADSISearcher]"(displayname=$Name)" $Results = $Searcher.FindOne() if ($Results -eq $null) { "$Name not in AD" >> C:\users\oh75256\userstatus.csv } else { $status = (Get-ADUser $Name).Enabled if ($status -eq "True") { "$Name is valid" >> C:\users\oh75256\userstatus.csv } else { "$Name is not valid" >> C:\users\oh75256\userstatus.csv } } }

That works! Now that I have 2 working powershell scripts, I'd like to combine them into one. I have one txt file with 4 columns: hostname, lastuser, status, AD. I would like to start with my ping script, which pings the hostname and if it pings, it returns the loggedon username. if it does not ping, it returns "offline" Then I would like to take the lastuser column for the offline hostname and check it against AD to return the Name of the user or "not found in AD". Here's an example of the text file:pingnames.txt

HostName LastUser Status (offline or SamAccountName) AD (Name or Not Found)

NAHNT-1234567 bruteaa
NAHNT-2345678 OH75256
NAHNT-3456789 YD13099
NAHNT-4567891 rh16633
NAHNT-5678912 Administrator
NAHNT-6789123 xt07718
NAHNT-8912345 AA00718
NAHNT-9123456 LR17114

This is my script to ping and get current logged on user.

function get-avail {
param ( $computername )
$availCheck = New-Object psobject -property @{
ComputerName = $computername
Status = 'Offline'
LogonUser = 'None'
}
$test = Test-Connection -ComputerName $computername -Count 1
-errorAction:SilentlyContinue
if ( $test ) {
$availCheck.Status = 'Online'
$wmics = Get-WmiObject Win32_ComputerSystem -ComputerName $computername
}
if ( $wmics.UserName ) {
$availCheck.LogonUser = $wmics.UserName
}
$availCheck | select ComputerName,Status,LogonUser
}
cat "c:\myfiles\pingnames.txt" | %{ get-avail $_ } | export-csv c:\myfiles\results.csv  

This is my script to validate userID in AD

$userProfile = Get-Content "c:\myfiles\userids.txt" |
Select @{Expression={$_};Label="SamAccountName"},
@{Expression={" "};Label="First Name"},
@{Expression={" "};Label="Last Name"},
@{Expression={$False};Label="ExistInAD"}

foreach($user in $userProfile)
{
$Account =  Get-ADUser $user.SamAccountName 
 -erroraction silentlycontinue
If($?)
{
    $user.SamAccountName
    $user.ExistInAD = $True
    $user.'First Name' = $Account.GivenName
    $user.'Last Name' = $Account.Surname
}
Else
{

    "------------------------ $($user.SamAccountName)" 
}
}

$userProfile | export-csv "c:\myfiles\mycsv.csv" -NoTypeInformation 

Is it possible to combine them? -Thanks, Karen

klhunter
  • 1
  • 2

1 Answers1

0

You state that you are looking for User ID's but your code is searching for display names.

I think you want this line changed:

  $Searcher = [ADSISearcher]"(SamAccountName=$Name)"

If you want additional information with columns you need a to create a PSObject:

$User = Get-Content "c:\users\oh75256\desktop\powershell learning\userids.txt"
$User | foreach {
    $Name = "$_"
    $Searcher = [ADSISearcher]"(SamAccountName=$Name)"
    $Results = $Searcher.FindOne()
    $Object = New-Object -TypeName PSObject
    $Object | Add-Member -Name "SamAccountName" -MemberType NoteProperty -Value $_
    if ($Results -eq $null) 
    {
        $Object | Add-Member -Name "Status" -MemberType NoteProperty -Value "Not in AD"
        $Object | Export-Csv C:\users\oh75256\userstatus.csv -NoTypeInformation -Append
    } 
    else 
    {
        [string]$DisplayName = $Results.Properties.name
        $Object | Add-Member -Name "Name" -MemberType NoteProperty -Value $DisplayName

        $status = (Get-ADUser $Name).Enabled
        if ($status -eq "True") 
        {
            $Object | Add-Member -Name "Status" -MemberType NoteProperty -Value "Enabled"
            $Object | Export-Csv C:\users\oh75256\userstatus.csv -NoTypeInformation -Append
        } 
        else 
        {
            $Object | Add-Member -Name "Status" -MemberType NoteProperty -Value "Disabled"
            $Object | Export-Csv C:\users\oh75256\userstatus.csv -NoTypeInformation -Append
        }
    }
}
Glen Buktenica
  • 302
  • 2
  • 12