tacticalrmm/scripts/Win_AD_Join_Computer.ps1

79 lines
2.0 KiB
PowerShell
Raw Normal View History

2021-05-12 02:53:37 +00:00
<#
.SYNOPSIS
2021-06-19 02:57:23 +00:00
Joins computer to Active Directory.
2021-05-12 02:53:37 +00:00
.DESCRIPTION
2021-06-19 02:51:21 +00:00
Computer can be joined to AD in a specific OU specified in the parameters or it will join the default location.
2021-05-12 02:53:37 +00:00
.OUTPUTS
2021-06-20 00:50:56 +00:00
Results are printed to the console and sent to a log file in C:\Temp
.EXAMPLE
In parameter set desired items
-domain DOMAIN -password ADMINpassword -UserAccount ADMINaccount -OUPath OU=testOU,DC=test,DC=local
2021-05-12 02:53:37 +00:00
.NOTES
Change Log
V1.0 Initial release
V1.1 Parameterization; Error Checking with conditionals and exit codes
V1.2 Variable declarations cleaned up; minor syntax corrections; Output to file added (@jeevis)
2021-05-12 02:53:37 +00:00
2021-06-19 02:51:21 +00:00
Reference Links:
www.google.com
docs.microsoft.com
2021-05-12 02:53:37 +00:00
#>
param(
$domain,
$password,
$UserAccount,
$OUPath
)
if ([string]::IsNullOrEmpty($domain)) {
Write-Output "Domain must be defined. Use -domain <value> to pass it."
EXIT 1
}
if ([string]::IsNullOrEmpty($UserAccount)) {
Write-Output "User Account must be defined. Use -UserAccount <value> to pass it."
EXIT 1
}
if ([string]::IsNullOrEmpty($password)){
Write-Output "Password must be defined. Use -password <value> to pass it."
EXIT 1
}
else{
$username = "$domain\$UserAccount"
$password = ConvertTo-SecureString -string $password -asPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential($username, $password)
}
try{
if ([string]::IsNullOrEmpty($OUPath)){
Write-Output "OU Path is not defined. Computer object will be created in the default OU."
Add-Computer -DomainName $domain -Credential $credential -Restart
echo "Add-Computer -DomainName $domain -Credential $credential -Restart" >> C:\Temp\ADJoinCommand.log
EXIT 0
}
else {
Add-Computer -DomainName $domain -OUPath $OUPath -Credential $credential -Restart
echo "Add-Computer -DomainName $domain -OUPath $OUPath -Credential $credential -Restart" >> C:\Temp\ADJoinCommand.log
EXIT 0
}
}
catch{
Write-Output "An error has occured."
EXIT 1
}
2021-06-19 03:25:15 +00:00
Exit $LASTEXITCODE