Howdy
I was able to get this script working from some posts on here. I need to find out if anyone can help me add in some code that would try different credentials if the creds that I was logged in with failed. There are standalone servers and multiple domains. I found one post on another site with a script that had multiple creds specified and you get prompted to enter them. The problem is that if you provide 2 or 3 sets of creds, all of them have to work against each server or an error keeps coming up.
Can anyone help with a way to have it loop through a few different creds, and I'd like to somehow encrypt the password in the script for the creds so I can schedule it to run as a task and then get it emailed out.
I put a couple comments in bold as I am not sure I need the lines below them?
$admins = @()
foreach ($computername in get-content Q:\Scripts\Powershell\Test\Systems.txt){
write-host $computername
$localGroupName = "Administrators"
$group = [ADSI]("WinNT://$computerName/$localGroupName,group")
$group.Members() |
foreach {
$AdsPath = $_.GetType().InvokeMember('Adspath', 'GetProperty', $null, $_, $null)
$a = $AdsPath.split('/',[StringSplitOptions]::RemoveEmptyEntries)
$name = $a[-1]
$domain = $a[-2]
# Ignore non-local accounts
if ($domain -eq $computerName)
{
$user=([ADSI]"WinNT://$($computername)/$($name)")
$adminInfo = @{
Name = $name;
Computer = $computerName;
LastLogin = $(([ADSI]"WinNT://$($computername)/$($name)").lastlogin)
NeverExpires=[bool]($neverexpiresflag -band $user.userflags[0])
Disabled=[bool]($disabledflag -band $user.userflags[0])
}
$admin = New-Object -TypeName PSObject -Property $adminInfo
$admins += $admin
}
}
}
#I didn't think this line was needed either?
$date=(get-date -format yyyyMMddHHmm)
# What does this line actually do? I got confused when I tried to remove it, but I'm not pulling domain accounts?
$outpath=$srcdomain +"_Admins_"+$date+".csv"
$admins | select name,computer,lastlogin,neverexpires,disabled | Export-Csv -NoTypeInformation Q:\Scripts\Powershell\Test