以下をスクリプトに貼り付けます。鉱山を「Custom-ZTIDomainJoin.ps1」と呼びます
%SCRIPTROOT%に配置します
[CmdletBinding()]
パラム
(
[パラメータ(必須= $ True)]
$ドメイン、
[Parameter(Mandatory=$True)]
$UserName,
[Parameter(Mandatory=$True)]
$Password,
[Parameter(Mandatory=$False)]
$OU,
[Parameter(Mandatory=$False)]
[Switch]$Log
)
画面をクリアする
Clear-Host
デフォルトのアクション設定を定義する
$DebugPreference = "Continue"
$ErrorActionPreference = "Continue"
$WarningPreference = "Continue"
ASCII文字を定義する
$Equals = [char]61
$Space = [char]32
$SingleQuote = [char]39
$DoubleQuote = [char]34
$NewLine = "`n"
作業ディレクトリを設定する
$ScriptDir = $MyInvocation.MyCommand.Definition | Split-Path -Parent
$ScriptName = [System.IO.Path]::GetFileNameWithoutExtension($MyInvocation.MyCommand.Name)
$Temp = "$Env:LocalAppData\Temp"
"/ Log"スイッチが存在する場合、スクリプト出力のログ記録を開始します。
If ($Log.IsPresent) {(Start-Transcript -Path "$Temp\$ScriptName.log")}
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")
関数を定義する
#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
}
スクリプトがMicrosoft Deployment Toolkitタスクシーケンス内で実行されている場合は、次の手順を実行します。
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")
}
}
スクリプトがMicrosoft Deployment Toolkitタスクシーケンス内で実行されていない場合は、次の手順を実行します。
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")
}
"/ Log"スイッチが存在する場合、スクリプト出力のログ記録を停止します
変数の取得Out-GridView -Title "$ ScriptName.ps1から収集した変数" - 待機
If($ Log.IsPresent){(Stop-Transcript)}