Skip to content

Instantly share code, notes, and snippets.

@stratalux
Created April 15, 2011 17:36
Show Gist options
  • Save stratalux/922096 to your computer and use it in GitHub Desktop.
Save stratalux/922096 to your computer and use it in GitHub Desktop.
Join Domain with Chef
#
# Cookbook Name:: development
# Recipe:: renameComputer
#
# Copyright 2011, YOUR_COMPANY_NAME
#
# All rights reserved - Do Not Redistribute
#
powershell "Join Active Directory Domain" do
parameters({'DOMAIN_ADMIN' => @node[:development][:DOMAIN_ADMIN]})
parameters({'DOMAIN' => @node[:development][:DOMAIN]})
parameters({'DOMAIN_ADMIN_PASSWORD' => @node[:development][:DOMAIN_ADMIN_PASSWORD]})
powershell_script = <<'POWERSHELL_SCRIPT'
$username = "$env:DOMAIN_ADMIN";
$domain = "$env:DOMAIN";
$password = "$env:DOMAIN_ADMIN_PASSWORD";
$message="";
function addComputer
{
param([string]$username, [string]$password, [string]$domain)
try
{
add-computer -domain $domain -credential (New-Object System.Management.Automation.PSCredential ($username, (ConvertTo-SecureString $password -AsPlainText -Force))) -passthru -verbose
}
catch
{
$message += "An error occcured while joining computer to domain." + "`n`rError: $_";
write-host "Error" $message
}
}
addComputer $username $password $domain
Restart-Computer
POWERSHELL_SCRIPT
source(powershell_script)
end
@stratalux
Copy link
Author


RS> Converging ***
*RS> Run list: development::joinDomain
*RS> Starting at Fri Apr 15 18:33:02 +0100 2011
18:33:02: Starting Chef Solo Run
18:33:07: Replacing the run_list with ["development::joinDomain"] from JSON
18:33:08: Running "Join Active Directory Domain"
18:33:09: VERBOSE: Performing operation "Add-Computer" on Target "This command will
change computer localhost to domain or workgroup. Do you wish to continue?".
18:33:12: HasSucceeded ComputerName


False CLIENT1
18:33:13: Add-Computer : This command cannot be executed on target computer('CLIENT1') du
e to following error: The format of the specified domain name is invalid.
At C:\Users\RIGHTS~1\AppData\Local\Temp\chef-powershell-06D9AC00-8D64-4213-A46A
-611FBAFB4426\powershell_provider_source.ps1:12 char:16

  •   add-computer <<<<  -domain $domain -credential (New-Object System.Manag
    
    ement.Automation.PSCredential ($username, (ConvertTo-SecureString $password -As
    PlainText -Force))) -passthru -verbose
    • CategoryInfo : InvalidOperation: (CLIENT1:String) [Add-Computer
      ], InvalidOperationException
    • FullyQualifiedErrorId : InvalidOperationException,Microsoft.PowerShell.C
      ommands.AddComputerCommand
      18:33:13: WARNING: Script exited successfully but $Error contained 1 error(s).
      18:33:13: Script duration: 4.415
      *ERROR> Chef converge failed
      *ERROR> An external command returned an error during the execution of Chef:

"powershell -command "&{set-executionpolicy -executionPolicy RemoteSigned -Scope Process; import-module C:\PROGRA1\RIGHTS1\SandBox\RIGHT_1\chef\lib\windows\bin\ChefNodeCmdlet.dll; &C:/Users/RIGHTS1/AppData/Local/Temp/chef-powershell-06D9AC00-8D64-4213-A46A-611FBAFB4426/powershell_provider_source.ps1; if ($NULL -eq $LastExitCode) { $LastExitCode = 0 }; if ((0 -eq $LastExitCode) -and ($Error.Count -gt 0)) { $RS_message = 'Script exited successfully but $Error contained '+($Error.Count)+' error(s).'; Write-warning $RS_message; $LastExitCode = 1 }; exit $LastExitCode}"" returned 1, expected 0.

The command was run from "C:/Users/RIGHTS~1/AppData/Local/Temp"
*ERROR> Chef process failure
*ERROR> Chef process failed with return code 1
*RS> failed: development::joinDomain

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment