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