Cole o seguinte em um script. Eu chamo as minas de "Custom-ZTIDomainJoin.ps1"
Eu coloco em% SCRIPTROOT%
[CmdletBinding ()]
Param
(
[Parâmetro (obrigatório = $ True)]
$ Domain,
[Parameter(Mandatory=$True)]
$UserName,
[Parameter(Mandatory=$True)]
$Password,
[Parameter(Mandatory=$False)]
$OU,
[Parameter(Mandatory=$False)]
[Switch]$Log
Limpar a tela
Clear-Host
Definir preferências de ação padrão
$DebugPreference = "Continue"
$ErrorActionPreference = "Continue"
$WarningPreference = "Continue"
Definir caracteres ASCII
$Equals = [char]61
$Space = [char]32
$SingleQuote = [char]39
$DoubleQuote = [char]34
$NewLine = "'n"
Definir diretório de trabalho
$ScriptDir = $MyInvocation.MyCommand.Definition | Split-Path -Parent
$ScriptName = [System.IO.Path]::GetFileNameWithoutExtension($MyInvocation.MyCommand.Name)
$Temp = "$Env:LocalAppData\Temp"
Inicie a saída do script de registro se a opção "/ Log" estiver presente
If ($Log.IsPresent) {(Start-Transcript -Path "$Temp\$ScriptName.log")}
Consulta WMI
$HostName = (Get-WmiObject -Class Win32_ComputerSystem -Property Name | Select -ExpandProperty Name).Trim().ToUpper()
$OSArchitecture = (Get-WmiObject -Class Win32_OperatingSystem -Property OSArchitecture | Select -ExpandProperty OSArchitecture).Replace("-bit", "").Replace("32", "86").Insert(0,"x").ToUpper()
$OSVersion_Major = ([Environment]::OSVersion.Version.Major)
$OSVersion_Minor = ([Environment]::OSVersion.Version.Minor)
[Decimal]$OSVersion = ("$OSVersion_Major" + "." + "$OSVersion_Minor")
Definir funções
#Encode a plain text string to a Base64 string
Function ConvertTo-Base64 ($String)
{
$Encoded = [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($String))
Return $Encoded
}
#Decode an Base64 string to a plain text string
Function ConvertFrom-Base64 ($String)
{
$Decoded = [System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($String))
Return $Decoded
}
Se o script estiver sendo executado em uma sequência de tarefas do Microsoft Deployment Toolkit, execute as seguintes etapas
If (Test-Path -Path TSEnv: -ErrorAction SilentlyContinue)
{
If ($OSVersion -lt "10.0")
{
#MDT passes in sensitive values as Base64 encoded strings, so they MUST be decoded to plain text first
$Domain = ConvertFrom-Base64 -String "$Domain"
$UserName = ConvertFrom-Base64 -String "$UserName"
$Password = ConvertFrom-Base64 -String "$Password" | ConvertTo-SecureString -AsPlainText -Force
$OU = $TSEnv:MachineObjectOU
#Create Credential Object For Active Directory Operations
$Credentials = (New-Object System.Management.Automation.PSCredential("$Domain\$UserName", $Password))
#Join the specified Active Directory Domain
$Device_JoinDomain = (Add-Computer -DomainName $Domain -Credential $Credentials -Force -Verbose)
#Wait 15 Seconds
(Start-Sleep -Seconds "15")
}
ElseIf ($OSVersion -ge "10.0")
{
#MDT passes in sensitive values as Base64 encoded strings, so they MUST be decoded to plain text first
$Password = (ConvertTo-SecureString -String "$Password" -AsPlainText -Force)
$OU = $TSEnv:MachineObjectOU
#Create Credential Object For Active Directory Operations
$Credentials = (New-Object System.Management.Automation.PSCredential("$Domain\$UserName", $Password))
#Join the specified Active Directory Domain
$Device_JoinDomain = (Add-Computer -DomainName $Domain -Credential $Credentials -Force -Verbose)
#Wait 15 Seconds
(Start-Sleep -Seconds "15")
}
}
Se o script NÃO estiver sendo executado em uma sequência de tarefas do Microsoft Deployment Toolkit, execute as seguintes etapas
ElseIf (!(Test-Path -Path TSEnv: -ErrorAction SilentlyContinue))
{
#Convert the password to a Secure String
$Password = (ConvertTo-SecureString -String "$Password" -AsPlainText -Force)
#Create Credential Object For Active Directory Operations
$Credentials = (New-Object System.Management.Automation.PSCredential("$Domain\$UserName", $Password))
#Join the specified Active Directory Domain
$Device_JoinDomain = (Add-Computer -DomainName $Domain -Credential $Credentials -Force -Verbose)
#Wait 15 Seconds
(Start-Sleep -Seconds "15")
}
Parar de registrar a saída do script se a opção "/ Log" estiver presente
Get-Variable | Out-GridView -Title "Variáveis coletadas de $ ScriptName.ps1" -Wait
If ($ Log.IsPresent) {(Stop-Transcript)}