Reputation: 1139
How can this script below modified to export the list of the both servers.txt and Accounts.txt that is not available in Active Directory?
The below script is working fine to import the lists from both files that are available and online, but I need to get the list of wrong or unavailable accounts & computer name into separate log files like:
failed_username.log and failed_computername.log respectively.
$Servers = Get-Content "C:\Users\$env:username\desktop\servers.txt" | Where-Object { ((Get-ADComputer -Filter "Name -eq '$($_)'" -Properties OperatingSystem).OperatingSystem -like '*Windows Server*') -and (Test-Connection -ComputerName $_ -Count 2 -Quiet) }
$Users = Get-Content "C:\Users\$env:username\desktop\Accounts.txt" | Where-Object { (Get-ADUser -Filter "SamAccountName -eq '$($_)'") }
ForEach ($Server in $Servers)
{
$ComputerName = $Server.Trim()
Write-Host "Processing $ComputerName" -ForegroundColor Green
ForEach ($Username in $Users)
{
$UserName = $User.Trim()
Write-Host "Processing $UserName" -ForegroundColor DarkGreen
}
}
Upvotes: 0
Views: 290
Reputation: 5232
May I suggest another approach? I think it wouldn't make that much sense to put users and computers to the same list.
I'd do it this way: I'd collect the information about the computers in a list like this:
$ServerInputList =
Get-Content "C:\Users\$env:username\desktop\servers.txt"
foreach ($ComputerName in $ServerInputList) {
[PSCustomObject]@{
Name = $ComputerName
OS = (Get-ADComputer -Filter "Name -eq '$($ComputerName)'" -Properties OperatingSystem).OperatingSystem
Online = Test-Connection -ComputerName $ComputerName -Count 1 -Quiet
}
}
Now you can easily filter for the computers you want. The same would work for the users.
Upvotes: 1