Jump to content

Capture hostname output directly ?


Recommended Posts

I would like to find, if possible of course, the AutoIt statement that would collect in a $variable the output of the command "hostname" (executable located in system32 btw).

Before AutoIt (in my batch file period) I would redirect its output to a file but I would like to avoid that inelegant method (that if possible again).

I saw so far no possibilities with the Run nor the ShellExecute commands, but tbh I'm only an AutoIt newbie ^_^

I've however found the registry key that seems to hold that value, via the following statement:

     $var=RegRead("HKEY_LOCAL_MACHINESYSTEMControlSet001servicesTcpipParameters", "Hostname")

But I'm really curious to know if what I wanted to do initially is possible ? Thanks for your inputs !

PS: nope, I'm not interested in fetching the computername (e.g. @COMPUTERNAME) as this is not exactly the same thing.

Link to comment
Share on other sites

  • Moderators

The stated properties of the Hostname command state that it returns the host name portion of the full computer name. Can you please explain how that is different from the @ComputerName macro?

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

The stated properties of the Hostname command state that it returns the host name portion of the full computer name. Can you please explain how that is different from the @ComputerName macro?

The @ComputerName (as the %COMPUTERNAME¨% environment variable which I understood it fetches) is seemingly always in full caps. The hostname may not only contain small caps (is the case on all my computers) but also spaces (if I remember right, but never used) and accents (which I used also but clearly try to avoid) and special characters.

Edited by AlterMind
Link to comment
Share on other sites

  • Moderators

I tried this on several machines, with underscore and another with other special characters. Seems to get what you're looking for:

MsgBox(0, "", StringLower(@ComputerName))

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

or WMI  

via UEZ:  )'>

(my slow ass way using 'ping -a' is at the bottom of that thread, but that effort was purely academic)

MsgBox(0, "Test", WMI_DNSHostName("localhost"))

Func WMI_DNSHostName($srv)
    Local $objWMIService = ObjGet("winmgmts:{impersonationLevel = impersonate}!\\" & $srv & "\root\cimv2")
    Local $DNSHostName, $colItems, $colItem, $ping
    $ping = Ping($srv)
    If $ping Then
        $colItems = $objWMIService.ExecQuery("SELECT DNSHostName FROM Win32_ComputerSystem", "WQL", 0x30)
        If IsObj($colItems) Then
            For $objItem In $colItems
                $DNSHostName = $objItem.DNSHostName
            Next
            SetError(0)
            Return $DNSHostName
        Else
            SetError(1)
            Return "Error!"
        EndIf
    Else
        SetError(1)
        Return "Host not reachable"
    EndIf
EndFunc
Edited by boththose

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...