Advertisement
Guest User

M@

a guest
Feb 6th, 2017
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #region FORM LOAD
  2. $MainForm_Load = {
  3.     $isadmin = isadmin
  4.     IF ($isadmin -eq $false)
  5.     {
  6.         $is = Invoke-PopUp -Title 'Error!' -Message 'Current user must be an Administrator.  You may also right click this executable, and select "Run As Administrator." The script will now exit.' -IconType Error -ButtonType OK
  7.         IF ($is -eq 'OK')
  8.         {
  9.             EXIT;
  10.         }      
  11.     }
  12.     FOREACH ($U in (Get-LocalUsers)) { $cbLocalAccount.Items.Add($U) }
  13. }
  14. #endregion FORM LOAD
  15.  
  16. #region GLOBAL VARIABLES
  17.  
  18. #endregion
  19.  
  20. #region FORM FUNCTIONS
  21. function isadmin
  22. {
  23.     # Returns true/false
  24.     ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")
  25. }
  26.  
  27. FUNCTION Invoke-PopUp
  28. {
  29.     [CMDLETBINDING()]
  30.     PARAM (
  31.         [Parameter(Mandatory = $true, Position = 0)]
  32.         [string]$Title,
  33.         [Parameter(Mandatory = $true, Position = 1)]
  34.         [string]$Message,
  35.         [PARAMETER(Mandatory = $true, Position = 2)]
  36.         [ValidateSet(
  37.                      'Asterisk',
  38.                      'Error',
  39.                      'Exclamation',
  40.                      'Hand',
  41.                      'Information',
  42.                      'None',
  43.                      'Question',
  44.                      'Stop',
  45.                      'Warning')]
  46.         [string]$IconType,
  47.         [PARAMETER(Mandatory = $false, Position = 3)]
  48.         [ValidateSet(
  49.                      'AbortRetryIgnore',
  50.                      'OK',
  51.                      'OKCancel',
  52.                      'RetryCancel',
  53.                      'YesNo',
  54.                      'YesNoCancel'
  55.         )]
  56.         [string]$ButtonType
  57.     )
  58.    
  59.     [void][System.Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms')
  60.     $m = [Windows.Forms.MessageBox]::Show($Message, $Title, [Windows.Forms.MessageBoxButtons]::$ButtonType, [Windows.Forms.MessageBoxIcon]::$IconType)
  61.     return $m
  62. }
  63.  
  64. FUNCTION Add-Logs ($Text)
  65. {
  66.     $Date = Get-Date
  67.     $TextValue = "$Date | $Text`r`n"
  68.     $rtbLogs.Text += $TextValue
  69. }
  70.  
  71. FUNCTION Get-LocalUsers
  72. {
  73.     $a = NET USER | Select-Object -skip 4
  74.     $a = $a | Select-Object -First ($a.Count - 2)
  75.     $a = $a.Trim()
  76.     $a -split '\s{2,}'
  77. }
  78.  
  79. FUNCTION Set-AutoLogon
  80. {
  81.     [CmdletBinding()]
  82.     Param (
  83.         [Parameter(Mandatory = $True, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
  84.         [String[]]$DefaultUsername,
  85.         [Parameter(Mandatory = $True, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
  86.         [String[]]$DefaultPassword,
  87.         [Parameter(Mandatory = $False, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
  88.         [AllowEmptyString()]
  89.         [string]$DefaultDomain,
  90.         [Parameter(Mandatory = $False, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
  91.         [AllowEmptyString()]
  92.         [string]$RunAsUsername,
  93.         [Parameter(Mandatory = $False, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
  94.         [AllowEmptyString()]
  95.         [string]$RunAsPassword,
  96.         [Parameter(Mandatory = $False, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
  97.         [AllowEmptyString()]
  98.         [String[]]$AutoLogonCount,
  99.         [Parameter(Mandatory = $False, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
  100.         [AllowEmptyString()]
  101.         [String[]]$Script
  102.     )
  103.    
  104.     BEGIN
  105.     {
  106.         #Registry path declaration
  107.         $RegPath = "[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon]"
  108.         $RegROPath = "[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce]"
  109.         $TheUserName = $DefaultUsername
  110.         $TheUserPass = $DefaultPassword
  111.         $TheDomain = $DefaultDomain
  112.         $TheRunAsUserName = $RunAsUsername
  113.         $TheRunAsUserPass = $RunAsPassword
  114.     }
  115.    
  116.     PROCESS
  117.     {
  118.         TRY
  119.         {
  120.             IF (!$AutoLogonCount) { $AutoLogonCount = 1 }
  121.            
  122.             $AALC = @"
  123. "AutoAdminLogon"="$AutoLogonCount"
  124.  
  125. "@
  126.             $DefDomDefUN = @"
  127. "DefaultUsername"="$DefaultDomain\\$DefaultUsername"                
  128.  
  129. "@
  130.             $DFPW = @"
  131. "DefaultPassword"="$DefaultPassword"
  132.  
  133. "@
  134.             $RunScript = @"
  135. @="$Script"
  136.  
  137. "@
  138.             $header = "Windows Registry Editor Version 5.00`r`n`r`n"
  139.             $header | Out-File -FilePath 'C:\Windows\Temp\LogOn.reg' -Force
  140.             $header | Out-File -FilePath 'C:\Windows\Temp\RunOnce.reg' -Force
  141.             Add-Content -Path 'C:\Windows\Temp\LogOn.reg' -Value "$RegPath" -Force
  142.             Add-Content -Path 'C:\Windows\Temp\LogOn.reg' -Value $AALC -Force
  143.             Add-Content -Path 'C:\Windows\Temp\LogOn.reg' -Value $DefDomDefUN -Force
  144.             Add-Content -Path 'C:\Windows\Temp\LogOn.reg' -Value $DFPW -Force
  145.             Add-Content -Path 'C:\Windows\Temp\RunOnce.reg' -Value "$RegROPath" -Force
  146.            
  147.             IF ($Script) { Add-Content -Path 'C:\Windows\Temp\RunOnce.reg' -Value $RunScript -Force }
  148.             ELSE { Add-Content -Path 'C:\Windows\Temp\RunOnce.reg' -Value '' -Force }
  149.         }
  150.        
  151.         CATCH { Add-Logs "Error setting required registry keys!`r`n`r`n$($_)"; $Global:BlastOff = $false }
  152.         FINALLY
  153.         {
  154.             IF (!$BlastOff)
  155.             {
  156.                 $Uzer = "$TheDomain\$TheRunAsUserName"
  157.                 $null = Runas /noprofile /user:$Uzer "reg.exe import C:\Windows\Temp\LogOn.reg"
  158.                 $null = Runas /noprofile /user:$Uzer "reg.exe import C:\Windows\Temp\RunOnce.reg"
  159.             }
  160.         }
  161.     }
  162.    
  163.     END {<#End#> }
  164. }
  165.  
  166. FUNCTION Set-FirstScript
  167. {
  168.     TRY
  169.     {
  170.         $N0 = @'
  171.                     #----------------------------------------------
  172.                     # Generated Form Function
  173.                     #----------------------------------------------
  174.                     function Show-profmigExportScript1_psf {
  175.  
  176.                     #----------------------------------------------
  177.                     #region Import the Assemblies
  178.                     #----------------------------------------------
  179.                     [void][reflection.assembly]::Load('System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
  180.                     [void][reflection.assembly]::Load('System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
  181.                     [void][reflection.assembly]::Load('System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a')
  182.                     #endregion Import Assemblies
  183.  
  184.                     #----------------------------------------------
  185.                     #region Generated Form Objects
  186.                     #----------------------------------------------
  187.                     [System.Windows.Forms.Application]::EnableVisualStyles()
  188.                     $form1 = New-Object 'System.Windows.Forms.Form'
  189.                     $richtextbox1 = New-Object 'System.Windows.Forms.RichTextBox'
  190.                     $timer1 = New-Object 'System.Windows.Forms.Timer'
  191.                     $InitialFormWindowState = New-Object 'System.Windows.Forms.FormWindowState'
  192.                     #endregion Generated Form Objects
  193.  
  194.                     #----------------------------------------------
  195.                     # User Generated Script
  196.                     #----------------------------------------------
  197.                     FUNCTION Set-AutoLogon
  198.                     {
  199.                         [CmdletBinding()]
  200.                         Param (
  201.                             [Parameter(Mandatory = $True, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
  202.                             [String[]]$DefaultUsername,
  203.                             [Parameter(Mandatory = $True, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
  204.                             [String[]]$DefaultPassword,
  205.                             [Parameter(Mandatory = $False, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
  206.                             [AllowEmptyString()]
  207.                             [string]$DefaultDomain,
  208.                             [Parameter(Mandatory = $False, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
  209.                             [AllowEmptyString()]
  210.                             [String[]]$AutoLogonCount,
  211.                             [Parameter(Mandatory = $False, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
  212.                             [AllowEmptyString()]
  213.                             [String[]]$Script
  214.                         )
  215.                        
  216.                         BEGIN
  217.                         {
  218.                             #Registry path declaration
  219.                             $RegPath = "[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon]"
  220.                             $RegROPath = "[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce]"
  221.                         }
  222.                        
  223.                         PROCESS
  224.                         {
  225.                             TRY
  226.                             {
  227.                         IF (!$AutoLogonCount) { $AutoLogonCount = 1 }
  228.            
  229.                         $AALC = @"
  230. "AutoAdminLogon"="$AutoLogonCount"
  231.  
  232. "@
  233.                         $DefDomDefUN = @"
  234. "DefaultUsername"="$DefaultDomain\\$DefaultUsername"                
  235.  
  236. "@
  237.                         $DFPW = @"
  238. "DefaultPassword"="$DefaultPassword"
  239.  
  240. "@
  241.                         $RunScript = @"
  242. @="$Script"
  243.  
  244. "@
  245.                         $header = "Windows Registry Editor Version 5.00`r`n`r`n"
  246.                         $header | Out-File -FilePath 'C:\Windows\Temp\LogOn.reg' -Force
  247.                         $header | Out-File -FilePath 'C:\Windows\Temp\RunOnce.reg' -Force
  248.                         Add-Content -Path 'C:\Windows\Temp\LogOn.reg' -Value "$RegPath" -Force
  249.                         Add-Content -Path 'C:\Windows\Temp\LogOn.reg' -Value $AALC -Force
  250.                         Add-Content -Path 'C:\Windows\Temp\LogOn.reg' -Value $DefDomDefUN -Force
  251.                         Add-Content -Path 'C:\Windows\Temp\LogOn.reg' -Value $DFPW -Force
  252.                         Add-Content -Path 'C:\Windows\Temp\RunOnce.reg' -Value "$RegROPath" -Force
  253.                        
  254.                         IF ($Script) { Add-Content -Path 'C:\Windows\Temp\RunOnce.reg' -Value $RunScript -Force }
  255.                         ELSE { Add-Content -Path 'C:\Windows\Temp\RunOnce.reg' -Value '' -Force }
  256.                     }
  257.                    
  258.                     CATCH { Add-Logs "Error setting required registry keys!`r`n`r`n$($_)"; $Global:BlastOff = $false }
  259.                     FINALLY
  260.                     {
  261.                         IF (!$BlastOff)
  262.                         {
  263.                             $R1A = "import C:\Windows\Temp\LogOn.reg"
  264.                             $R2A = "import C:\Windows\Temp\RunOnce.reg"
  265.                             Start-Process -FilePath 'C:\Windows\System32\reg.exe' -ArgumentList $R1A
  266.                             Start-Process -FilePath 'C:\Windows\System32\reg.exe' -ArgumentList $R2A
  267.                         }
  268.                     }
  269.                 }
  270.                
  271.                 END {<#End#> }
  272.             }
  273.                 FUNCTION Add-Text ($Text)
  274.                 {
  275.                     $richtextbox1.Text = ''
  276.                     $richtextbox1.Text = 'Waiting 3 minutes to restart local computer.
  277.  
  278. *** YOU MAY PRESS Q TO REBOOT IMMEDIATELY!***'
  279.                     $richtextbox1.Text += ""
  280.                     $richtextbox1.Text += "$Text"
  281.                 }
  282.                    
  283.                 $richtextbox1_KeyPress=[System.Windows.Forms.KeyPressEventHandler]{
  284.                 #Event Argument: $_ = [System.Windows.Forms.KeyPressEventArgs]
  285.                     if ($_.KeyChar -eq 'q' -or $_.KeyChar -eq 'Q')
  286.                         {
  287.                             Add-Text "Rebooting now"
  288.                             Start-Sleep 1
  289.                             Restart-Computer
  290.                         }
  291.                     }
  292.                    
  293.                     $form1_KeyPress=[System.Windows.Forms.KeyPressEventHandler]{
  294.                     #Event Argument: $_ = [System.Windows.Forms.KeyPressEventArgs]
  295.                         if ($_.KeyChar -eq 'q' -or $_.KeyChar -eq 'Q')
  296.                         {
  297.                             Add-Text "Rebooting now"
  298.                             Start-Sleep 1
  299.                             Restart-Computer
  300.                         }
  301.                     }
  302.                    
  303.                     $timer1_Tick={
  304.                         $form1.Refresh()
  305.                         $CT = $stopwatch.elapsed
  306.                         Add-Text $([string]::Format("`rTime: {0:d2}:{1:d2}:{2:d2}",
  307.                                 $CT.hours,
  308.                                 $CT.minutes,
  309.                                 $CT.seconds))
  310.                         IF ($stopwatch.elapsed.minutes -eq 3)
  311.                         {
  312.                             Add-Text 'Restarting computer now...'
  313.                             Restart-Computer   
  314.                         }
  315.                     }
  316.  
  317.                     $form1_Load = {
  318.                         $Global:stopwatch = [System.Diagnostics.Stopwatch]::StartNew()
  319.                         $timer1.Start()
  320.  
  321. '@
  322.         $N0 += @"
  323.                         Set-AutoLogon -DefaultUsername "$($tbServiceCredsUN.Text)" -DefaultPassword "$($mtbServiceCredsPW.Text)" -DefaultDomain "$($tbDomainToJoin.Text)" -Script 'C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe C:\Windows\Temp\S2.ps1 -ExecutionPolicy Unrestricted'
  324.                     }
  325.  
  326. "@
  327.         $N0 += @'          
  328.                 # --End User Generated Script--
  329.                 #----------------------------------------------
  330.                 #region Generated Events
  331.                 #----------------------------------------------
  332.                
  333.                 $Form_StateCorrection_Load=
  334.                 {
  335.                     #Correct the initial state of the form to prevent the .Net maximized form issue
  336.                     $form1.WindowState = $InitialFormWindowState
  337.                 }
  338.                
  339.                 $Form_Cleanup_FormClosed=
  340.                 {
  341.                     #Remove all event handlers from the controls
  342.                     try
  343.                     {
  344.                         $stopwatch.Stop()                      
  345.                         $richtextbox1.remove_KeyPress($richtextbox1_KeyPress)
  346.                         $form1.remove_Load($form1_Load)
  347.                         $form1.remove_KeyPress($form1_KeyPress)
  348.                         $timer1.remove_Tick($timer1_Tick)
  349.                         $form1.remove_Load($Form_StateCorrection_Load)
  350.                         $form1.remove_FormClosed($Form_Cleanup_FormClosed)
  351.                     }
  352.                     catch { Out-Null <# Prevent PSScriptAnalyzer warning #> }
  353.                 }
  354.                 #endregion Generated Events
  355.  
  356.                 #----------------------------------------------
  357.                 #region Generated Form Code
  358.                 #----------------------------------------------
  359.                 $form1.SuspendLayout()
  360.                 #
  361.                 # form1
  362.                 #
  363.                 $form1.Controls.Add($richtextbox1)
  364.                 $form1.AutoScaleDimensions = '6, 13'
  365.                 $form1.AutoScaleMode = 'Font'
  366.                 $form1.ClientSize = '554, 396'
  367.                 $form1.FormBorderStyle = 'None'
  368.                 $form1.MinimizeBox = $False
  369.                 $form1.Name = 'form1'
  370.                 $form1.ShowIcon = $False
  371.                 $form1.StartPosition = 'WindowsDefaultBounds'
  372.                 $form1.Text = 'Form'
  373.                 $form1.WindowState = 'Maximized'
  374.                 $form1.add_Load($form1_Load)
  375.                 $form1.add_KeyPress($form1_KeyPress)
  376.                 #
  377.                 # richtextbox1
  378.                 #
  379.                 $richtextbox1.Anchor = 'Top, Bottom, Left, Right'
  380.                 $richtextbox1.Location = '12, 12'
  381.                 $richtextbox1.Name = 'richtextbox1'
  382.                 $richtextbox1.ReadOnly = $True
  383.                 $richtextbox1.Size = '530, 372'
  384.                 $richtextbox1.TabIndex = 0
  385.                 $richtextbox1.Text = ''
  386.                 $richtextbox1.add_KeyPress($richtextbox1_KeyPress)
  387.                 #
  388.                 # timer1
  389.                 #
  390.                 $timer1.Interval = 1000
  391.                 $timer1.add_Tick($timer1_Tick)
  392.                 $form1.ResumeLayout()
  393.                 #endregion Generated Form Code
  394.  
  395.                 #----------------------------------------------
  396.  
  397.                 #Save the initial state of the form
  398.                 $InitialFormWindowState = $form1.WindowState
  399.                 #Init the OnLoad event to correct the initial state of the form
  400.                 $form1.add_Load($Form_StateCorrection_Load)
  401.                 #Clean up the control events
  402.                 $form1.add_FormClosed($Form_Cleanup_FormClosed)
  403.                 #Show the Form
  404.                 return $form1.ShowDialog()
  405.  
  406.             } #End Function
  407.  
  408.             #Call the form
  409.             Show-profmigExportScript1_psf | Out-Null
  410.  
  411.  
  412. '@
  413.         $N0 | Out-File 'C:\Windows\Temp\S1.ps1' -Force
  414.         Start-Sleep -Milliseconds 200
  415.         $SetS1 = $true
  416.     }
  417.     CATCH
  418.     {
  419.         $IVP = @{
  420.             'Title' = 'Error!'
  421.             'Message' = 'Error exporting Staging Script #1'
  422.             'IconType' = 'Error'
  423.             'ButtonType' = 'OK'
  424.         }
  425.         Invoke-PopUp @IVP
  426.         Add-Logs 'Error exporting Staging Script #1'
  427.         $SetS1 = $false
  428.     }
  429.     FINALLY
  430.     {
  431.         IF ($SetS1 -eq $true)
  432.         {
  433.             Add-Logs 'Successfully exported Staging Script #1.'
  434.         }
  435.     }
  436. }
  437.  
  438. FUNCTION Set-SecondScript
  439. {
  440.     TRY
  441.     {
  442.         $SourceProfile = $env:USERPROFILE
  443.         $DestinationProfile = "C:\Users\$($tbDomainUsernameDEST.Text)\"
  444.        
  445.         $N1 = @'
  446.     #----------------------------------------------
  447.     # Generated Form Function
  448.     #----------------------------------------------
  449.     function Show-profmig_psf {
  450.  
  451.         #----------------------------------------------
  452.         #region Import the Assemblies
  453.         #----------------------------------------------
  454.         [void][reflection.assembly]::Load('System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
  455.         [void][reflection.assembly]::Load('System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
  456.         [void][reflection.assembly]::Load('System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a')
  457.         #endregion Import Assemblies
  458.  
  459.         #----------------------------------------------
  460.         #region Generated Form Objects
  461.         #----------------------------------------------
  462.         [System.Windows.Forms.Application]::EnableVisualStyles()
  463.         $form1 = New-Object 'System.Windows.Forms.Form'
  464.         $richtextbox1 = New-Object 'System.Windows.Forms.RichTextBox'
  465.         $InitialFormWindowState = New-Object 'System.Windows.Forms.FormWindowState'
  466.         #endregion Generated Form Objects
  467.  
  468.         #----------------------------------------------
  469.         # User Generated Script
  470.         #----------------------------------------------
  471.  
  472. '@
  473.         $N1 += @'
  474.         FUNCTION Set-AutoLogon
  475.         {
  476.             [CmdletBinding()]
  477.             Param (
  478.                 [Parameter(Mandatory = $True, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
  479.                 [String[]]$DefaultUsername,
  480.                 [Parameter(Mandatory = $True, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
  481.                 [String[]]$DefaultPassword,
  482.                 [Parameter(Mandatory = $False, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
  483.                 [AllowEmptyString()]
  484.                 [string]$DefaultDomain,
  485.                 [Parameter(Mandatory = $False, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
  486.                 [AllowEmptyString()]
  487.                 [String[]]$AutoLogonCount,
  488.                 [Parameter(Mandatory = $False, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
  489.                 [AllowEmptyString()]
  490.                 [String[]]$Script
  491.             )
  492.            
  493.             BEGIN
  494.             {
  495.                 #Registry path declaration
  496.                 $RegPath = "[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon]"
  497.                 $RegROPath = "[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce]"
  498.             }
  499.            
  500.             PROCESS
  501.             {
  502.                 TRY
  503.                 {
  504.                     IF (!$AutoLogonCount) { $AutoLogonCount = 1 }
  505.                    
  506.                     $AALC = @"
  507. "AutoAdminLogon"="$AutoLogonCount"
  508.  
  509. "@
  510.                     $DefDomDefUN = @"
  511. "DefaultUsername"="$DefaultDomain\\$DefaultUsername"                
  512.  
  513. "@
  514.                     $DFPW = @"
  515. "DefaultPassword"="$DefaultPassword"
  516.  
  517. "@
  518.                     $RunScript = @"
  519. @="$Script"
  520.  
  521. "@
  522.                     $header = "Windows Registry Editor Version 5.00`r`n`r`n"
  523.                     $header | Out-File -FilePath 'C:\Windows\Temp\LogOn.reg' -Force
  524.                     $header | Out-File -FilePath 'C:\Windows\Temp\RunOnce.reg' -Force
  525.                     Add-Content -Path 'C:\Windows\Temp\LogOn.reg' -Value "$RegPath" -Force
  526.                     Add-Content -Path 'C:\Windows\Temp\LogOn.reg' -Value $AALC -Force
  527.                     Add-Content -Path 'C:\Windows\Temp\LogOn.reg' -Value $DefDomDefUN -Force
  528.                     Add-Content -Path 'C:\Windows\Temp\LogOn.reg' -Value $DFPW -Force
  529.                     Add-Content -Path 'C:\Windows\Temp\RunOnce.reg' -Value "$RegROPath" -Force
  530.                    
  531.                     IF ($Script) { Add-Content -Path 'C:\Windows\Temp\RunOnce.reg' -Value $RunScript -Force }
  532.                     ELSE { Add-Content -Path 'C:\Windows\Temp\RunOnce.reg' -Value '' -Force }
  533.                 }
  534.                
  535.                 CATCH { Add-Logs "Error setting required registry keys!`r`n`r`n$($_)"; $Global:BlastOff = $false }
  536.                 FINALLY
  537.                 {
  538.                     IF (!$BlastOff)
  539.                     {
  540.                         $R1A = "import C:\Windows\Temp\LogOn.reg"
  541.                         $R2A = "import C:\Windows\Temp\RunOnce.reg"
  542.                         Start-Process -FilePath 'C:\Windows\System32\reg.exe' -ArgumentList $R1A
  543.                         Start-Process -FilePath 'C:\Windows\System32\reg.exe' -ArgumentList $R2A
  544.                     }
  545.                 }
  546.             }
  547.            
  548.             END {<#End#> }
  549.         }
  550.  
  551.         FUNCTION Invoke-PopUp
  552.         {
  553.             [CMDLETBINDING()]
  554.             PARAM (
  555.                 [Parameter(Mandatory = $true, Position = 0)]
  556.                 [string]$Title,
  557.                 [Parameter(Mandatory = $true, Position = 1)]
  558.                 [string]$Message,
  559.                 [PARAMETER(Mandatory = $true, Position = 2)]
  560.                 [ValidateSet(
  561.                              'Asterisk',
  562.                              'Error',
  563.                              'Exclamation',
  564.                              'Hand',
  565.                              'Information',
  566.                              'None',
  567.                              'Question',
  568.                              'Stop',
  569.                              'Warning')]
  570.                 [string]$IconType,
  571.                 [PARAMETER(Mandatory = $false, Position = 3)]
  572.                 [ValidateSet(
  573.                              'AbortRetryIgnore',
  574.                              'OK',
  575.                              'OKCancel',
  576.                              'RetryCancel',
  577.                              'YesNo',
  578.                              'YesNoCancel'
  579.                 )]
  580.                 [string]$ButtonType
  581.             )
  582.            
  583.             [void][System.Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms')
  584.             $m = [Windows.Forms.MessageBox]::Show($Message, $Title, [Windows.Forms.MessageBoxButtons]::$ButtonType, [Windows.Forms.MessageBoxIcon]::$IconType)
  585.             return $m
  586.         }
  587.  
  588. '@
  589.         $N1 += @'
  590.         FUNCTION Migrate-Profile
  591.         {
  592.             $GCI = @()
  593.  
  594. '@
  595.         $N1 += @"
  596.             Get-ChildItem -Path "$($SourceProfile)" -File | ForEach-Object {
  597.                 Copy-Item $_ -Destination "$($DestinationProfile)" -Recurse -Container -PassThru;
  598.  
  599. "@
  600.         $N1 += @'
  601.                 Write-Host "Processing: $($_.Name)"
  602.             }
  603.  
  604. '@
  605.         $N1 += @"
  606.             Get-ChildItem -Path "$($SourceProfile)" -Directory | ForEach-Object {
  607.  
  608. "@
  609.         $N1 += @'
  610.                 Write-Host "Found Root Directory: $($_.FullName)"
  611.                 $GCI += $_
  612.             }
  613.             $GCI | ForEach-Object {
  614.                     Copy-Item $_ -Destination `
  615.  
  616. '@
  617.         $N1 += @"
  618.                     "$($DestinationProfile)" -Recurse -Container -PassThru | Select-Object -ExpandProperty Fullname | ForEach-Object {
  619.  
  620. "@
  621.         $N1 += @'
  622.                     Write-Host "Processing: "$_
  623.                 }
  624.             }
  625.         }
  626.  
  627.         $richtextbox1_TextChanged={
  628.             $richtextbox1.SelectionStart = $richtextbox1.Text.Length
  629.             $richtextbox1.ScrollToCaret()
  630.         }
  631.  
  632. $form1_Load = {
  633.  
  634. '@
  635.         $N1 += @"
  636.     Set-AutoLogon -DefaultUsername "$($tbDomainUsernameDEST.Text)" -DefaultPassword "$($mtbDomainPasswordDEST.Text)" -DefaultDomain "$($tbDomainToJoin.Text)"
  637.  
  638. "@
  639.         $N1 += @'
  640.     Migrate-Profile
  641.     Invoke-PopUp -Title 'Completed Operation' -Message 'Press OK to finalize process, and boot into destination account profile.' -IconType Informational -ButtonType OK   
  642. }
  643.  
  644. '@
  645.         $N1 += @'
  646.         # --End User Generated Script--
  647.         #----------------------------------------------
  648.         #region Generated Events
  649.         #----------------------------------------------
  650.        
  651.         $Form_StateCorrection_Load=
  652.         {
  653.             #Correct the initial state of the form to prevent the .Net maximized form issue
  654.             $form1.WindowState = $InitialFormWindowState
  655.         }
  656.        
  657.         $Form_Cleanup_FormClosed=
  658.         {
  659.             #Remove all event handlers from the controls
  660.             try
  661.             {
  662.                 $form1.remove_Load($form1_Load)
  663.                 $form1.remove_Load($Form_StateCorrection_Load)
  664.                 $form1.remove_FormClosed($Form_Cleanup_FormClosed)
  665.             }
  666.             catch { Out-Null <# Prevent PSScriptAnalyzer warning #> }
  667.         }
  668.         #endregion Generated Events
  669.  
  670.         #----------------------------------------------
  671.         #region Generated Form Code
  672.         #----------------------------------------------
  673.         $form1.SuspendLayout()
  674.         #
  675.         # form1
  676.         #
  677.         $form1.Controls.Add($richtextbox1)
  678.         $form1.AutoScaleDimensions = '6, 13'
  679.         $form1.AutoScaleMode = 'Font'
  680.         $form1.ClientSize = '675, 533'
  681.         $form1.FormBorderStyle = 'None'
  682.         $form1.MinimizeBox = $False
  683.         $form1.Name = 'form1'
  684.         $form1.ShowIcon = $False
  685.         $form1.ShowInTaskbar = $False
  686.         $form1.StartPosition = 'WindowsDefaultBounds'
  687.         $form1.Text = 'Form'
  688.         $form1.WindowState = 'Maximized'
  689.         $form1.add_Load($form1_Load)
  690.         #
  691.         # richtextbox1
  692.         #
  693.         $richtextbox1.Location = '13, 12'
  694.         $richtextbox1.Anchor = 'Top, Bottom, Left, Right'
  695.         $richtextbox1.Name = 'richtextbox1'
  696.         $richtextbox1.ReadOnly = $True
  697.         $richtextbox1.Size = '650, 509'
  698.         $richtextbox1.TabIndex = 0
  699.         $richtextbox1.Text = ''
  700.         $form1.ResumeLayout()
  701.         #endregion Generated Form Code
  702.  
  703.         #----------------------------------------------
  704.  
  705.         #Save the initial state of the form
  706.         $InitialFormWindowState = $form1.WindowState
  707.         #Init the OnLoad event to correct the initial state of the form
  708.         $form1.add_Load($Form_StateCorrection_Load)
  709.         #Clean up the control events
  710.         $form1.add_FormClosed($Form_Cleanup_FormClosed)
  711.         #Show the Form
  712.         return $form1.ShowDialog()
  713.  
  714.     } #End Function
  715.  
  716.     #Call the form
  717.     Show-profmig_psf | Out-Null
  718.  
  719. '@
  720.         $N1 | Out-File -FilePath "C:\Windows\Temp\S2.ps1" -Force
  721.         $SetAL2 = $true
  722.     }
  723.     CATCH
  724.     {
  725.         $ILP = @{
  726.             'Title' = 'Error!'
  727.             'Message' = "Error exporting Staging Script #2 $($_)"
  728.             'IconType' = 'Error'
  729.             'ButtonType' = 'OK'
  730.         }
  731.         Invoke-PopUp @ILP
  732.         Add-Logs 'Error exporting Staging Script #2'
  733.         $SetAL2 = $false
  734.     }
  735.     FINALLY
  736.     {
  737.         IF ($SetAL2 -eq $true)
  738.         {
  739.             Add-Logs 'Successfully exported Staging Script #2.'
  740.         }
  741.     }
  742. }
  743. #endregion FORM FUNCTIONS
  744.  
  745. #region FORM MAIN
  746. $btnOK_Click = {
  747.     IF (!$BlastOff) { $Script:BlastOff = 'Go' }
  748.    
  749.     $MTB = [System.Windows.Forms.MaskedTextBox]
  750.     $RTB = [System.Windows.Forms.TextBox]
  751.     $RCB = [System.Windows.Forms.ComboBox]
  752.     $RGB = [System.Windows.Forms.GroupBox]
  753.     FOREACH ($RGB in $MainForm.Controls)
  754.     {
  755.         FOREACH ($MTB in $RGB.Controls)
  756.         {
  757.             IF ($MTB.Text -eq $null -or $MTB.Text -eq "") { $BlastOff = $false }
  758.         }
  759.         FOREACH ($RTB in $RGB.Controls)
  760.         {
  761.             IF ($RTB.Text -eq $null -or $RTB.Text -eq "") { $BlastOff = $false }
  762.         }
  763.         FOREACH ($RCB in $RGB.Controls)
  764.         {
  765.             IF ($RCB.Text -eq $null -or $RCB.Text -eq "") { $BlastOff = $false }
  766.         }
  767.     }
  768.     IF ($BlastOff -eq $false)
  769.     {
  770.         $IPP = @{
  771.             'Title' = 'Error!'
  772.             'Message' = 'Please fill out empty fields.'
  773.             'IconType' = 'Exclamation'
  774.             'ButtonType' = 'OK'
  775.         }
  776.         Invoke-PopUp @IPP
  777.         Add-Logs 'Please fill out empty fields'
  778.     }
  779.     IF ($BlastOff -eq 'Go')
  780.     {
  781.         TRY
  782.         {
  783.             [string]$Global:DOMA = $tbDomainToJoin.Text
  784.             [string]$Global:PTUN = $tbServiceCredsUN.Text
  785.             [string]$Global:PassPass = $mtbServiceCredsPW.Text
  786.             $Global:FinalPW = $PassPass | ConvertTo-SecureString -AsPlainText -Force
  787.             [string]$Global:FinalUN = "$DOMA\$PTUN"
  788.             $Global:svcCrds = New-Object System.Management.Automation.PSCredential($FinalUN, $FinalPW)
  789.            
  790.             Start-Sleep -Milliseconds 200
  791.             Add-Logs 'Exporting required files...'
  792.             Set-FirstScript
  793.             Start-Sleep -Milliseconds 200
  794.             Set-SecondScript
  795.             $SIPsplat = @{
  796.                 'Path' = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System"
  797.                 'Name' = 'EnableFirstLogonAnimation'
  798.                 'Type' = 'DWORD'
  799.                 'Value' = 0
  800.                 'Force' = $true
  801.             }
  802.             Set-ItemProperty @SIPsplat | Out-Null
  803.             Start-Sleep -Milliseconds 200
  804.             Add-Logs 'Finished exporting required files...'
  805.             Start-Sleep -Milliseconds 100
  806.             Add-Logs 'Configuring auto logon for next boot..'
  807.             Set-AutoLogon -DefaultUsername $($tbDomainUsernameDEST.Text) -DefaultPassword $($mtbDomainPasswordDEST.Text) -DefaultDomain $($tbDomainToJoin.Text) -AutoLogonCount 1 -Script 'C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe C:\Windows\Temp\S1.ps1 -ExecutionPolicy Unrestricted'
  808.             Start-Sleep -Milliseconds 200
  809.             Add-Logs 'Joining machine to domain...'
  810.             Add-Computer -DomainName $($tbDomainToJoin.Text) -Credential $svcCrds -ErrorAction Stop -WarningAction SilentlyContinue -ErrorVariable ErrorJoiningMachineToDomain
  811.             IF (!$ErrorJoiningMachineToDomain) { $cont = $true; Add-Logs 'Successfully joined domain!' }
  812.         }
  813.        
  814.         CATCH
  815.         {
  816.             Invoke-PopUp -Title 'Error!' -Message "Encountered Error!`r`n`r`n$($_)" -IconType Error -ButtonType OK
  817.             Add-Logs 'Error joining localhost to domain!'
  818.             $cont = $false
  819.         }
  820.        
  821.         FINALLY
  822.         {
  823.             IF ($cont -eq $true)
  824.             {
  825.                 $reboot = Invoke-PopUp -Title 'Warning' -Message 'Please select OK to reboot.' -IconType Information -ButtonType OKCancel
  826.                 SWITCH ($reboot)
  827.                 {
  828.                     'OK'         { Restart-Computer }
  829.                     'Cancel'     { <# DO NOTHING #> }
  830.                 }
  831.             }
  832.         }
  833.     }
  834. }
  835.  
  836. $rtbLogs_TextChanged = {
  837.     $rtbLogs.SelectionStart = $rtbLogs.Text.Length
  838.     $rtbLogs.ScrollToCaret()
  839. }
  840.  
  841. $tbDomainUsernameDEST_TextChanged = {
  842.     IF ($tbDomainUsernameDEST.Text -like "*\*")
  843.     {
  844.         $b = Invoke-PopUp -Title 'Stop' -Message "Only username is needed, do not enter a domain. `r`n| GOOD = john.smith |`r`n| BAD = domain\john.smith |" -IconType Stop -ButtonType OK
  845.         Add-Logs 'Only username is needed, do not enter a domain.'
  846.         SWITCH ($b) { 'OK' { $tbDomainUsernameDEST.Text = "" } }
  847.     }
  848. }
  849.  
  850. $tbServiceCredsUN_TextChanged = {
  851.     IF ($tbServiceCredsUN.Text -like "*\*")
  852.     {
  853.         $g = Invoke-PopUp -Title 'Stop' -Message "Only username is needed, do not enter a domain. `r`n| GOOD = john.smith |`r`n| BAD = domain\john.smith |" -IconType Stop -ButtonType OK
  854.         Add-Logs 'Only username is needed, do not enter a domain.'
  855.         SWITCH ($g) { 'OK' { $tbServiceCredsUN.Text = "" } }
  856.     }
  857. }
  858. #endregion FORM MAIN
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement