question

Dave-5722 avatar image
0 Votes"
Dave-5722 asked saldana-msft edited

Set Computername via Powershell (SCCM Task Sequence)

Hi,

Please remove or advise if this is in the wrong place or not allowed, but i was wondering if one of you wonderful and very knowledgeable people could lend me a hand and help me with my code.

I will admit know most of this main code i have taken from other scripts and tried to piece together but its not setting the computer name or TS variable.

What I'm hoping to get out of this script is to ask for a Computer name and then select a build type, then used the name given and type to form a new computer name (Example: Name:- W10-001 Build Type: Standard for Set computer name to S-W10-001 - Then at the same time set a TS variable of either S or Standard depending on Build type selected so that later in the TS it can install Different Apps and Configurations for each build.

Script:

----------------------------------------------------------------


PowerShell Script to ask for a Computer name and a biuld choice


Set computer name depending on Input and build select


Set build TS Variable in TS for differnet builds from same TS


----------------------------------------------------------------



Add-Type -AssemblyName PresentationFramework
Add-Type -AssemblyName System.Windows.Forms
[void][reflection.assembly]::Load('System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
[void][reflection.assembly]::Load('System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a')
[System.Windows.Forms.Application]::EnableVisualStyles()

---- Form Information ----


function Load-Build {
$Build.Controls.Add($TextBox1)
$Build.Controls.Add($RadioButton1) #Standard
$Build.Controls.Add($RadioButton2) #Admin
$Build.Controls.Add($Button1)
$Build.Controls.Add($Groupbox1)
$Build.Add_Shown({$Build.Activate()})
[void] $Build.ShowDialog()
}

$InitialFormWindowState = New-Object 'System.Windows.Forms.FormWindowState'

$Build = New-Object system.Windows.Forms.Form
$Build.StartPosition = "CenterScreen"
$Build.SizeGripStyle = "Hide"
$Build.ControlBox = $false
$Build.Size = New-Object System.Drawing.Size(180,200)
$Build.MinimumSize = New-Object System.Drawing.Size(180,200)
$Build.MaximumSize = New-Object System.Drawing.Size(180,200)
$Build.text = "Build"
$Build.TopMost = $true

$TextBox1 = New-Object system.Windows.Forms.TextBox
$TextBox1.multiline = $false
$TextBox1.width = 135
$TextBox1.height = 20
$TextBox1.location = New-Object System.Drawing.Point(6,15)
$TextBox1.Font = New-Object System.Drawing.Font('Microsoft Sans Serif',10)
$TextBox1.Text = ""

$RadioButton1 = New-Object system.Windows.Forms.RadioButton
$RadioButton1.text = "Standard Build"
$RadioButton1.AutoSize = $true
$RadioButton1.width = 104
$RadioButton1.height = 20
$RadioButton1.location = New-Object System.Drawing.Point(8,46)
$RadioButton1.Font = New-Object System.Drawing.Font('Microsoft Sans Serif',10)

$RadioButton2 = New-Object system.Windows.Forms.RadioButton
$RadioButton2.text = "Admin Build"
$RadioButton2.AutoSize = $true
$RadioButton2.width = 104
$RadioButton2.height = 20
$RadioButton2.location = New-Object System.Drawing.Point(8,72)
$RadioButton2.Font = New-Object System.Drawing.Font('Microsoft Sans Serif',10)

$Button1 = New-Object system.Windows.Forms.Button
$Button1.text = "OK"
$Button1.width = 130
$Button1.height = 30
$Button1.location = New-Object System.Drawing.Point(7,100)
$Button1.Font = New-Object System.Drawing.Font('Microsoft Sans Serif',10)

$Groupbox1 = New-Object system.Windows.Forms.Groupbox
$Groupbox1.height = 140
$Groupbox1.width = 150
$Groupbox1.text = "Enter Computer Name"
$Groupbox1.location = New-Object System.Drawing.Point(5,10)

$Groupbox1.controls.AddRange(@($TextBox1,$RadioButton1,$RadioButton2,$Button1))
$Build.controls.AddRange(@($Groupbox1))

---- Main Code ----



Determine last Part of the Computer Name

$ComputerName = "$($TextBox1.text)"
$ComputerName = $ComputerName.ToUpper()

Determine first part of Build Selected

if ($radiobutton1.Checked -eq $true) { $Build_Type = "$($Build_Type)S"; } # S = Standard
elseif ($radiobutton2.Checked -eq $true) { $Build_Type = "$($Build_Type)A"; } # A = Admin

Determine the Fill Computer Name

$OSDFullName = "$($Build_Type)$($ComputerName)"

---- Close Form Down Code ----



$Button1.Add_Click({

Load TS Environment

$TSEnv = New-Object -COMObject Microsoft.SMS.TSEnvironment

Set Computer Name in TS

$TSEnv.Value("OSDComputerName") = "$OSDFullName"

Set Build Type in TS

$TSEnv.Value($TSBuild) = $Build_Type

Close Build

$Build.Close()
})

---- Show Form ----



$Build.ShowDialog()


I know that once the TS Variable is set that it will choose the correct software/settings etc.. as i have copied this from another TS, i just cant get this all to work from my script.

Thanks in advance

Dave

windows-server-powershellmem-cm-generalmem-cm-osd
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

RichMatheisen-8856 avatar image
0 Votes"
RichMatheisen-8856 answered Dave-5722 commented

You're using the variable name $Build_Type a lot, but I can't find where you initialize it with any value! The 1st place I see it is in this section of your code : Determine first part of Build Selected. There, you use it like this: $Build_Type = "$($Build_Type)S". But since $Build_Type doesn't yet exist, so the result is simply "S". If that's what you intend, why not simply assign the value "S" (or maybe "S-")????

Just below that, in the section "Determine the Fill Computer Name" you use this code: $OSDFullName = "$($Build_Type)$($ComputerName)" which should result in a value of "SMyComPuterName" or "AMyComputerName" which, I guess would be result in the correct value of "S-W10-001" or "A-W10-001" if $TextBox1 had a value of "-W10-001".

However, you give no indication of what the value of $ComputerName might be, or what you mean when you say " just cant get this all to work from my script".


· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks for your input. I'm very new to PowerShell. So I'm sorry is i used the $Build_Type a lot.

When i manually run this Script outside of the TS and use a Message box to show the final variable "$OSDFullName" then the message box comes back completely blank.

Please can you offer any advise on the best code to use to get this outcome from the form:

Pop Up in the TS and take user input from the textbox and take the relevant button and create a computer name like in the example above S-WIN-001 or A-WIN-001. and then also take that S or A from the Button choice and create a TS variable.

Sorry for not making this clear the first time. Any help would really be appreciated.

0 Votes 0 ·
RichMatheisen-8856 avatar image
0 Votes"
RichMatheisen-8856 answered

Try this code. I rearranged the sequence in which you executed the form open/close. I also added some error/sanity checks on the data from the form.

 Add-Type -AssemblyName PresentationFramework
 Add-Type -AssemblyName System.Windows.Forms
 [void][reflection.assembly]::Load('System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
 [void][reflection.assembly]::Load('System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a')
 [System.Windows.Forms.Application]::EnableVisualStyles()
    
 #
 #---- Form Information ----
 #
 function Load-Build {
     $Build.Controls.Add($TextBox1)
     $Build.Controls.Add($RadioButton1) #Standard
     $Build.Controls.Add($RadioButton2) #Admin
     $Build.Controls.Add($Button1)
     $Build.Controls.Add($Groupbox1)
     $Build.Add_Shown( { $Build.Activate() })
     [void] $Build.ShowDialog()
 }
    
 $InitialFormWindowState = New-Object 'System.Windows.Forms.FormWindowState'
    
     $Build = New-Object system.Windows.Forms.Form
     $Build.StartPosition = "CenterScreen"
     $Build.SizeGripStyle = "Hide"
     $Build.ControlBox = $false
     $Build.Size = New-Object System.Drawing.Size(180, 200)
     $Build.MinimumSize = New-Object System.Drawing.Size(180, 200)
     $Build.MaximumSize = New-Object System.Drawing.Size(180, 200)
     $Build.text = "Build"
     $Build.TopMost = $true
    
 $TextBox1 = New-Object system.Windows.Forms.TextBox
     $TextBox1.multiline = $false
     $TextBox1.width = 135
     $TextBox1.height = 20
     $TextBox1.location = New-Object System.Drawing.Point(6, 15)
     $TextBox1.Font = New-Object System.Drawing.Font('Microsoft Sans Serif', 10)
     $TextBox1.Text = ""
    
 $RadioButton1 = New-Object system.Windows.Forms.RadioButton
     $RadioButton1.text = "Standard Build"
     $RadioButton1.AutoSize = $true
     $RadioButton1.width = 104
     $RadioButton1.height = 20
     $RadioButton1.location = New-Object System.Drawing.Point(8, 46)
     $RadioButton1.Font = New-Object System.Drawing.Font('Microsoft Sans Serif', 10)
    
 $RadioButton2 = New-Object system.Windows.Forms.RadioButton
     $RadioButton2.text = "Admin Build"
     $RadioButton2.AutoSize = $true
     $RadioButton2.width = 104
     $RadioButton2.height = 20
     $RadioButton2.location = New-Object System.Drawing.Point(8, 72)
     $RadioButton2.Font = New-Object System.Drawing.Font('Microsoft Sans Serif', 10)
    
 $Button1 = New-Object system.Windows.Forms.Button
     $Button1.text = "OK"
     $Button1.width = 130
     $Button1.height = 30
     $Button1.location = New-Object System.Drawing.Point(7, 100)
     $Button1.Font = New-Object System.Drawing.Font('Microsoft Sans Serif', 10)
    
 $Groupbox1 = New-Object system.Windows.Forms.Groupbox
     $Groupbox1.height = 140
     $Groupbox1.width = 150
     $Groupbox1.text = "Enter Computer Name"
     $Groupbox1.location = New-Object System.Drawing.Point(5, 10)
    
 $Groupbox1.controls.AddRange(@($TextBox1, $RadioButton1, $RadioButton2, $Button1))
 $Build.controls.AddRange(@($Groupbox1))
 $Button1.Add_Click( { 
         #Close Build
         $Build.Close() | Out-Null
     }
 )
 #
 #---- Main Code ----
 #
 #---- Show Form ----
 $Build.ShowDialog() | Out-Null
 # Check for empty/All-Spaces computer name
 If ($TextBox1.Text.Trim().Length -lt 1){
     Throw "Computer name wasn't entered!!"
 }
 # Determine first part of Build Selected
 if ($Radiobutton1.Checked) { 
     $Build_Type = "S"  # S = Standard
 }
 elseif ($Radiobutton2.Checked) { 
     $Build_Type = "A"  # A = Admin
 }
 else{
     Throw "This should never happen! No RadioButton checked!!!"
 }
 # Determine the Full Computer Name
 $OSDFullName =  "{0}{1}" -f $Build_Type, $TextBox1.Text.Trim().ToUpper()
 #### If you want a hyphen between the computer name and the build type, use the line below
 #$OSDFullName =  "{0}-{1}" -f $Build_Type, $TextBox1.Text.Trim().ToUpper()
 #
 #################################
 #  Omitting SMS code because I don't have any SMS!
 #$TSEnv = New-Object -COMObject Microsoft.SMS.TSEnvironment
 #$TSEnv.Value("OSDComputerName") = "$OSDFullName"
 #$TSEnv.Value($TSBuild) = $Build_Type
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.