0

こんにちは、私は基本的にスクリプトを使ってコンピュータの結合解除と結合を自動化するスクリプトを作成し始めました。コンピュータをオンラインに戻すためにpingを開始する必要があります。 un-joiningのインスタンスでは正常に動作しますが、参加しようとするとすべての問題が発生します。JoinとUn-Join Domainスクリプトを適切にスクリプト化する手助け

多分私は新鮮な眼のペアを私に貸してくれます。

<# 
############################################################################################################ 
# Written by CPineda        # NOTE: This only works if computer is on the wire.  # 
# This Script un-joins and re-joins the domain. #  So its very important that we connect the devce # 
# Created: 04/27/2016       #  to the LAN.          # 
# Last revision 4/6/2016       #               # 
############################################################################################################ 
#> 

# Set up your Variables 
$ComputerIP = "" #Stores the Computers IP 
$ComputerName = "" #Stores the Name of the computer you will be working with. 
#$LocalCredentials = "" #Stores the Local Administrator credentials. (As Neeeded) 
$DomainCredentials = "" #Stores the Domain Administrator credentials. 

# Get information needed for the script to run. 
while ($ComputerIP -eq ""){ 

    Clear-Host #Clear the PS Console Window 
    $ComputerIP = Read-Host "Enter the name of the Computer IP" 
} 

while ($ComputerName -eq ""){ 

    Clear-Host #Clear the PS Console Window 
    $ComputerName = Read-Host "Enter the name of the Computer" 
} 

<# while ($LocalCredentials -eq ""){ 

    Clear-Host #Clear the PS Console Window 
    $LocalCredentials = Read-Host "Enter the User name of the Local User Admin Account"  
} 
#> 

while ($DomainCredentials -eq ""){ 

    Clear-Host #Clear the PS Console Window 
    $DomainCredentials = Read-Host "Enter the User name of the Domain User Admin Account"  
} 
# Remove the computer from the Domain. 
Remove-Computer -ComputerName $ComputerName -LocalCredential $ComputerName\administrator -UnJoinDomainCredential kelsonfla\$DomainCredentials -WorkgroupName WORKGROUP -Force -Restart 

Read-Host "Hit ENTER to continue" 

# Ping until computer returns on the wire. 
Clear-Host 
Write-Host "At this time we will ping the compputer in question untill it returns back online" 
Write-Host "Hit ENTER to continue" 
Read-Host 

Test-Connection ($ComputerIP) { 
     $result = Test-Connection $ComputerIP -Count 3 -Delay 10 -Quiet 
     if ($Result | where { $_ -match 'Reply from ' }){$true} 
     else {$false} 
}  
     Write-Verbose "The computer $ComputerIP has went down for a reboot. Waiting for it to come back up..." 
     while (!(Test-Connection -ComputerName $ComputerIP)) { 
      Start-Sleep -Seconds 5 
      Write-Verbose "Waiting for $ComputerIP to come back online" 
} 
     Write-Verbose "The computer $ComputerIP has come online. Waiting for OS to initialize" 
     $EapBefore = $ErrorActionPreference 
     $ErrorActionPreference = [System.Management.Automation.ActionPreference]::SilentlyContinue 
     while (!(Get-WmiObject -ComputerName $ComputerIP -Class Win32_OperatingSystem -Credential $LocalCredentials)) { 
      Start-Sleep -Seconds 5 
      Write-Verbose "Waiting for OS to initialize..." 
     $ErrorActionPreference = $EapBefore 
} 

# Add computer back to the Domain. 
Add-Computer -ComputerName $ComputerIP -LocalCredential $ComputerName\administrator -DomainName kelsonfla.local -Credential kelsonfla\$DomainCredentials -Restart -Force 

Read-Host "Hit ENTER to continue" 

# Ping until computer returns on the wire. 
Clear-Host 
Write-Host "At this time we will ping the compputer in question untill it returns back online" 
Write-Host "Hit ENTER to continue" 
Read-Host 

Test-Connection ($ComputerIP) { 
     $result = Test-Connection $ComputerIP -Count 3 -Delay 10 -Quiet 
     if ($Result | where { $_ -match 'Reply from ' }){$true} 
     else {$false} 
}  
     Write-Verbose "The computer $ComputerIP has went down for a reboot. Waiting for it to come back up..." 
     while (!(Test-Connection -ComputerName $ComputerIP)) { 
      Start-Sleep -Seconds 5 
      Write-Verbose "Waiting for $ComputerIP to come back online" 
} 
     Write-Verbose "The computer $ComputerIP has come online. Waiting for OS to initialize" 
     $EapBefore = $ErrorActionPreference 
     $ErrorActionPreference = [System.Management.Automation.ActionPreference]::SilentlyContinue 
     while (!(Get-WmiObject -ComputerName $ComputerIP -Class Win32_OperatingSystem -Credential $LocalCredentials)) { 
      Start-Sleep -Seconds 5 
      Write-Verbose "Waiting for OS to initialize..." 
     $ErrorActionPreference = $EapBefore 
} 

Clear-Host 
Write-Output "If you are Reading this then you have successfully Unjoined and Re-Joined a Computer to the Network" 
Start-Sleep -Seconds 3 
Clear-Host 
+0

発生している特定の問題を再現するのに必要な最小限のコードしか含まれていない短いスクリプトを提供することをお勧めします。 –

+0

実際の返答は何ですか?またはwhileループが永遠に続く状況ですか?消えるWindowsファイアウォールを無効にしているGPOがありますか?そして、あなたは接続されていないマシンを見ようとしているファイアウォールの問題に対処していますか? – ssaviers

+0

@Bill_Stewartの問題は、ping要求または冗長コマンドがまったく表示されないことです。最初に実行すると、認証の全過程を経て実行され、次にping処理のために何も入力しなくなります。 – CharlesP

答えて

0

まず

Test-Connection ($ComputerIP) { 
     $Result = ping $ComputerIP -n 3 
     if ($Result | where { $_ -match 'Reply from ' }) { 
      $true 
     } else { 
      $false 
     } 
} 

と単に

!$result = $false 
do { 
    Write-Verbose "Waiting for $ComputerIP to come back online" 
    $result = Test-Connection $ComputerIP -Count 2 -Delay 5 -Quiet 
} while (!$result) 

を使用し、見に$resultブールの値を使用します。pingの一部について、あなたはコードを削除する必要がありますあなたのコンピュータが再び応答した場合。 Test-Connectionは、pingの役割をカバーするコマンドレットです。

+0

私はあなたが私に何を削除したいのかよく分かりません。コードのその部分を削除してこれを追加すると、無効な表現ができてしまいます。 – CharlesP

+0

だから、JPBlancはスクリプト全体を少し修正しました。これはあなたが思うところで私はまだそれをテストする必要があります。 – CharlesP

+0

だから、JPBlancの設定は今やスクリプトの前半がうまくいきます。それから私は正しい認証ボックスを取得するが、正しくシーケンスを終了しない私はキャンセルを押すと、資格情報のない認証のためのプロンプトのプロセスの間にピングプロセス中に。スクリプトが最初の休憩をして、そこでは正しく機能しなくなりました。 – CharlesP

関連する問題