Advertisement
Guest User

ZTIMoveComputer.ps1

a guest
Jun 7th, 2016
725
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Import-Module Z:\Tools\Modules\ZTIUtility\ZTIUtility.psm1
  2.  
  3. [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
  4. $Message = ""
  5.  
  6. If ((get-wmiobject -class Win32_ComputerSystem).DomainRole -match '3|2|4|5'){
  7.     #For Windows Servers
  8.     if ((Get-Module -Name ActiveDirectory -ListAvailable).Name -ne "ActiveDirectory") {
  9.         Install-WindowsFeature -name RSAT-AD-PowerShell
  10.     }
  11. }elseif ((get-wmiobject -class Win32_OperatingSystem).version -match '(^6\.1)') {
  12.     #For Windows Clients
  13.     if ((get-wmiobject -class Win32_QuickFixEngineering).HotfixID -match 'KB958830'){
  14.         & dism /online /enable-feature /featurename:RemoteServerAdministrationTools /featurename:RemoteServerAdministrationTools-Roles /featurename:RemoteServerAdministrationTools-Roles-AD /featurename:RemoteServerAdministrationTools-Roles-AD-Powershell
  15.     }else{
  16.         $Message = "Computer Object Move Paused: You must manually install Powershell's Active Directory Modules - Typically found in RSAT installer"
  17.         [System.Windows.Forms.MessageBox]::Show($Message)
  18.     }
  19. }elseif ((get-wmiobject -class Win32_OperatingSystem).version -match '(^10\.)') {
  20.     if ((get-wmiobject -class Win32_QuickFixEngineering).HotfixID -match 'KB2693643'){
  21.         & dism /online /enable-feature /featurename:RSATClient /featurename:RSATClient-Roles /featurename:RSATClient-Features /featurename:RSATClient-Roles-AD /featurename:RSATClient-Roles-AD-DS /featurename:RSATClient-Roles-AD-DS-SnapIns /featurename:RSATClient-Roles-AD-Powershell
  22.     }else{
  23.         $Message = "Computer Object Move Paused: You must manually install Powershell's Active Directory Modules - Typically found in RSAT installer"
  24.         [System.Windows.Forms.MessageBox]::Show($Message)
  25.     }
  26. }else{
  27.     $Message = "Computer Object Move Paused: You must manually install Powershell's Active Directory Modules - Typically found in RSAT installer"
  28.     [System.Windows.Forms.MessageBox]::Show($Message)
  29. }
  30. #Workflow:
  31. #DomainJoinMachineObjectOU=<Set in custemsettings as a custom variable>
  32. #Set $DestinationMachineObjectOU to $MachineObjectOU in the task sequence using Set Task Sequence Variable
  33. #Set $DomainObjectOU to $DomainJoinMachineObjectOU in the task sequence using Set Task Sequence Variable (this is done so the machine joins to the Deployment OU)
  34. #After the last time your machine will reboot move the machine to $DestinationMachineObjectOU
  35.  
  36. #Load the AD Powershell module so that we can move the machine OU
  37. #Import-Module ActiveDirectory
  38.  
  39. #function borrowed from http://gallery.technet.microsoft.com/scriptcenter/Powershell-script-to-33887eb2#content
  40. function ConvertFrom-Base64($stringfrom) {
  41.     $bytesfrom  = [System.Convert]::FromBase64String($stringfrom);
  42.     $decodedfrom = [System.Text.Encoding]::UTF8.GetString($bytesfrom);
  43.     return $decodedfrom  
  44. }
  45.  
  46. # Grab the variables from the Task Sequence
  47.  
  48. $tsenv = New-Object -COMObject Microsoft.SMS.TSEnvironment
  49. $tsenv.GetVariables() | % { Set-Variable -Name "$_" -Value "$($tsenv.Value($_))" }
  50. #Set Credentials to Task Sequence variable values
  51. $ClearID = ConvertFrom-Base64 -stringfrom "$UserID"
  52. $ClearDomain = ConvertFrom-Base64 -stringfrom "$UserDomain"
  53. $ClearPW = ConvertFrom-Base64 -stringfrom "$UserPassword"
  54. $User = "$ClearDomain\$ClearID"
  55. $Password = ConvertTo-SecureString -String "$ClearPW" -AsPlainText -Force
  56. $Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User,$Password
  57.  
  58. #Load the AD Powershell module so that we can move the machine OU
  59. Import-Module ActiveDirectory
  60.  
  61. Write-Host "Moving (Get-WmiObject Win32_ComputerSystem).Name to $MachineObjectOU"
  62.  
  63. #Force the machine into the Destination OU.
  64. Get-ADComputer -Identity (Get-WmiObject Win32_ComputerSystem).Name -Credential $Credential | Move-ADObject -TargetPath "$MachineObjectOU" -Credential $Credential -Verbose
  65.  
  66. Exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement