/build/static/layout/Breadcrumb_cap_w.png

Collect/Apply Computer Name Pre/Post Installation Tasks

Hi Team,

I was just wondering if there are any changes that need to be made to the scripts that are loaded in the pre/post installation tasks? They are loaded into my (Collect) Pre-installation Tasks and (Apply) Post-installation tasks however they are not seeming to apply any name afterwards I am just getting a randomly generated name.

I am running the latest version of KACE.

Cheers


0 Comments   [ + ] Show comments

Answers (1)

Answer Summary:
Posted by: cdalley 10 years ago
7th Degree Black Belt
1

I may have found the answer, the apply computer name script was set to run in the runtime "K2000 Boot Environment (Windows)" not "Windows"

Testing my theory.


Comments:
  • Post installs happen in the boot environment, so you shouldn't have to change that. Can you post the script in question? Also, is the order of your pre and post installs correct? For example, if you wipe the hard drive before the name is collected, then it won't work. - nheyne 10 years ago
  • Hi nheyne:

    The scripts are part of KACE's built in tasks.
    The collect task is before the wipe and format of the HDD, the apply computer name is a mid-level task.

    Collect Computer Name Script:
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set diskDrives = fso.Drives

    regFound = False
    'Search through fixed drives (Drivetype = 2) for required registry file.
    For Each drive In diskDrives
    If (drive.DriveType = 2) and drive.DriveLetter <> "X" and fso.FileExists(drive.DriveLetter & ":\WINDOWS\System32\Config\SYSTEM") Then
    systemDrive = drive.DriveLetter
    regFound = True
    exit for
    End If
    NEXT

    'Registry not found
    If regFound = False Then
    WScript.Quit
    End If

    'Get the computer name from the registry
    Set wshShell = CreateObject("WScript.Shell")
    Set execStatus = wshShell.Exec("reg load HKLM\TEMP_SYSTEM " & systemDrive & ":\WINDOWS\System32\Config\SYSTEM")
    'Wait for registry to load completely (max 10 seconds)
    count = 0
    Do While (count < 10) and execStatus.Status = 0
    WScript.Sleep 1000
    count = count + 1
    Loop
    If execStatus.Status = 0 Then
    WScript.Quit
    End If
    regKeyCompName = "HKLM\TEMP_SYSTEM\ControlSet001\Control\ComputerName\ComputerName\ComputerName"
    compName = wshShell.RegRead(regKeyCompName)
    WshShell.Exec("reg unload HKLM\TEMP_SYSTEM")
    WScript.Sleep 1000

    'Dump the computer name into a file named as the mac address inside X:
    If fso.DriveExists("X:") then
    'Get mac adress
    Set objSysEnv = wshShell.Environment("PROCESS")
    macAddress = objSysEnv("MAC_ADDRESS")
    Set compNameFile = fso.CreateTextFile("X:\" & macAddress, True)
    Else
    usbDrive = fso.GetDriveName(wscript.ScriptFullName)
    Set compNameFile = fso.CreateTextFile(usbDrive & "\KACE\ComputerName", True)
    End If
    compNameFile.WriteLine(compName)
    compNameFile.Close


    ApplyComputerName:
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set wshShell = CreateObject("WScript.Shell")

    systemDrive = wshShell.ExpandEnvironmentStrings("%system_drive%")

    'Get ComputerName, exit script if get empty.
    coputerName = GetComputerName()
    If coputerName = "" Then
    WScript.Quit
    End If

    count = 0

    'Check whether its a syspreped imgage or not, if not exit the script
    sysprep = False
    sysprep = IsSyspreped()
    If (sysprep = False) or (sysprep = "Error") Then
    WScript.Quit
    End If

    'Check the OS type and set call appropriate function to hadle required file changes
    osVersion = ""
    osVersion = GetOSVersion()
    If osVersion = "" Then
    WScript.Quit
    ElseIf mid(osVersion, 1, 1) = "6" Then
    ModifyVistaConfFile()
    Else
    ModifyNonVistaConfFile()
    End If



    'Function for getting computer name from temp file stored insided X: by pre-installtask
    'Input parameter - nothing
    'Returning - Computer name if found else return null string.

    Function GetComputerName()
    If fso.DriveExists("X:") then
    Set objSysEnv = wshShell.Environment("PROCESS")
    macAddress = objSysEnv("MAC_ADDRESS")
    compNameFilePath = "X:\" & macAddress
    Else
    usbDrive = fso.GetDriveName(wscript.ScriptFullName)
    compNameFilePath = usbDrive & "\KACE\ComputerName"
    End If
    If fso.FileExists(compNameFilePath) Then
    Set compNameFile = fso.OpenTextFile(compNameFilePath, 1)
    coputerName = Trim(compNameFile.ReadLine)
    compNameFile.close
    'Delete file containing computer name
    fso.DeleteFile compNameFilePath
    Else
    coputerName = ""
    End If
    GetComputerName = coputerName
    End Function

    'Function for getting whether its a syspreped image or not
    'Input parameter - nothing
    'Returning - True or False or Error.

    Function IsSyspreped()
    If fso.FileExists(systemDrive & "\WINDOWS\System32\Config\SYSTEM") Then
    Set execStatus = wshShell.Exec("reg load HKLM\TEMP_SYSTEM " & systemDrive & "\WINDOWS\System32\Config\SYSTEM")
    'Wait for registry to load completely (max 10 seconds)
    count = 0
    Do While (count < 10) and execStatus.Status = 0
    WScript.Sleep 1000
    count = count + 1
    Loop
    If execStatus.Status = 0 Then
    syspreped = "Error"
    End If
    Else
    syspreped = "Error"
    End If
    regKeySysprep = "HKLM\TEMP_SYSTEM\Setup\SystemSetupInProgress"
    If wshShell.RegRead(regKeySysprep) = 0 Then
    syspreped = False
    Else
    syspreped = True
    End If
    wshShell.Exec("reg unload HKLM\TEMP_SYSTEM")
    WScript.Sleep 1000
    IsSyspreped = syspreped
    End Function

    'Function for getting whether its a vista or non vista image
    'Input parameter - nothing
    'Returning - True or False or Error.

    Function GetOSVersion()
    If fso.FileExists(systemDrive & "\WINDOWS\System32\Config\SOFTWARE") Then
    Set execStatus = wshShell.Exec("reg load HKLM\TEMP_SOFTWARE " & systemDrive & "\WINDOWS\System32\Config\SOFTWARE")
    'Wait for registry load
    count = 0
    Do While count < 10 and execStatus.Status = 0
    WScript.Sleep 1000
    count = count + 1
    Loop
    If execStatus.Status = 0 Then
    GetOSVersion = ""
    End If
    Else
    GetOSVersion = ""
    End If
    regKeyOsVersion = "HKLM\TEMP_SOFTWARE\Microsoft\Windows NT\CurrentVersion\CurrentVersion"
    osVersion = wshShell.RegRead(regKeyOsVersion)
    wshShell.Exec("reg unload HKLM\TEMP_SOFTWARE")
    WScript.Sleep 1000
    GetOSVersion = osVersion
    End Function

    'Function for modifying vista configuration file (Unattend.xml)
    'Input parameter - nothing
    'Returning - nothing

    Sub ModifyVistaConfFile()
    unattendedXmlFiles = Array( systemDrive & "\Unattend.xml",_
    systemDrive & "\Windows\System32\sysprep\Unattend.xml",_
    systemDrive & "\Windows\panther\Unattend.xml",_
    systemDrive & "\Windows\panther\Autounattend.xml")
    fileCreated = false
    For Each unattendedXmlFile In unattendedXmlFiles
    If fso.FileExists(unattendedXmlFile) Then
    wshShell.Exec("regsvr32 %SystemRoot%\System32\msxml6.dll /s")
    WScript.Sleep 1000
    Set objXMLDoc = CreateObject("msxml2.domdocument.6.0")
    objXMLDoc.async = False
    objXMLDoc.setProperty "SelectionLanguage", "XPath"
    ns = "xmlns:default='urn:schemas-microsoft-com:unattend'"
    objXMLDoc.setProperty "SelectionNamespaces", ns
    objXMLDoc.resolveExternals = False
    objXMLDoc.load(unattendedXmlFile)

    settingsFound = false
    componentFound = false
    computerNameFound = false

    Set settingsNodes = objXMLDoc.documentElement.selectNodes("default:settings")
    for each settingsNode in settingsNodes
    If settingsNode.getAttribute ("pass") = "specialize" Then
    settingsFound = true
    Set componentNodes = settingsNode.selectNodes("default:component")
    for each componentNode in componentNodes
    If componentNode.getAttribute ("name") = "Microsoft-Windows-Shell-Setup" Then
    componentFound = true
    Set computerNameNodes = componentNode.selectNodes("default:ComputerName")
    For Each computerNameNode in computerNameNodes
    computerNameFound = true
    computerNameNode.text = coputerName
    NEXT
    If computerNameFound = false Then
    Set newNode = objXMLDoc.createElement("ComputerName")
    Set newNodeText = objXMLDoc.createTextNode(coputerName)
    newNode.appendChild newNodeText
    newNode.setAttribute "xmlns", "urn:schemas-microsoft-com:unattend"
    componentNode.appendChild newNode
    computerNameFound = true
    End If
    End If
    If componentFound = true Then
    Exit For
    End If
    NEXT
    If componentFound = false Then
    AddComponent objXMLDoc, settingsNode, coputerName
    componentFound = true
    End If
    End If
    If settingsFound = true Then
    Exit For
    End If
    NEXT
    If settingsFound = false Then
    Set root = objXMLDoc.documentElement
    Set newSettingsNode = objXMLDoc.createElement("settings")
    AddComponent objXMLDoc, newSettingsNode, coputerName
    newSettingsNode.setAttribute "pass","specialize"
    newSettingsNode.setAttribute "xmlns", "urn:schemas-microsoft-com:unattend"
    root.appendChild newSettingsNode
    settingsFound = true
    End If

    objXMLDoc.Save(unattendedXmlFile)
    fileCreated = true
    ElseIf fileCreated = false then
    Set unattendedXml = fso.CreateTextFile(unattendedXmlFile, True)
    systemArch = wshShell.ExpandEnvironmentStrings("%PROCESSOR_ARCHITECTURE%")
    unattendedXml.WriteLine "<?xml version=""1.0"" encoding=""utf-8""?>"
    unattendedXml.WriteLine "<unattend xmlns=""urn:schemas-microsoft-com:unattend"" xmlns:wcm=""http://schemas.microsoft.com/WMIConfig/2002/State"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"">"
    unattendedXml.WriteLine Space(2) & "<settings pass=""specialize"">"
    unattendedXml.WriteLine Space(4) & "<component name=""Microsoft-Windows-Shell-Setup"" processorArchitecture=""" & systemArch & """ publicKeyToken="""" language="""" versionScope="""">"
    unattendedXml.WriteLine Space(8) & "<ComputerName>" & coputerName & "</ComputerName>"
    unattendedXml.WriteLine Space(4) & "</component>"
    unattendedXml.WriteLine Space(2) & "</settings>"
    unattendedXml.WriteLine "</unattend>"
    unattendedXml.Close
    fileCreated = true
    End If
    Next

    End Sub

    'Function for creating and appending child node of "component" type in "Unattend.xml"
    'Input parameter - xmlDoc object, Parent Node Object and computer name
    'Returning - nothing.

    Sub AddComponent(objXMLDoc, newSettingsNode, compName)
    Set newCompoentNode = objXMLDoc.createElement("component")
    Set newComputerNode = objXMLDoc.createElement("ComputerName")
    Set newComputerNodeText = objXMLDoc.createTextNode(compName)
    newComputerNode.appendChild newComputerNodeText
    newComputerNode.setAttribute "xmlns", "urn:schemas-microsoft-com:unattend"
    newCompoentNode.appendChild newComputerNode
    newCompoentNode.setAttribute "name","Microsoft-Windows-Shell-Setup"
    systemArch = wshShell.ExpandEnvironmentStrings("%PROCESSOR_ARCHITECTURE%")
    newCompoentNode.setAttribute "processorArchitecture",systemArch
    newCompoentNode.setAttribute "publicKeyToken",""
    newCompoentNode.setAttribute "language",""
    newCompoentNode.setAttribute "versionScope",""
    newCompoentNode.setAttribute "xmlns", "urn:schemas-microsoft-com:unattend"
    newSettingsNode.appendChild newCompoentNode
    End Sub

    'Function for modifying non-vista configuration file (sysprep.inf)
    'Input parameter - nothing
    'Returning - nothing.

    Sub ModifyNonVistaConfFile()
    sysprepInfFile = systemDrive & "\sysprep\sysprep.inf"

    userDataSection = false
    computerNameFound = false

    If fso.FileExists(sysprepInfFile) Then
    Set sysprepInf = fso.OpenTextFile(sysprepInfFile, 1)

    Do Until sysprepInf.AtEndOfStream
    nextLine = sysprepInf.ReadLine
    Set re = new regexp
    re.Pattern = "^\["
    re.Global = True
    If re.Test(Trim(nextLine)) Then
    'Start of new section
    If InStr(Trim(nextLine), "[UserData]") Then
    userDataSection = true
    Else
    'Start of non user data section
    If userDataSection = true and computerNameFound = false Then
    'Completes the UserData section but didn't find "ComputerName"
    newLine = Space(4) & "ComputerName=" & Trim(coputerName)
    sysprepInfTemp = sysprepInfTemp & newLine & VbCrLf
    userDataSection = false
    computerNameFound = true
    End if
    End If
    Else
    If InStr(Trim(nextLine), "ComputerName") Then
    '"ComputerName" found
    computerNameFound = true
    nextLine = Space(4) & "ComputerName=" & Trim(coputerName)
    End If
    End If
    sysprepInfTemp = sysprepInfTemp & nextLine & VbCrLf
    Loop

    sysprepInf.Close
    End If

    'End of file but did not find "ComputerName
    If computerNameFound = false Then
    If userDataSection = false Then
    'Didn't get either computer name or user data section
    sysprepInfTemp = sysprepInfTemp & "[UserData]" & VbCrLf
    End If
    nextLine = Space(4) & "ComputerName=" & Trim(coputerName)
    sysprepInfTemp = sysprepInfTemp & nextLine & VbCrLf
    End If

    'Modify sysprep.inf, second argument 2 - means create file if it doesn't exist.
    Set sysprepInf = fso.OpenTextFile(sysprepInfFile, 2, True)
    sysprepInf.WriteLine sysprepInfTemp
    sysprepInf.Close
    End Sub - cdalley 10 years ago
    • We use this: http://www.kace.com/support/resources/kb/article/get-set-computername
      I ran into similar issues with the canned tasks that came with the K2, but these seem to work better. What is the order of your mid-level tasks? - nheyne 10 years ago
      • The only mid-task we are running at the moment is the said Apply Computer Name. I will give the get/set computer name scripts a go. I just wanted to try those first rather than implement third-party scripts.

        Cheers - cdalley 10 years ago
  • Check the PETemp folder on Kace once the Collect Name script runs to verify if it first getting collected. Are you using sysprep? If so, random name means the * is not getting replaced in the unattend.xml. You could also uncheck the auto restart, and once the image is done, go to Recovery and open your unattend. You should see the new name injected in there instead of the * for ComputerName. That should at least tell you which step is not working. Check C:\Windows\Panther\unattend.xml too make sure that doesn't have the * either. - SDNBTP 10 years ago
  • Working perfectly with the script you advised nheyne.

    Just one other question - is there anyway of getting a prompt to come up if the computer did not have an allocated name previously i.e. completely wiped hard drive?


    Cheers - cdalley 10 years ago
    • add /dialog to the Get computer name command line and that will give you a prompt. - andrew_lubchansky 10 years ago
      • hanks for that mate, is there a way to get it to check if there was a name acquired and if, then prompt? Would that require my own scripting or is there a function for this. - cdalley 10 years ago
  • Also, my workmate advised me that his old RIS server was able to read the GUID of the machine in question and check Active Directory as to what that GUID applies to i.e. Machine name - does KACE have this ability. - cdalley 10 years ago
    • As far as the prompt, /dialog will always prompt. The prompt will show you the current name and give you an opportunity to change it. If you just want to grab what is there and keep it, leave the /dialog off. You can have 2 tasks for this and use them when you need to.

      You may want to look into WSName if you want to start doing some more automation with naming. - andrew_lubchansky 10 years ago
      • If I leave the flag there would it try pick up the previous computer name and place it in this dialog field to allow us to accept/change it or it would it just wait for the input? - cdalley 10 years ago
  • It will place the previous name in the field and you just press OK to keep it. - andrew_lubchansky 10 years ago
    • Excellent. That will do for now. Cheers. - cdalley 10 years ago
  • Hi Team,

    Is there any script out there that could attain a computer's GUID and confirm its set name within Active Directory and apply that?

    Just an idea for future. - cdalley 10 years ago
    • I think there is a user voice on that, but it may be geared toward polling the K1 and not AD. - nheyne 10 years ago
      • That would work also, any ideas on how to get it to poll K1? - cdalley 10 years ago
    • Found the articles: https://kace.uservoice.com/forums/82717-k2000/suggestions/1301473-automatic-naming-of-computer-from-k1000
      http://www.itninja.com/question/name-machines-via-k1-with-k2-post-install-script - nheyne 10 years ago
 
This website uses cookies. By continuing to use this site and/or clicking the "Accept" button you are providing consent Quest Software and its affiliates do NOT sell the Personal Data you provide to us either when you register on our websites or when you do business with us. For more information about our Privacy Policy and our data protection efforts, please visit GDPR-HQ