Remote on Active Directory ?
Bonjour à tous,
Aujourd'hui je m'en remet à vous pour mon problème après pleins de recherches (et de l'aide en vain sur Stack Overflow) je test ici ^^.
J'ai dans une entreprise (pour un stage) installé et configuré 2 Windows serveur en 2008 R2. Jusque là, tout va bien.
On m'en ensuite dit ceci :
Citation:
Comme tu vas aller sur chaque serveur peux-tu me donner les admins locaux ?
Ce que j'ai testé sur mon premier serveur avec cette démarche :
1. Sur le bureau de mon serveur création d'un fichier "List1.txt" qui contient juste :
01SPEAI-PROD1
01SPEAI-PROD2
02SPHPV-PROD1
01SLCPTAAP-PROD
01SLSPSSA-PROD1
2. Écriture du script PowerShell :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| Write-Host Script to display members of the local -ForegroundColor Green
Write-Host Administators group of a remote server. -ForegroundColor Green
Write-Host "`n"
Write-Host Script is running... Please dont close this command prompt! -ForegroundColor Red
Write-Host "`n"
Get-Content 'C:\Users\herbautr\Desktop\List1.txt' | ForEach-Object {
$computer = [ADSI]("WinNT://" + $_ + ",computer")
$group = $computer.PSBase.Children.Find("administrators")
Write-Output "Computer Name : "$computer.Name
Write-Output "__________________________________"
Write-Output "Group Name : "$Group.Name
Write-Output "__________________________________"
$domain = $group.Path.Split("/")[2]
$string1 = "WinNT://" + $domain + "/" + $_ + "/"
$string2 = $_ + "/"
$string3 = "WinNT://"
$members = ($group.PSBase.Invoke("Members") | Foreach-Object {$_.GetType().InvokeMember("Adspath", 'GetProperty', $null, $_, $null)}) -replace ($string1,$string2) -replace ($string3,"")
Write-Output "Members : "
$members
Write-Output ""
Write-Output "---------------Next---------------"
} | Set-Content 'C:\Users\herbautr\Desktop\admins.txt' |
3. En sortie sur mon bureau j'ai bien le fichier "admins.txt" avec ceci :
Computer Name :
01SPEAI-PROD1
__________________________________
Group Name :
Administrators
__________________________________
Members :
01SPEAI-PROD1/Administrator
TEST-PRINCIPAL/Admins du domaine
TEST-PRINCIPAL/svceri
---------------Next---------------
Computer Name :
01SPEAI-PROD2
__________________________________
Group Name :
Administrators
__________________________________
Members :
01SPEAI-PROD2/Administrator
TEST-PRINCIPAL/Admins du domaine
TEST-PRINCIPAL/svceri
---------------Next---------------
Ainsi de suite pour tous les serveurs que List1.txt contient.
Note : TEST-PRINCIPAL, TEST remplace le nom de la société soucis de confidentialité :)
Venons-en au problème... on m'a demandé de ne plus utilisé de fichier .txt ou .csv sur le bureau pour extraire ces informations, mais de directement passer par l'AD (Active Directory) et là... je ne vois absolument pas comment faire.. j'ai demandé de l'aide et donc écrit des trucs du genre :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| Invoke-Command{
$members = net localgroup administrators |
where {$_ -AND $_ -notmatch "commande completed successfully"} |
select -skip 4
New-Object PSObject -Property @{
Computername = $env:COMPUTERNAME
Group = "Administrators"
Members=$members
}
} -computer AD -HideComputerName |
Select * -ExcludeProperty RunspaceID
$objDomain = New-Object System.DirectoryServices.DirectoryEntry
$objSearcher = New-Object System.DirectoryServices.DirectorySearcher
$objSearcher.SearchRoot = $objDomain
$objSearcher.PageSize = 1000
$objSearcher.Filter = $strFilter
$objSearcher.SearchScope = "Subtree"
$colProplist = "name"
foreach ($i in $colPropList){$objSearcher.PropertiesToLoad.Add($i)}
$colResults = $objSearcher.FindAll()
foreach ($objResult in $colResults)
{$objItem = $objResult.Properties; $objItem.name} |
Sans obtenir le résultat voulu et sans trop comprendre... quelqu'un pourrait-il m'éclaircir sur comment faire ? Car sur google trop de tutoriels sont consacrés au fait d'ajouter ou manager ou modifier un membre de l'AD mais pas ce que je veux, et la plupart du temps en ligne de commande uniquement.
EDIT : J'ai retrouvé un fichier qu'on m'a envoyé et on m'a dit de m'inspirer de ceci concernant la partie AD (-SearchBase, les OU, DC, etc..) :
Citation:
[PS] Get-ADUser -SearchBase "OU=TEST,DC=test,DC=local" -searchscope subtree -ldapfilter {(&(objectCategory=person)(objectClass=user)(employeeID=*)(mail=*)(cn=*)(userAccountControl:1.2.840.113556.1.4.803:=0))} -properties * | select Surname,GivenName,EmployeeID,Mail | Export-csv -NoTypeInformation -Delimiter ";" -Encoding utf8 c:\exports\users_mail_insee.csv
Get-ADUser -SearchBase "OU=TEST,DC=test,DC=local" -searchscope subtree -ldapfilter {(&(objectCategory=person)(objectClass=user)(employeeID=*)(mail=*)(cn=*)(!userAccountControl:1.2.840.113556.1.4.803:=2))} -properties * | select sAMAccountName,userPrincipalName,name,distinguishedName,lastLogon,logonCount,title,description,msExchRecipientDisplayType | Export-csv -NoTypeInformation -Delimiter ";" -Encoding utf8 c:\exports_users\nom_prenom_mail.csv
Au cas où ça pourrait vous inspirez aussi ^^
Merci d'avance,
Cordialement Skyxia.