Quantcast
Channel: Configuration Manager 2012 - Operating System Deployment forum
Viewing all articles
Browse latest Browse all 9126

Task Sequence to Run powershell script to join computer to domain and rename

$
0
0

Should I run a powershell script in an OSD task sequence by using a "Command" (cmd /c powershell.exe etc..) or what's the best way to do it? 

This is the code I'm trying to implement in order to join a computer to the domain and rename it on the domain. In the event the computer object already exist in the domain, I'm hoping this function switch would allow it to still join using an existing AD computer object name. 
Unless there is a better way without using netdom?

My task sequence fails when I try running a "command" task that runs cmd /c powershell.exe and unc path to this ps1 file containing this code. So I'm not sure if the code sucks or if my OSD task method sucks. 

$comp=gwmi win32_computersystem -Authentication 6
$username = "joindomain@domain.com"
$password = "ThePassword"
$secstr = New-Object -TypeName System.Security.SecureString
$password.ToCharArray() | ForEach-Object {$secstr.AppendChar($_)}


gwmi Win32_Bios | % { $newname = "Prefix-" + $_.SerialNumber }
$ComputerName = $newname
$domain="domain.com"


Function JoinDOMAIN {
 $DomainJoin = 1
 $CreateAccount = 2
 $AllowJoinIfAlreadyJoined = 32
 $computer = get-wmiobject Win32_ComputerSystem
 $ret = $computer.JoinDomainOrWorkGroup($domain,$cred.getnetworkcredential().password,$cred.username,$null,$DomainJoin+$CreateAccount+$AllowJoinIfAlreadyJoined)
 $comp.rename($ComputerName,$cred.getnetworkcredential().password,$cred.username)
 $ret = $ret.ReturnValue

 Switch ($ret) {
 2224 {
  $ret = $computer.JoinDomainOrWorkGroup($domain,$cred.getnetworkcredential().password,$cred.username,$null,33)
  $ret = $ret.ReturnValue
  $comp.rename($ComputerName,$cred.getnetworkcredential().password,$cred.username)
  }
 }
 
 return $ret
}


Viewing all articles
Browse latest Browse all 9126

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>