FLAG , , .
-
Blockquote
powershell_script 'Domain_Join' do
guard_interpreter :powershell_script
code -domainJoin
$currentTime = Get-Date
$currentTimeString = $currentTime.ToString()
$systemDomain = (Get-WmiObject Win32_ComputerSystem).Domain
If (($systemDomain) -eq 'domainName')
{
Try {
write-host "$env:computerName is DOMAIN MEMBER"
Remove-Item C:\\DomainJoinFlag.txt -ErrorAction Stop
Unregister-ScheduledTask -TaskName "Chef client schedule_DJ" -Confirm:$false
}
Catch [System.Management.Automation.ItemNotFoundException]
{ write-host "Server is already domain member, Or Exception raised due to either missing FLAG file or Server startup schedule task configuration."
eventcreate /t INFORMATION /ID 0909 /L APPLICATION /SO "ChefClient_$env:computerName" /D "Server is already domain member, Or Exception raised due to either missing FLAG file or Server startup schedule task configuration. Refer to the CHEF reciepie for DomainJoin or check Administrative credentials for creting schedule task"}
}
else { write-host "$env:computerName is NOT domain member, joining the server to the domain. Server will be rebooting in a while..."
eventcreate /t INFORMATION /ID 0909 /L APPLICATION /SO "ChefClient_$env:computerName" /D "Joining the server : $env:ComputerName to the domain ININLAB.COM (Server Time): $currentTimeString"
New-Item C:\\DomainJoinFlag.txt -type file -force
write-host "$env:computerName DOMAIN JOIN INITIATED for the server"
$cred = New-Object System.Management.Automation.PsCredential("domain\\domain_user", (ConvertTo-SecureString "Password" -AsPlainText -Force))
Add-Computer -DomainName "domainName" -Credential $cred -OUPath "OU=HyperV,OU=Infrastructure,OU=Servers,DC=domain,DC=name"
shutdown -r -t 120
}
domainJoin
notifies :run, 'windows_task[Chef client schedule_DJ]', :immediately
end
windows_task 'Chef client schedule_DJ' do
user 'SYSTEM'
command 'chef-client -L C:\chef\chef-client_after_reboot_domainJoin.log'
run_level :highest
frequency :onstart
frequency_modifier 30
action :create
only_if { ::File.exist?('C:\\DomainJoinFlag.txt') }
end