none
Issue using powershell to get computername to use in a variable RRS feed

  • Question

  • I am having an issue getting the correct value from Powershell when using this:

    $ComputerName =  env:computername
    $FileName = $ComputerName + ".prefs"

    When I do that, I get the value of MOST of the computername, however not the full computername.

    What comes back is: FAKETEXT-WIN732_.prefs

    It should be FAKETEXT-WIN732_vm.prefs

    The computername is FAKETEXT-WIN732_vm

    Is this because of the underscore in the computername? How do I circumvent this?

    Thanks!

    Jesse Rooney

    Tuesday, January 7, 2014 9:46 PM

Answers


  • $FileName = "$ENV:COMPUTERNAME.prefs"

    Bill

    • Proposed as answer by jrv Tuesday, January 7, 2014 11:07 PM
    • Marked as answer by J ROON Tuesday, January 7, 2014 11:08 PM
    Tuesday, January 7, 2014 11:02 PM
    Moderator

All replies

  • I believe the underscore is an acceptable character for a computer name and so that may not the issue. When you manually run $env:COMPUTERNAME on the computer where the variable is not created properly, does it return the computer name with or without the underscore?

    You might try this and see if you get any thing different.

    $FileName = "$ComputerName.prefs"




    Tuesday, January 7, 2014 10:05 PM
  • This way:

    $FileName="$env:ComputerName.prefs"


    ¯\_(ツ)_/¯

    • Proposed as answer by jrv Tuesday, January 7, 2014 11:06 PM
    Tuesday, January 7, 2014 10:06 PM
  • Thank you for your very prompt responses!

    I am having the same issue trying your solution jrv.

    I did not try yours tommymaynard.

    This was easily resolved though, I changed the name to be shorter (also had to change it in my SQL server). Computer naming convention should not include underscores - just learning this now!

    The issue was related to 1 of 2 things from my observation:

    1. The underscore (known issue with computer name in previous windows OS's)

    2. Length of name (The real name was 17 characters, 15 characters were returned)


    This would be a good reference article if anyone does know the answer (or knows of any other reason this would be happening)




    • Edited by J ROON Tuesday, January 7, 2014 11:19 PM
    Tuesday, January 7, 2014 10:10 PM
  • It is not possible for a name to be returned short of characters.  There must be something wrong with your system.  To many thing are dependent on that code.  You original code is not possible.

    This will not work:

    $ComputerName =  env:computername

    If you run just that line it will throw an exception.  You must use the dollar sign:
    $ComputerName =  $env:computername

    Try it like this:

    $computername='this is a value'
    $ComputerName =  env:computername
    $computername

    You will see that it contains the original value.

    That is just the way PowerShell works.


    ¯\_(ツ)_/¯


    • Edited by jrv Tuesday, January 7, 2014 10:24 PM
    Tuesday, January 7, 2014 10:21 PM
  • To be sure that it is a mistake in yourunderstnding of how this works do this:

    dir env:computername

    You will see the whole object and its full value.


    ¯\_(ツ)_/¯

    Tuesday, January 7, 2014 10:30 PM

  • $FileName = "$ENV:COMPUTERNAME.prefs"

    Bill

    • Proposed as answer by jrv Tuesday, January 7, 2014 11:07 PM
    • Marked as answer by J ROON Tuesday, January 7, 2014 11:08 PM
    Tuesday, January 7, 2014 11:02 PM
    Moderator
  • The name was being cut off because it was over 15 characters. This should give you the full name if it's over 15 characters:

    (gwmi win32_computersystem -Property DNSHostName).DNSHostName


    And this would give you the full string you were looking for:

    "{0}.prefs" -f (gwmi win32_computersystem -Property DNSHostName).DNSHostName


    Edited to add:

    An even easier way than WMI: [System.Net.Dns]::GetHostName()

    • Edited by Rohn Edwards Wednesday, January 8, 2014 1:13 AM Added GetHostName() method
    Wednesday, January 8, 2014 1:07 AM
  • A computer name cannot be  greater than 15 characters.  It is not possible to make it greater.  The DNSHostName will reflect a name that is too long but it is NOT the NetBIOS computer name.

    The required name is not stated here.  What was stated was the name of a config file. THis file should be the same name as the NetBIOS name but I am not certain about that.

    Further simple tests are to look at the name returned by IPCONFIG /all nd the name returned by CMD:
    "echo %computername%".

    The IPCONFIG name will be the longer one if the name has been set as too long and the %computerrname% will always be the NetBIOS Compatible 15 character truncation. 


    ¯\_(ツ)_/¯



    • Edited by jrv Thursday, January 9, 2014 9:43 AM
    Wednesday, January 8, 2014 1:43 AM