Guest User

OSDComputerName

a guest
Apr 24th, 2015
150
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Add-Type -AssemblyName System.Windows.Forms
  2.  
  3. # show the MsgBox:
  4. $result = [System.Windows.Forms.MessageBox]::Show('Would you like to rename this computer', 'Warning', 'YesNo', 'Warning')
  5.  
  6. # check the result:
  7. if ($result -eq 'Yes')
  8. {
  9. Load-Form
  10. }
  11. else
  12. {
  13. Exit
  14. }
  15. Function Load-Form
  16. {
  17. $Form.Controls.Add($TBComputerName)
  18. $Form.Controls.Add($GBComputerName)
  19. $Form.Controls.Add($ButtonOK)
  20. $Form.Add_Shown({$Form.Activate()})
  21. [void] $Form.ShowDialog()
  22. }
  23.  
  24. Function Set-OSDComputerName
  25. {
  26. $ErrorProvider.Clear()
  27. if ($TBComputerName.Text.Length -eq 0)
  28. {
  29. $ErrorProvider.SetError($GBComputerName, "Please enter a computer name.")
  30. }
  31.  
  32. elseif ($TBComputerName.Text.Length -gt 15)
  33. {
  34. $ErrorProvider.SetError($GBComputerName, "Computer name cannot be more than 15 characters.")
  35. }
  36.  
  37. #Validation Rule for computer names.
  38. elseif ($TBComputerName.Text -match "^[-_]|[^a-zA-Z0-9-_]")
  39. {
  40. $ErrorProvider.SetError($GBComputerName, "Computer name invalid, please correct the computer name.")
  41. }
  42.  
  43. else
  44. {
  45. $OSDComputerName = $TBComputerName.Text.ToUpper()
  46. $TSEnv = New-Object -COMObject Microsoft.SMS.TSEnvironment
  47. $TSEnv.Value("OSDComputerName") = "$($OSDComputerName)"
  48. $Form.Close()
  49. }
  50. }
  51.  
  52. [void][System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
  53. [void][System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
  54.  
  55. $Global:ErrorProvider = New-Object System.Windows.Forms.ErrorProvider
  56.  
  57. $Form = New-Object System.Windows.Forms.Form
  58. $Form.Size = New-Object System.Drawing.Size(285,140)
  59. $Form.MinimumSize = New-Object System.Drawing.Size(285,140)
  60. $Form.MaximumSize = New-Object System.Drawing.Size(285,140)
  61. $Form.StartPosition = "CenterScreen"
  62. $Form.SizeGripStyle = "Hide"
  63. $Form.Text = "Enter Computer Name"
  64. $Form.ControlBox = $false
  65. $Form.TopMost = $true
  66.  
  67. $TBComputerName = New-Object System.Windows.Forms.TextBox
  68. $TBComputerName.Location = New-Object System.Drawing.Size(25,30)
  69. $TBComputerName.Size = New-Object System.Drawing.Size(215,50)
  70. $TBComputerName.TabIndex = "1"
  71.  
  72. $GBComputerName = New-Object System.Windows.Forms.GroupBox
  73. $GBComputerName.Location = New-Object System.Drawing.Size(20,10)
  74. $GBComputerName.Size = New-Object System.Drawing.Size(225,50)
  75. $GBComputerName.Text = "Computer name:"
  76.  
  77. $ButtonOK = New-Object System.Windows.Forms.Button
  78. $ButtonOK.Location = New-Object System.Drawing.Size(195,70)
  79. $ButtonOK.Size = New-Object System.Drawing.Size(50,20)
  80. $ButtonOK.Text = "OK"
  81. $ButtonOK.TabIndex = "2"
  82. $ButtonOK.Add_Click({Set-OSDComputerName})
  83.  
  84. $Form.KeyPreview = $True
  85. $Form.Add_KeyDown({if ($_.KeyCode -eq "Enter"){Set-OSDComputerName}})
RAW Paste Data