diff --git a/README.md b/README.md index 3cb2c0a..2d0f3cb 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,7 @@ # OSD -Powershell script using windows forms to prompt for computer name during OSD deployment in PE. +Powershell scripts for OSD deployment in PE. Presents simple and clean UI for naming a PC Requires powershell feature added to WinPE. + + +![alt text](https://1.bp.blogspot.com/-Ux-u5XxNGXE/WbajKn28lfI/AAAAAAAAJko/rB2X5Cq7f-stOUXp_L8Mrdaz-lMAQVCHgCLcBGAs/s1600/OSDComputerName.png) diff --git a/Set-OSDComputerName.ps1 b/Set-OSDComputerName.ps1 index 17d1139..f966f09 100644 --- a/Set-OSDComputerName.ps1 +++ b/Set-OSDComputerName.ps1 @@ -9,11 +9,11 @@ $BIOSInfo = get-wmiobject Win32_BIOS [string]$SerialNumber = $BIOSInfo.SerialNumber [boolean]$Is64Bit = [boolean]((Get-WmiObject -Class 'Win32_Processor' | Where-Object { $_.DeviceID -eq 'CPU0' } | Select-Object -ExpandProperty 'AddressWidth') -eq 64) -If ($Is64Bit) { [string]$envOSArchitecture = '64-bit' } Else { [string]$envOSArchitecture = '32-bit' } - - -Function Load-Form -{ +If ($Is64Bit) { [string]$envOSArchitecture = '64-bit' } Else { [string]$envOSArchitecture = '32-bit' } + + +Function Load-Form +{ $Form.Controls.AddRange(@( $LabelManufacturer, $LabelModel, @@ -28,15 +28,15 @@ Function Load-Form $TBComputerName, $GBComputerName, $ButtonOK - )) - #$Form.Add_Shown({Retrieve-SystemInfo -DisplayType "Basic" -DisplayOutbox -IgnorePing}) - $Form.Add_Shown({Get-SystemImage}) - $Form.Add_Shown({$Form.Activate()}) - [void] $Form.ShowDialog() - -} - -Function Get-SystemImage{ + )) + #$Form.Add_Shown({Retrieve-SystemInfo -DisplayType "Basic" -DisplayOutbox -IgnorePing}) + $Form.Add_Shown({Get-SystemImage}) + $Form.Add_Shown({$Form.Activate()}) + [void] $Form.ShowDialog() + +} + +Function Get-SystemImage{ $system = Get-WMIObject -class Win32_systemenclosure $type = $system.chassistypes @@ -67,67 +67,67 @@ Function Get-SystemImage{ "23" {$type = "Rack Mount Chassis";$PBDELLCHASSIS.Image = $Null} "24" {$type = "Sealed-Case PC";$PBDELLCHASSIS.Image = $Null} Default {$type = "Unknown";$PBDELLCHASSIS.Image = $Null} - } - $LabelModel.Visible = $true -} - -Function Get-OSDComputerName{ - If ($ComputerName.StartsWith('MININT')) { - If ($serialNumber.StartsWith('System')){ - return "[Unit]-[identifier]" - } - Else{ - return "[Unit]-" + $serialNumber - } - } - ElseIf ($ComputerName.StartsWith('MINWINPC')) { - If ($serialNumber.StartsWith('System')){ - return "[Unit]-[identifier]" - } - Else{ - return "[Unit]-" + $serialNumber - } - } - Else { - return $ComputerName - } -} -Function Set-OSDComputerName -{ - $ErrorProvider.Clear() - $ErrorProvider.SetIconPadding($GBComputerName, 10) - $ErrorProvider.SetIconAlignment($GBComputerName, "MiddleRight") - - if ($TBComputerName.Text.Length -eq 0) - { - $ErrorProvider.SetError($GBComputerName, "Please enter a computer name.") - } - - elseif ($TBComputerName.Text.Length -gt 15) - { - $ErrorProvider.SetError($GBComputerName, "Computer name cannot be more than 15 characters.") - } - - elseif ($TBComputerName.Text -eq $serialNumber) - { - $ErrorProvider.SetError($GBComputerName, "Computer name cannot be just the serial number.") - } - - #Validation Rule for computer names. - elseif ($TBComputerName.Text -match "^[-_]|[^a-zA-Z0-9-_]") - { - $ErrorProvider.SetError($GBComputerName, "Computer name invalid, please correct the computer name.") - } - - else - { - $OSDComputerName = $TBComputerName.Text.ToUpper() - $TSEnv = New-Object -COMObject Microsoft.SMS.TSEnvironment - $TSEnv.Value("OSDComputerName") = "$($OSDComputerName)" - $Form.Close() - } -} - + } + $LabelModel.Visible = $true +} + +Function Get-OSDComputerName{ + If ($ComputerName.StartsWith('MININT')) { + If ($serialNumber.StartsWith('System')){ + return "[Unit]-[identifier]" + } + Else{ + return "[Unit]-" + $serialNumber + } + } + ElseIf ($ComputerName.StartsWith('MINWINPC')) { + If ($serialNumber.StartsWith('System')){ + return "[Unit]-[identifier]" + } + Else{ + return "[Unit]-" + $serialNumber + } + } + Else { + return $ComputerName + } +} +Function Set-OSDComputerName +{ + $ErrorProvider.Clear() + $ErrorProvider.SetIconPadding($GBComputerName, 10) + $ErrorProvider.SetIconAlignment($GBComputerName, "MiddleRight") + + if ($TBComputerName.Text.Length -eq 0) + { + $ErrorProvider.SetError($GBComputerName, "Please enter a computer name.") + } + + elseif ($TBComputerName.Text.Length -gt 15) + { + $ErrorProvider.SetError($GBComputerName, "Computer name cannot be more than 15 characters.") + } + + elseif ($TBComputerName.Text -eq $serialNumber) + { + $ErrorProvider.SetError($GBComputerName, "Computer name cannot be just the serial number.") + } + + #Validation Rule for computer names. + elseif ($TBComputerName.Text -match "^[-_]|[^a-zA-Z0-9-_]") + { + $ErrorProvider.SetError($GBComputerName, "Computer name invalid, please correct the computer name.") + } + + else + { + $OSDComputerName = $TBComputerName.Text.ToUpper() + $TSEnv = New-Object -COMObject Microsoft.SMS.TSEnvironment + $TSEnv.Value("OSDComputerName") = "$($OSDComputerName)" + $Form.Close() + } +} + Function Retrieve-SystemInfo <# .SYNOPSIS @@ -353,8 +353,8 @@ Function Retrieve-SystemInfo $data.GetEnumerator() | Sort-Object 'Name' | Format-Table -AutoSize } If($DisplayForm){$data.GetEnumerator() | Sort-Object 'Name' | Out-GridView -Title "$env:ComputerName Information"} -} - +} + function Clear-OutputBox { $OutputBox.ResetText() } @@ -384,31 +384,31 @@ function Write-OutputBox { } } } - - + + ##*============================================= ##* ASSEMBLIES ##*============================================= Add-Type -AssemblyName "System.Drawing" -Add-Type -AssemblyName "System.Windows.Forms" - -$Global:ErrorProvider = New-Object System.Windows.Forms.ErrorProvider - +Add-Type -AssemblyName "System.Windows.Forms" + +$Global:ErrorProvider = New-Object System.Windows.Forms.ErrorProvider + $FormWidth = 805 -$FormHeight = 530 - -$Form = New-Object System.Windows.Forms.Form +$FormHeight = 530 + +$Form = New-Object System.Windows.Forms.Form $Form.Size = New-Object System.Drawing.Size($FormWidth,$FormHeight) $Form.MinimumSize = New-Object System.Drawing.Size($FormWidth,$FormHeight) -$Form.MaximumSize = New-Object System.Drawing.Size($FormWidth,$FormHeight) -$Form.StartPosition = "CenterScreen" -$Form.SizeGripStyle = "Hide" -$Form.Text = "Enter Computer Name" -$Form.ControlBox = $false -$Form.TopMost = $true - -#Pictures +$Form.MaximumSize = New-Object System.Drawing.Size($FormWidth,$FormHeight) +$Form.StartPosition = "CenterScreen" +$Form.SizeGripStyle = "Hide" +$Form.Text = "Enter Computer Name" +$Form.ControlBox = $false +$Form.TopMost = $true + +#Pictures $DesktopBase64String = "iVBORw0KGgoAAAANSUhEUgAAANwAAAChCAYAAACs5tGeAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJ bWFnZVJlYWR5ccllPAAAAw9pVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADw/eHBhY2tldCBiZWdp bj0i77u/IiBpZD0iVzVNME1wQ2VoaUh6cmVTek5UY3prYzlkIj8+IDx4OnhtcG1ldGEgeG1sbnM6 @@ -806,8 +806,8 @@ YII=" $DesktopImageBytes = [Convert]::FromBase64String($DesktopBase64String) $DesktopMemoryStream = New-Object -TypeName IO.MemoryStream($DesktopImageBytes, 0, $DesktopImageBytes.Length) $DesktopMemoryStream.Write($DesktopImageBytes, 0, $DesktopImageBytes.Length) -$DesktopImage = [System.Drawing.Image]::FromStream($DesktopMemoryStream, $true) - +$DesktopImage = [System.Drawing.Image]::FromStream($DesktopMemoryStream, $true) + $LaptopBase64String = "iVBORw0KGgoAAAANSUhEUgAAANwAAAChCAYAAACs5tGeAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJ bWFnZVJlYWR5ccllPAAAAw9pVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADw/eHBhY2tldCBiZWdp bj0i77u/IiBpZD0iVzVNME1wQ2VoaUh6cmVTek5UY3prYzlkIj8+IDx4OnhtcG1ldGEgeG1sbnM6 @@ -1112,13 +1112,13 @@ C+uKj634nMG+ffsqpVRklgi4d8f7CmZjlXQfg+Q/YLArk56O08lafkuib5hQLY4dWQuwxC6SmFo4 nEbU2dCDKhkjMjg2zJiBhAkTwMW2ktxTCZDz74zPu7m6uvpvlpaW6vi1I+AiRYqAixQpUgRcpEgR cJEiRcBFihQpAi5SpAi4SJEiRcBFihQBFylSBFykSJEi4CJFioCLFClSBFykSBFwkSJFioCLFCkC LlKkCLhIkSJFwEWKFAEXKVKkCLhIkSLgIkWKgIsUKVIEXKRIEXCRIkWKgIsUKQIuUqRIEXCRIkXA -RYoUARcpUqQIuEiRIuAiRYoUARcp0v1A/0eAAQCjhIFTZsTX9AAAAABJRU5ErkJggg==" +RYoUARcpUqQIuEiRIuAiRYoUARcp0v1A/0eAAQCjhIFTZsTX9AAAAABJRU5ErkJggg==" $LaptopImageBytes = [Convert]::FromBase64String($LaptopBase64String) $LaptopMemoryStream = New-Object -TypeName IO.MemoryStream($LaptopImageBytes, 0, $LaptopImageBytes.Length) $LaptopMemoryStream.Write($LaptopImageBytes, 0, $LaptopImageBytes.Length) -$LaptopImage = [System.Drawing.Image]::FromStream($LaptopMemoryStream, $true) - - +$LaptopImage = [System.Drawing.Image]::FromStream($LaptopMemoryStream, $true) + + $VMBase64String = "iVBORw0KGgoAAAANSUhEUgAAANwAAAChCAYAAACs5tGeAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJ bWFnZVJlYWR5ccllPAAAAw9pVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADw/eHBhY2tldCBiZWdp bj0i77u/IiBpZD0iVzVNME1wQ2VoaUh6cmVTek5UY3prYzlkIj8+IDx4OnhtcG1ldGEgeG1sbnM6 @@ -1239,73 +1239,73 @@ nreImO1vz58//yF3JoMAXF7U1dV1t6+v749ra2t31dTUHC0rK9svBt4a8VThCxnWPoAMa54s5c+4 cRgyLP75TfF9/LKjo+N/BgcH0escwOVfU1NTVltb23W+4duAAFye9N5778mdcLiX5MtMQHzTFBYT Tk5O0uHDhzGY/N8ZBg4EvTxhWgCCABwEATgIggAcBAE4CIIAHAQBOAgCcBAEATgIAnAQBAE4CAJw EATgIAgCcBAE4CAIAnAQBOAgCAJwEATgIAjAQRAE4CAIwEEQBOAgCMBBEICDIAjAQRCAgyAIwEEQ -gIMgCMBBEICDIAAHQRCAgyAAB0EQgIMgAAdBAA6CoBem/xdgAKqN9AhMwYoOAAAAAElFTkSuQmCC" +gIMgCMBBEICDIAAHQRCAgyAAB0EQgIMgAAdBAA6CoBem/xdgAKqN9AhMwYoOAAAAAElFTkSuQmCC" $VMImageBytes = [Convert]::FromBase64String($VMBase64String) $VMMemoryStream = New-Object -TypeName IO.MemoryStream($VMImageBytes, 0, $VMImageBytes.Length) $VMMemoryStream.Write($VMImageBytes, 0, $VMImageBytes.Length) -$VMImage = [System.Drawing.Image]::FromStream($VMMemoryStream, $true) - +$VMImage = [System.Drawing.Image]::FromStream($VMMemoryStream, $true) + # PictureBoxes $PBDELLCHASSIS = New-Object -TypeName System.Windows.Forms.PictureBox $PBDELLCHASSIS.Location = New-Object -TypeName System.Drawing.Size(38,20) -$PBDELLCHASSIS.Size = New-Object -TypeName System.Drawing.Size(220,160) - +$PBDELLCHASSIS.Size = New-Object -TypeName System.Drawing.Size(220,160) + $LabelManufacturer = New-Object System.Windows.Forms.Label $LabelManufacturer.Location = New-Object System.Drawing.Size(38,210) $LabelManufacturer.Size = New-Object System.Drawing.Size(160,15) -$LabelManufacturer.Text = $Manufacturer - +$LabelManufacturer.Text = $Manufacturer + $LabelModel = New-Object System.Windows.Forms.Label $LabelModel.Location = New-Object System.Drawing.Size(38,235) $LabelModel.Size = New-Object System.Drawing.Size(160,15) -$LabelModel.Text = $Model - +$LabelModel.Text = $Model + $LabelSerial = New-Object System.Windows.Forms.Label $LabelSerial.Location = New-Object System.Drawing.Size(38,260) $LabelSerial.Size = New-Object System.Drawing.Size(160,15) -$LabelSerial.Text = $SerialNumber - +$LabelSerial.Text = $SerialNumber + $GBModel = New-Object System.Windows.Forms.GroupBox $GBModel.Location = New-Object System.Drawing.Size(20,180) -$GBModel.Size = New-Object System.Drawing.Size(220,120) -$GBModel.Font = New-Object System.Drawing.Font("Microsoft Sans Serif","8.25",[System.Drawing.FontStyle]::Bold) -$GBModel.Text = "Model" - +$GBModel.Size = New-Object System.Drawing.Size(220,120) +$GBModel.Font = New-Object System.Drawing.Font("Microsoft Sans Serif","8.25",[System.Drawing.FontStyle]::Bold) +$GBModel.Text = "Model" + $GBSystemInfo = New-Object System.Windows.Forms.GroupBox $GBSystemInfo.Location = New-Object System.Drawing.Size(20,324) $GBSystemInfo.Size = New-Object System.Drawing.Size(220,138) $GBSystemInfo.Font = New-Object System.Drawing.Font("Microsoft Sans Serif","8.25",[System.Drawing.FontStyle]::Bold) -$GBSystemInfo.Text = "System Information" +$GBSystemInfo.Text = "System Information" $OutputBoxSysInfo = New-Object System.Windows.Forms.RichTextBox $OutputBoxSysInfo.Location = New-Object System.Drawing.Size(30,340) $OutputBoxSysInfo.Size = New-Object System.Drawing.Size(200,115) $OutputBoxSysInfo.BackColor = "white" $OutputBoxSysInfo.ReadOnly = $true -$OutputBoxSysInfo.MultiLine = $True - -$TBComputerName = New-Object System.Windows.Forms.TextBox -$TBComputerName.Location = New-Object System.Drawing.Size(310,80) -$TBComputerName.Size = New-Object System.Drawing.Size(390,80) -$TBComputerName.Font = New-Object System.Drawing.Font("Microsoft Sans Serif","18",[System.Drawing.FontStyle]::Bold) -$TBComputerName.TabIndex = "1" -$TBComputerName.Text = Get-OSDComputerName - -$GBComputerName = New-Object System.Windows.Forms.GroupBox -$GBComputerName.Location = New-Object System.Drawing.Size(290,50) -$GBComputerName.Size = New-Object System.Drawing.Size(440,90) -$GBComputerName.Font = New-Object System.Drawing.Font("Microsoft Sans Serif","10",[System.Drawing.FontStyle]::Bold) -$GBComputerName.Text = "Computer name:" - -$ButtonOK = New-Object System.Windows.Forms.Button -$ButtonOK.Location = New-Object System.Drawing.Size(655,400) -$ButtonOK.Size = New-Object System.Drawing.Size(100,60) -$ButtonOK.Font = New-Object System.Drawing.Font("Microsoft Sans Serif","10",[System.Drawing.FontStyle]::Bold) -$ButtonOK.Text = "Set" -$ButtonOK.TabIndex = "2" -$ButtonOK.Add_Click({Set-OSDComputerName}) - -$Form.KeyPreview = $True -$Form.Add_KeyDown({if ($_.KeyCode -eq "Enter"){Set-OSDComputerName}}) - -Load-Form \ No newline at end of file +$OutputBoxSysInfo.MultiLine = $True + +$TBComputerName = New-Object System.Windows.Forms.TextBox +$TBComputerName.Location = New-Object System.Drawing.Size(310,80) +$TBComputerName.Size = New-Object System.Drawing.Size(390,80) +$TBComputerName.Font = New-Object System.Drawing.Font("Microsoft Sans Serif","18",[System.Drawing.FontStyle]::Bold) +$TBComputerName.TabIndex = "1" +$TBComputerName.Text = Get-OSDComputerName + +$GBComputerName = New-Object System.Windows.Forms.GroupBox +$GBComputerName.Location = New-Object System.Drawing.Size(290,50) +$GBComputerName.Size = New-Object System.Drawing.Size(440,90) +$GBComputerName.Font = New-Object System.Drawing.Font("Microsoft Sans Serif","10",[System.Drawing.FontStyle]::Bold) +$GBComputerName.Text = "Computer name:" + +$ButtonOK = New-Object System.Windows.Forms.Button +$ButtonOK.Location = New-Object System.Drawing.Size(655,400) +$ButtonOK.Size = New-Object System.Drawing.Size(100,60) +$ButtonOK.Font = New-Object System.Drawing.Font("Microsoft Sans Serif","10",[System.Drawing.FontStyle]::Bold) +$ButtonOK.Text = "Set" +$ButtonOK.TabIndex = "2" +$ButtonOK.Add_Click({Set-OSDComputerName}) + +$Form.KeyPreview = $True +$Form.Add_KeyDown({if ($_.KeyCode -eq "Enter"){Set-OSDComputerName}}) + +Load-Form