SOLVED Need a way to pass variables in Powershell using hostname, primary user, other tag #1 or other tag #2

  • I don’t know the best way to phase this in a question so I will try my best to describe my end goal.

    I am trying to establish a provisioning process so that during full host registration I can enter the Active Directory username then later on in the provisioning process FOG will (either via a SnapIn or Sysprep Powershell script) use the username to do the following:

    • Change the computer name to equal $username + append a date to that name. Example Computername = username191206 (today’s date) or $computerName = "${username}${Date}"
    • Add the username as an administrator to the computer. (Example: NET LOCALGROUP administrators ${DOMAIN_NAME}\${username} /add
    • Disable the current user and add the username as the ‘LastLoggedOnUser’

    Currently during full host registration I can use the host name for my username then after FOG changes the computer name to equal the host name, I can use $env:computername as the variable for my username in my SnapIn or Powershell script. The biggest problem with that is I don’t see any way to append a date to the computer name without FOG changing the computer name back after a computer reset.

    What would be preferable to me is if I could use the “primary user” or “tag #1” or “tag #2” I set during host registration and pass that as the $username variable. I just don’t see any way to do that. Is this possible? Does what I am trying to do make sense?

    Thanks in advance for any help or knowledge you can provide.

  • Hey @Daniel-Miller and @george1421 thanks again for your effort in helping me figure this out. I think the complexity (for me) for what I was about to do scared me into thinking of a roundabout approach to solving this issue. I think I found a better solution than what I was originally trying to do (and WAY simpler). I will post here in case this will be of use for anyone in the future.

    Rather than appending a date to the hostname so I can change the computer name I decided to start out with the date appended to the hostname so that renaming the computer would be exactly how I want it to be then for my other requirements (adding the user to be admin of the computer) I simply removed the date from the hostname to come up with the username.

    Basically, if my username is bobsmith and today’s date is 2019-12-06, I will name the host: bobsmith191206 then in my Powershell script I just need to set $userName = $env:computername -replace '[0-9]' then the hostname remains bobsmith191206 and now I have a variable $userName that now equals BOBSMITH

    Anyhow, that is one of those… so easy it is dumb solutions. Not sure why I didn’t think of it earlier. Thanks again you guys for helping me to think outside of the box I stuck myself in.

  • Hey @george1421 thanks for your active involvement here and for your link! It looks promising. I will see what I can figure out.

  • Moderator

    @mckay Well if you know the user’s name at imaging time a post install script could be written to mash up the user name and current date and update the unattend.xml file with the host name and you wouldn’t need to run the PS script.

    I have a tutorial on how to write a post install script. Its more copy and paste than anything:

    If you look in the fog.updateunattend section you will see the example on how to update the unattend.xml file dynamically at deployment time.

  • Thanks guys for your responses. I was planning on disabling FOG from managing the host name after I figured out a way to get the variables I need to pass in my SnapIn file. @george1421 I already have an unattend.xml file running so the initial naming of the computer is not a huge concern for me; just the naming of the computer after the fact.

    The links and examples you guys responded with I think help a bit but are beyond the scope of my current capabilities (which is fine… guess it is time to learn something new); hopefully I can use that info to get started. (As should be obvious at this point, I am not at all a developer; just trying to figure out what I can)

    Thanks again!

  • Moderator

    Really what I think I would do here is a combination of a post install script to update an unattend.xml file and then disable the FOG host name change function. Then you can push out a PS script or .vbs script to change the name of the target computer after imaging is complete.

    As Daniel said, you can use the hostinfo.php script to get the kernel parameters from the FOG server.

    Here is the curl call that FOG uses to pick up the parameters during a usb boot.

    curl -Lks -o /tmp/hinfo.txt --data "sysuuid=${sysuuid}&mac=$mac" "${web}service/hostinfo.php" -A ''

    Now the trick here is that you can not pass the browser settings to the fog server or it will reject the call. But you can make a similar curl call from a powershell prompt.

    So the workflow would go is to use a postinstall script to update the host name value in the unattend.xml file on the target computer then when the computer runs through OOBE it will use that name to assign to the computer. Since the fog service is not configured to change the host name the name set by the unattend.xml file will stay. At some time in the future you will assign a snapin or via some other method run you PS script to reset the name from the fields you get from the fog server like primary user.

  • You might be able to use hostinfo.php to pull down the variables for the machine. I would think it would have those fields available.

    If you didn’t want FOG to manage the hostname, you could disable that in the service settings. I also remember something about a FOG API someplace, but haven’t played with it yet…