Overblog
Editer l'article Suivre ce blog Administration + Créer mon blog

script to join domain in any case (osd or in windows) and self-destruct itself

Publié le par damcuvelier

$erroractionpreference = 'silentlycontinue'
set-executionpolicy bypass -force

$DomainAdmin="<account can join computer to domain>"
$DomainAdminDomain='<FDQN Domain Name>'
$MachineObjectOU="OU=<Computer OU Name>,OU=<Parent OU Name>,<ADSI Domain Name>"
$DomainAdminPassword="account password in clear"

$tsenv = New-Object -COMObject Microsoft.SMS.TSEnvironment
$scriptpath = split-path -parent $MyInvocation.MyCommand.Path
$scriptname = $MyInvocation.MyCommand.Name
$scriptfullname = "$scriptpath\$scriptname"
$cmdscriptfullname = $scriptfullname.replace(".ps1",".cmd")
$LogPath = $tsenv.Value('LogPath')
if(! $LogPath){$LogPath = $scriptpath}
$logfile = $scriptname.replace(".ps1",".log")
$logfilename = "$LogPath\$logfile"
$TranscriptLogFile = "$LogPath\$Transcript_logfile"

function logorhost($value){
    logorhost($value
    $value | out-file $logfilename -Encoding ascii -Force
    write-host $value
}

logorhost("LogPath = $LogPath")
logorhost("JoinDomainlogfile = $TranscriptLogFile")

Start-Transcript -Path $TranscriptLogFile -Append
logorhost("Recover in Domain:")


$JoinDomain=$DomainAdminDomain
$SecurPassword = ConvertTo-SecureString $DomainAdminPassword -AsPlainText -Force
$DomainAdminPassword = ""
$credential = New-Object System.Management.Automation.PSCredential ("$DomainAdminDomain\$DomainAdmin", $SecurPassword)

logorhost("Join Domain $JoinDomain")
$errorMsg = $(Add-Computer -DomainName $JoinDomain -OUPath $MachineObjectOU -Credential $credential -Force 2>&1)

if($?) {
    logorhost("L'ordinateur a été ajouté au domaine avec succès.")
}
else {
    logorhost("Une erreur s'est produite lors de l'ajout de l'ordinateur au domaine.")
    logorhost($errorMsg $logfilename
}

"del /F /Q $scriptfullname" | out-file $cmdscriptfullname -Encoding ascii -Force
"del /F /Q $cmdscriptfullname" | out-file $cmdscriptfullname -Append Encoding ascii -Force
cmd /c $cmdscriptfullname
 

 

Commenter cet article