Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

Powershell - Search for AD Computer

$
0
0

Hi, I have made this script for search for the AD computers, but it doesn´t work.

What should I change?

Thanks.

$startDTM = (Get-Date) $path = Split-Path -parent "C:\a" $logdate = Get-Date -Format ddmmyyyy $userdisableddate = Get-Date #Define CSV and log file location variables #they have to be on the same location as the script $csvfile = $path + "a\User_Info.csv" $logfile = $path + "a\$logdate.logfile.txt" $scriptrunrime = $path + "a\scripttime.txt" #Define variable for a server with AD web services installed $ADServer = 'noc' $GetAdminact = Get-Credential #Import Active Directory Module Import-Module ActiveDirectory #Import CSV file and update users in the OU with details from the file #Create the function script to update the users Function Update-ADComputers { $Computers = Import-Csv -Path $csvfile $OU = 'ou=secretaria,ou=alcaldia,dc=depresion,dc=com','ou=administrativo,ou=alcaldia,dc=depresion,dc=com', 'ou=movilidad,ou=policia,dc=depresion,dc=com' ForEach ($Computer In $Computers) { $Description = $Computer."Descripcion" $Sam = $Computer."Logon Name" If ($Sam -And $Description) { $OU | Foreach { $ComputerInAD = Get-ADComputer -Server $ADServer -Credential $GetAdminact -LDAPFilter "(sAMAccountName=$Sam)" -Searchbase $_ -SearchScope OneLevel If ( $ComputerInAD ) { Set-ADComputer -Server $ADServer -Credential $GetAdminact -Identity $Sam -Replace @{Description = $Description} } #If Else { #Log error for users that are not in Active Directory or with no Logon name in excel file $sam + " Not modified because it doese not exist in AD or LogOn name field is empty on excel file" | Out-File $logfile -Append } } #Foreach } } } # Run the function script Update-ADComputers $endDTM = (Get-Date) # Echo Time elapsed "Elapsed Time: $(($endDTM-$startDTM).totalseconds) seconds""Elapsed Time: $(($endDTM-$startDTM).totalminutes) minutes"


Viewing all articles
Browse latest Browse all 21975

Trending Articles



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