Set Computername via Powershell (SCCM Task Sequence)

Dave 1 Reputation point
2021-01-09T21:22:35.173+00:00

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')

---- 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 PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,358 questions
Microsoft Configuration Manager Deployment
Microsoft Configuration Manager Deployment
Microsoft Configuration Manager: An integrated solution for for managing large groups of personal computers and servers.Deployment: The process of delivering, assembling, and maintaining a particular version of a software system at a site.
896 questions
Microsoft Configuration Manager
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Rich Matheisen 44,776 Reputation points
    2021-01-10T19:34:25.567+00:00

    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
    
    1 person found this answer helpful.
    0 comments No comments

  2. Rich Matheisen 44,776 Reputation points
    2021-01-09T22:43:12.16+00:00

    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".