Print
Hits: 2304

Create PsScript (GUI set value to TSEnvVariables)

We have used and modified Nikolaj Andersens script (http://www.scconfigmgr.com/2013/10/02/prompt-for-computer-name-during-osd-with-powershell/)

Her is our version:

 

 

1

 

 

Here is modified Script:

 

<#

            .NOTES

            ===========================================================================

            Created with:          SAPIEN Technologies, Inc., PowerShell Studio 2016 v5.3.131

            Created on:            02-01-2017 12:43

            Created by:            KASIA

            Organization:          XXX

            Filename:

            ===========================================================================

             .DESCRIPTION

                        A description of the file.

 #>

 Function Load-Form

 {

             $Form.Controls.Add($TBComputerName)

             $Form.Controls.Add($GBComputerName)

             $Form.Controls.Add($TBComputerIP)

             $Form.Controls.Add($GBComputerIP)

             $Form.Controls.Add($TBComputerGW)

             $Form.Controls.Add($GBComputerGW)

             $Form.Controls.Add($TBComputerDNS)

             $Form.Controls.Add($GBComputerDNS)

             $Form.Controls.Add($TBComputerDNS2)

             $Form.Controls.Add($GBComputerDNS2)

             $Form.Controls.Add($ButtonOK)

             $Form.Add_Shown({ $Form.Activate() })

            

$TBComputerIP.add_TextChanged({

$ip2 = $TBComputerIP.text.split('.')

if($ip2.count -lt 4) {

$TBComputerGW.text = $TBComputerIP.text

} else {

$ip2[-1] = 1

$TBComputerGW.text = $ip2 -join '.'

}

})

             [void]$Form.ShowDialog()

 }

 [void][System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")

 [void][System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")

 

$Global:ErrorProvider = New-Object System.Windows.Forms.ErrorProvider

$Form = New-Object System.Windows.Forms.Form

$Form.Size = New-Object System.Drawing.Size(285, 440)

$Form.MinimumSize = New-Object System.Drawing.Size(285, 440)

$Form.MaximumSize = New-Object System.Drawing.Size(285, 440)

$Form.StartPosition = "CenterScreen"

$Form.SizeGripStyle = "Hide"

$Form.Text = "Enter Computer Informations"

$Form.ControlBox = $false

$Form.TopMost = $true

 

$TBComputerName = New-Object System.Windows.Forms.TextBox

$TBComputerName.Location = New-Object System.Drawing.Size(25, 30)

$TBComputerName.Size = New-Object System.Drawing.Size(215, 50)

$TBComputerName.TabIndex = "1"

$GBComputerName = New-Object System.Windows.Forms.GroupBox

$GBComputerName.Location = New-Object System.Drawing.Size(20, 10)

$GBComputerName.Size = New-Object System.Drawing.Size(225, 50)

$GBComputerName.Text = "Computer name:"

$TBComputerIP = New-Object System.Windows.Forms.TextBox

$TBComputerIP.Location = New-Object System.Drawing.Size(25, 100)

$TBComputerIP.Size = New-Object System.Drawing.Size(215, 50)

$TBComputerIP.TabIndex = "1"

$GBComputerIP = New-Object System.Windows.Forms.GroupBox

$GBComputerIP.Location = New-Object System.Drawing.Size(20, 80)

$GBComputerIP.Size = New-Object System.Drawing.Size(225, 50)

$GBComputerIP.Text = "IP:"

$TBComputerGW = New-Object System.Windows.Forms.TextBox

$TBComputerGW.Location = New-Object System.Drawing.Size(25, 170)

$TBComputerGW.Size = New-Object System.Drawing.Size(215, 50)

$TBComputerGW.TabIndex = "1"

$GBComputerGW = New-Object System.Windows.Forms.GroupBox

$GBComputerGW.Location = New-Object System.Drawing.Size(20, 150)

$GBComputerGW.Size = New-Object System.Drawing.Size(225, 50)

$GBComputerGW.Text = "Gateway:"

$TBComputerDNS = New-Object System.Windows.Forms.TextBox

$TBComputerDNS.Location = New-Object System.Drawing.Size(25, 240)

$TBComputerDNS.Size = New-Object System.Drawing.Size(215, 50)

$TBComputerDNS.TabIndex = "1"

#$TBComputerDNS.text = "10.10.10.1"

$GBComputerDNS = New-Object System.Windows.Forms.GroupBox

$GBComputerDNS.Location = New-Object System.Drawing.Size(20, 220)

$GBComputerDNS.Size = New-Object System.Drawing.Size(225, 50)

$GBComputerDNS.Text = "DNS:"

$TBComputerDNS2 = New-Object System.Windows.Forms.TextBox

$TBComputerDNS2.Location = New-Object System.Drawing.Size(25, 310)

$TBComputerDNS2.Size = New-Object System.Drawing.Size(215, 50)

$TBComputerDNS2.TabIndex = "1"

#$TBComputerDNS2.text = "10.10.10.2"

$GBComputerDNS2 = New-Object System.Windows.Forms.GroupBox

$GBComputerDNS2.Location = New-Object System.Drawing.Size(20, 290)

$GBComputerDNS2.Size = New-Object System.Drawing.Size(225, 50)

$GBComputerDNS2.Text = "DNS:"

$ButtonOK = New-Object System.Windows.Forms.Button

$ButtonOK.Location = New-Object System.Drawing.Size(195, 360)

$ButtonOK.Size = New-Object System.Drawing.Size(50, 20)

$ButtonOK.Text = "OK"

$ButtonOK.TabIndex = "2"

$ButtonOK.Add_Click({

                          ##Set-ComputerName##

                          $ErrorProvider.Clear()

                          if ($TBComputerName.Text.Length -eq 0)

                          {

                                      $ErrorProvider.SetError($GBComputerName, "Please enter a computer name.")

                                      return

                          }

                          elseif ($TBComputerName.Text.Length -gt 15)

                                                  {

                                      $ErrorProvider.SetError($GBComputerName, "Computer name cannot be more than 15 characters.")

                                      return

                          }

                          #Validation Rule for computer names.

                          elseif ($TBComputerName.Text -match "^[-_]|[^a-zA-Z0-9-_]")

                          {

                                      $ErrorProvider.SetError($GBComputerName, "Computer name invalid, please correct the computer name.")

                                      return

                          }

                         else

                          {

                                      $OSDComputerName = $TBComputerName.Text.ToUpper()

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

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

                                      #$Form.Close()

                          }

                          ##Set-OSDComputerIP##

                          if ($TBComputerIP.Text.Length -eq 0)

                          {

                                      $ErrorProvider.SetError($GBComputerIP, "Please enter a computer IP.")

                                      return

                          }

                          elseif ($TBComputerIP.Text.Length -gt 15)

                          {

                                      $ErrorProvider.SetError($GBComputerIP, "Computer IP cannot be more than 15 characters.")

                                      return

                          }

                          #Validation Rule for computer IP.

                          $ip = [regex]"^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$"

                          if ($ip.IsMatch($TBComputerIP.text) -ne $true)

                          {

                                      $ErrorProvider.SetError($GBComputerIP, "Computer IP invalid, please correct the computer IP.")

                                      return

                          }

                          else

                          {

                                      $OSDComputerIP = $TBComputerIP.Text

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

                                      $TSEnv.Value("OSDAdapter0IPAddressList") = "$($OSDComputerIP)"

                                      $TSEnv.Value("OSDAdapter0SubnetMask") = "255.255.255.0"

                                      $TSEnv.Value("OSDAdapter0EnableDHCP") = "False"

                                      $TSEnv.Value("OSDAdapterCount") = "1"

                                      #$Form.Close()

                          }

                          ##Set-OSDComputerGW##

                          if ($TBComputerGW.Text.Length -eq 0)

                          {

                                      $ErrorProvider.SetError($GBComputerGW, "Please enter a computer Gateway.")

                                      return

                          }

                          elseif ($TBComputerGW.Text.Length -gt 15)

                          {

                                      $ErrorProvider.SetError($GBComputerGW, "Computer Gateway cannot be more than 15 characters.")

                                      return

                          }

                          #Validation Rule for computer GW.

                          $GWip = [regex]"^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$"

                          if ($GWip.IsMatch($TBComputerGW.text) -ne $true)

                          {

                                      $ErrorProvider.SetError($GBComputerGW, "Computer Gateway invalid, please correct the computer Gateway.")

                                      return

                          }

                          else

                          {

                                      $OSDComputerGW = $TBComputerGW.Text

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

                                      $TSEnv.Value("OSDAdapter0Gateways") = "$($OSDComputerGW)"

                                      #$Form.Close()

                          }

                          ##Set-OSDComputerDNS##

                          if ($TBComputerDNS.Text.Length -eq 0)

                          {

                                      $ErrorProvider.SetError($GBComputerDNS, "Please enter a computer DNS.")

                                      return

                          }

                          elseif ($TBComputerDNS.Text.Length -gt 15)

                          {

                                      $ErrorProvider.SetError($GBComputerDNS, "Computer DNS cannot be more than 15 characters.")

                                      return

                          }

                          elseif ($TBComputerDNS2.Text.Length -eq 0)

                          {

                                      $ErrorProvider.SetError($GBComputerDNS2, "Please enter a computer DNS.")

                                      return

                          }

                          elseif ($TBComputerDNS2.Text.Length -gt 15)

                          {

                                      $ErrorProvider.SetError($GBComputerDNS2, "Computer DNS cannot be more than 15 characters.")

                                     return

                          }

                          #Validation Rule for computer DNS.

                          $DNSip = [regex]"^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$"

                          if ($DNSip.IsMatch($TBComputerDNS.text) -ne $true)

                          {

                                      $ErrorProvider.SetError($GBComputerDNS, "Computer DNS invalid, please correct the computer DNS.")

                                      return

                          }

                          $DNS2ip = [regex]"^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$"

                          $DNS2ip.IsMatch($TBComputerDNS2.text)

                          if ($DNS2ip.IsMatch($TBComputerDNS2.text) -ne $true)

                          {

                                      $ErrorProvider.SetError($GBComputerDNS2, "Computer DNS invalid, please correct the computer DNS.")

                                      return

                          }

                          else

                          {

                                      $OSDComputerDNS = $TBComputerDNS.Text

                                      $OSDComputerDNS2 = $TBComputerDNS2.Text

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

                                      $TSEnv.Value("OSDAdapter0DNSServerList") = "$($OSDComputerDNS),$($OSDComputerDNS2)"

                          }

                          $Form.Close()

             })

 $Form.KeyPreview = $True

 $Form.Add_KeyDown({

                          if ($_.KeyCode -eq "Enter")

                          { ##Set-ComputerName##

                                      $ErrorProvider.Clear()

                                      if ($TBComputerName.Text.Length -eq 0)

                                      {

                                                   $ErrorProvider.SetError($GBComputerName, "Please enter a computer name.")

                                                   return

                                      }

                                      elseif ($TBComputerName.Text.Length -gt 15)

                                      {

                                                   $ErrorProvider.SetError($GBComputerName, "Computer name cannot be more than 15 characters.")

                                                   return

                                      }

                                      #Validation Rule for computer names.

                                      elseif ($TBComputerName.Text -match "^[-_]|[^a-zA-Z0-9-_]")

                                      {

                                                   $ErrorProvider.SetError($GBComputerName, "Computer name invalid, please correct the computer name.")

                                                   return

                                      }

                                      else

                                      {

                                                   $OSDComputerName = $TBComputerName.Text.ToUpper()

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

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

                                                   #$Form.Close()

                                      }

                                      ##Set-OSDComputerIP##

                                      if ($TBComputerIP.Text.Length -eq 0)

                                      {

                                                   $ErrorProvider.SetError($GBComputerIP, "Please enter a computer IP.")

                                                   return

                                      }

                                      elseif ($TBComputerIP.Text.Length -gt 15)

                                      {

                                                   $ErrorProvider.SetError($GBComputerIP, "Computer IP cannot be more than 15 characters.")

                                                   return

                                      }

                                      #Validation Rule for computer IP.

                                      $ip = [regex]"^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$"

                                      if ($ip.IsMatch($TBComputerIP.text) -ne $true)

                                      {

                                                   $ErrorProvider.SetError($GBComputerIP, "Computer IP invalid, please correct the computer IP.")

                                                   return

                                      }

                                      else

                                      {

                                                   $OSDComputerIP = $TBComputerIP.Text

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

                                                   $TSEnv.Value("OSDAdapter0IPAddressList") = "$($OSDComputerIP)"

                                                   $TSEnv.Value("OSDAdapter0SubnetMask") = "255.255.255.0"

                                                   $TSEnv.Value("OSDAdapter0EnableDHCP") = "False"

                                                   $TSEnv.Value("OSDAdapterCount") = "1"

                                                   #$Form.Close()

                                      }

                                      ##Set-OSDComputerGW##

                                      if ($TBComputerGW.Text.Length -eq 0)

                                      {

                                                   $ErrorProvider.SetError($GBComputerGW, "Please enter a computer Gateway.")

                                                   return

                                      }

                                      elseif ($TBComputerGW.Text.Length -gt 15)

                                      {

                                                   $ErrorProvider.SetError($GBComputerGW, "Computer Gateway cannot be more than 15 characters.")

                                                   return

                                      }

                                      #Validation Rule for computer GW.

                                      $GWip = [regex]"^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$"

                                      if ($GWip.IsMatch($TBComputerGW.text) -ne $true)

                                      {

                                                   $ErrorProvider.SetError($GBComputerGW, "Computer Gateway invalid, please correct the computer Gateway.")

                                                   return

                                      }

                                      else

                                      {

                                                   $OSDComputerGW = $TBComputerGW.Text

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

                                                   $TSEnv.Value("OSDAdapter0Gateways") = "$($OSDComputerGW)"

                                                   #$Form.Close()

                                      }

                                      ##Set-OSDComputerDNS##

                                      if ($TBComputerDNS.Text.Length -eq 0)

                                      {

                                                   $ErrorProvider.SetError($GBComputerDNS, "Please enter a computer DNS.")

                                                   return

                                      }

                                      elseif ($TBComputerDNS.Text.Length -gt 15)

                                      {

                                                   $ErrorProvider.SetError($GBComputerDNS, "Computer DNS cannot be more than 15 characters.")

                                                   return

                                      }

                                      elseif ($TBComputerDNS2.Text.Length -eq 0)

                                      {

                                                   $ErrorProvider.SetError($GBComputerDNS2, "Please enter a computer DNS.")

                                                   return

                                      }

                                      elseif ($TBComputerDNS2.Text.Length -gt 15)

                                      {

                                                   $ErrorProvider.SetError($GBComputerDNS2, "Computer DNS cannot be more than 15 characters.")

                                                   return

                                      }

                                      #Validation Rule for computer DNS.

                                      $DNSip = [regex]"^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$"

                                      if ($DNSip.IsMatch($TBComputerDNS.text) -ne $true)

                                      {

                                                   $ErrorProvider.SetError($GBComputerDNS, "Computer DNS invalid, please correct the computer DNS.")

                                                   return

                                      }

                                      $DNS2ip = [regex]"^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$"

                                      $DNS2ip.IsMatch($TBComputerDNS2.text)

                                      if ($DNS2ip.IsMatch($TBComputerDNS2.text) -ne $true)

                                      {

                                                   $ErrorProvider.SetError($GBComputerDNS2, "Computer DNS invalid, please correct the computer DNS.")

                                                   return

                                      }

                                      else

                                      {

                                                   $OSDComputerDNS = $TBComputerDNS.Text

                                                   $OSDComputerDNS2 = $TBComputerDNS2.Text

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

                                                   $TSEnv.Value("OSDAdapter0DNSServerList") = "$($OSDComputerDNS),$($OSDComputerDNS2)"

                                      }

                                      $Form.Close() }

             })

 Load-Form

 

Update your boot image to support PowerShell 

If you havn’t already updated your boot images with PowerShell support, follow these instructions.

1. Right-click on your boot image and select Properties.
2. Select the Optional Components tab.
3. Click on the yellow star and select the following components:
 

4. Click OK and then OK again. Let the wizard update your DP’s

Implement the script in a Task Sequence

1. Edit the Task Sequence that you wish to add this functionality to.
2. Add a Run Command Line step in between the Partition Disk and Apply Operating System steps.
3. Rename the Run Command Line step to Prompt for ComputerName.
4. Edit the Command Line so that it looks like this:

5. Put a check mark in the Package box, click Browse and select your newly created package (OSDComputerName).

 

2

 

 

Flush Variables.dat file

We have referred to Johan Blog:

http://deploymentresearch.com/Research/Post/541/Quick-Fix-MDT-Environment-Variables-set-in-PowerShell-are-not-flushed-to-Variables-dat

Make ZTIFlushVariables.wsf script and save it in %SCRIPTROOT%

  

<job id="ZTIFlushVariables">

    <script  language="VBScript" src="ZTIUtility.vbs"/>

    <script  language="VBScript">

    ' Flush variables set via PowerShell to Variables.dat (workaround for bug in MDT 2013 Update 2)

    Dim oTSEnv

    Set oTSEnv = CreateObject("Microsoft.SMS.TSEnvironment")

    Dim oVar

        For Each oVar In oTSEnv.GetVariables

        '  Only worry about variables that have a value

        If oEnvironment.Item(oVar)  <> "" Then

            '  Log variable before flush

            oLogging.CreateEntry  "---------------------------------------------------------------------------",  LogTypeInfo   

            oLogging.CreateEntry  "Before Flush: " & oVar & " variable from Variables.dat: " & oEnvironment.GetDat(oVar), LogTypeInfo   

            '  Flush the variable

            oEnvironment.Item(oVar) = oEnvironment.Item(oVar)

            '  Log variable after flush

            oLogging.CreateEntry  "After Flush: " & oVar & " variable from Variables.dat: " & oEnvironment.GetDat(oVar), LogTypeInfo   

        End if

    Next

    </script>

</job>

  

3

  

Call a VBscript that resets the value to whatever value your PowerShell script set, then just run that script after your PowerShell script. 

 

4

  

Add OSDAdapterCount value in Task Sequence

5

  

VOILA! DONE! J