Powershell

Personnalisation OS

Rename-Computer -NewName 'DC2-1'
Restart-Computer
Get-NetAdapter

Get-NetAdapter -InterfaceIndex 'Adapter le N° Interface : ifIndex'

Rename-NetAdapter -Name 'Nom adaptateur' -NewName 'RL'

Set-NetIPInterface -InterfaceIndex 'N° Interface : ifIndex' -Dhcp Enabled
New-NetIPAddress   -InterfaceIndex 'N° Interface : ifIndex' -IPAddress '@IP' -PrefixLength '8'

# Joindre le domaine ad100.labo

Set-DnsClientServerAddress -InterfaceIndex 'N° Interface : ifIndex' -ServerAddresses '@IP_DNS_DC'

Test-Connection -ComputerName ad100.labo


Add-Computer -DomainName ad100.labo -DomainCredential AD100\Administrateur

Restart-Computer

Déploiement AD

Install-WindowsFeature -Name AD-Domain-Services -IncludeManagementTools

$PasswordSafeMode = ConvertTo-SecureString -AsPlainText 'Pa$$w0rd' -Force

$Params = @{
    CreateDnsDelegation = $false
    DatabasePath = 'C:\Windows\NTDS'
    DomainMode = 'WinThreshold'
    DomainName = 'AD100.LABO'        # Adapter les noms DomainName et DomainNetbiosName
    DomainNetbiosName = 'AD100'
    ForestMode = 'WinThreshold'
    InstallDns = $true
    LogPath = 'C:\Windows\NTDS'
    NoRebootOnCompletion = $false
    SafeModeAdministratorPassword = $PasswordSafeMode
    SysvolPath = 'C:\Windows\SYSVOL'
    Force = $true
}

Install-ADDSForest @Params

AD Personnalisation

$domainInfos = Get-ADDomain

#----- Modifier la stratégie de mots de passe du domaine

$params = @{
        "Identity"=$domainInfos.DNSRoot; 
        "LockoutDuration"="00:00:00";
        "LockoutObservationWindow"="00:00:00";
        "LockoutThreshold"=0;
        "ComplexityEnabled"=$false; 
        "ReversibleEncryptionEnabled"=$False;
        "MaxPasswordAge"="00.00:00:00";
        "MinPasswordAge"="00.00:00:00";
        "PasswordHistoryCount"=0;
        "MinPasswordLength"=0
        }
	
Set-ADDefaultDomainPasswordPolicy @params
Get-ADDefaultDomainPasswordPolicy
Invoke-GPUpdate

Write-Host 'AD Configuration - Default Password Policy OK' -ForegroundColor Green

#----- CreateDialogu --> NOM Prenom

$objADDN = "CN=user-Display,cn=40c,CN=DisplaySpecifiers,CN=Configuration," + $domainInfos.DistinguishedName
Set-ADObject -Identity $objADDN -Replace @{createDialog="%<givenName> %<sn> "}

Write-Host 'AD Configuration - Create Dialogu Box OK' -ForegroundColor Green

#----- Création d"un Administrateur supplémentaire

$Password = ConvertTo-SecureString -AsPlainText "ad" -Force
$upn = "ad@" + $domainInfos.DNSRoot

$params = @{
    "Path"=('cn=Users,' + $domainInfos.DistinguishedName);
    "Name"="AD";
    "AccountPassword"=$Password;
    "Enabled"=$true;
    "UserPrincipalName"=$upn
            }

New-ADUser  @params

$members = Get-ADUser -Identity "Administrator" -Properties memberof
foreach ($group in $members.memberof)
{
    Add-ADGroupMember -Identity $group -Members "AD" -ErrorAction SilentlyContinue
}

Write-Host 'AD Configuration - Admin supplémentaire AD OK' -ForegroundColor Green

Write-Host "Personnalisation TERMINEE" -ForegroundColor Green

Move computer

Get-ADComputer -Identity SV1 | Move-ADObject -TargetPath 'ou=serveurs,ou=_pcform,dc=ad100,dc=labo'
Get-ADComputer -Identity SV1 

Exporter tous les drivers

Sources : https://www.prajwal.org/powershell-export-drivers-from-windows/

Export-WindowsDriver -Online -Destination D:\Drivers


Script de sauvegarde de fichiers avec structure

 

$sourceDir = 'F:\'
$targetDir = 'Z:\backupfilessrv\F\'
$morning = (Get-Date).AddHours(-16)
$afternoon = (Get-Date).AddHours(2)
Get-ChildItem -Path $sourceDir -recurse | Where-Object {$_.LastWriteTime -gt $morning} | Where-Object {$_.LastWriteTime -lt $afternoon} | 
foreach {
    $targetFile = $targetDir + $_.FullName.SubString($sourceDir.Length);
    Write-Host $targetFile
    $m = $_.Mode
    if  ($m[0] -eq 'd') {
        Write-Host "Dir"
        New-Item -ItemType Directory -Path $targetFile -Force;
    } else {
    Write-Host "File"
    New-Item -ItemType File -Path $targetFile -Force;
    Copy-Item $_.FullName -destination $targetFile
    }
    }


Refaire la réapprobation entre un pc et le controleur de domaine

 

Reset-ComputerMachinePassword -Server srv-dc.yourdomain -Credential administrator@yourdomain