Jump to content

Computer name containing the running script


Go to solution Solved by Tippex,

Recommended Posts

I wish to put my compiled script on multiple servers (a single executable compiled from a single source script file with users given only a desktop shortcut to one of them) and for the script to discover which of the servers it is being run from.

@ComputerName just returns the user's computer name

@ScriptDir just gives the server's path to it but not its name

I realise that I could put the executable in different folders (then interrogating with @ScriptDir) or compile multiple times having changed a constant holding the ServerName but is there anything I can use like a "@ScriptHostName"?

Edited by Tippex
Link to comment
Share on other sites

Check the IP address of the machine that it's being run on, that might tell you which server it's located on.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

  • Moderators

Why not have the script write out to a log file? Have it write @Scriptdir to a single log file on a central server, with the @Username, as soon as it is launched. That should provide you with the server name in the share path.

"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

Check the IP address of the machine that it's being run on, that might tell you which server it's located on.

 

 

Unfortunately that would be the IP of the user's own PC where the shortcut is and not the IP of the server where the executable is (and a cross-referencing table would be messy).

I tried @AutoItExe too, but the hostname isn't given by it.

I wondered whether there was some way of querying the hostname using the Process ID from @AutoItPID or some UDF technique?

Edited by Tippex
Link to comment
Share on other sites

I found this in the help file.

#include <APIFilesConstants.au3>
#include <MsgBoxConstants.au3>
#include <WinAPIFiles.au3>
#include <WinAPISys.au3>

If _WinAPI_GetVersion() < '6.0' Then
    MsgBox(BitOR($MB_ICONERROR, $MB_SYSTEMMODAL), 'Error', 'Require Windows Vista or later.')
    Exit
EndIf

Local $hFile = _WinAPI_CreateFile(@ScriptFullPath, 2, 0, 6)

ConsoleWrite(_WinAPI_GetFinalPathNameByHandleEx($hFile) & @CRLF)
ConsoleWrite(_WinAPI_GetFinalPathNameByHandleEx($hFile, $VOLUME_NAME_GUID) & @CRLF)
ConsoleWrite(_WinAPI_GetFinalPathNameByHandleEx($hFile, $VOLUME_NAME_NT) & @CRLF)
ConsoleWrite(_WinAPI_GetFinalPathNameByHandleEx($hFile, $VOLUME_NAME_NONE) & @CRLF)

_WinAPI_CloseHandle($hFile)

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Why not have the script write out to a log file? Have it write @Scriptdir to a single log file on a central server, with the @Username, as soon as it is launched. That should provide you with the server name in the share path.

 

The central log file would then have the server path & user ID but not the server name. If the server path storing the executable varied then the server name could be worked out but it's a messy solution unfortunately.

Link to comment
Share on other sites

 

I found this in the help file.

#include <APIFilesConstants.au3>
#include <MsgBoxConstants.au3>
#include <WinAPIFiles.au3>
#include <WinAPISys.au3>

If _WinAPI_GetVersion() < '6.0' Then
    MsgBox(BitOR($MB_ICONERROR, $MB_SYSTEMMODAL), 'Error', 'Require Windows Vista or later.')
    Exit
EndIf

Local $hFile = _WinAPI_CreateFile(@ScriptFullPath, 2, 0, 6)

ConsoleWrite(_WinAPI_GetFinalPathNameByHandleEx($hFile) & @CRLF)
ConsoleWrite(_WinAPI_GetFinalPathNameByHandleEx($hFile, $VOLUME_NAME_GUID) & @CRLF)
ConsoleWrite(_WinAPI_GetFinalPathNameByHandleEx($hFile, $VOLUME_NAME_NT) & @CRLF)
ConsoleWrite(_WinAPI_GetFinalPathNameByHandleEx($hFile, $VOLUME_NAME_NONE) & @CRLF)

_WinAPI_CloseHandle($hFile)

 

To create the file on the server where the script is being run from, I would first need to know its hostname (to prefix the @scriptfullpath).

Link to comment
Share on other sites

The script already knows what its name is. It's not technically "creating" the file, just opening it for access.

Edited by BrewManNH

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

The script already knows what its name is. It's not technically "creating" the file, just opening it for access.

 

Local $hFile = _WinAPI_CreateFile(@ScriptFullPath, 2, 0, 6)

when queried with _WinAPI_GetFinalPathNameByHandleEx will discover the user's computer name by default (the client PC where the desktop shortcut is) and unfortunately not the server name where the executable is being run from.

For similar reason, dabbler's "netinfo.udf" suggestion will default to the user's PC (client) and not the host with the executable.

Exit's idea of "querying the desktop link data" may work if the user hasn't moved the shortcut to a quick launch, a browser link, another folder etc ...

 

Link to comment
Share on other sites

#include <File.au3>
#include <String.au3>
If Not @Compiled Then Exit MsgBox(64 + 262144, Default, "Please compile before using.", 0)
$aArray = _FileListToArrayRec(@DesktopDir, "*.lnk", 0, 0, 0, 2)
For $i = 1 To $aArray[0]
    $text = FileRead($aArray[$i])
    If Not StringInStr($text, @ScriptFullPath) Then ContinueLoop
    $aserver = _StringBetween($text, "\\", "\")
    Exit MsgBox(262144, Default, "Servername: " & $aserver[0], 0)
Next

App: Au3toCmd              UDF: _SingleScript()                             

Link to comment
Share on other sites

Have you actually taken this script I posted and run it? Because when I have a script housed on a server, and run it from my computer I get this.

 

?UNCussafile02homedirredactedtest.au3

DeviceMupussafile02homedirredactedtest.au3
ussafile02homedirredactedtest.au3

The script is housed on ussafile02 in my homedir folder on that server.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Have you actually taken this script I posted and run it? Because when I have a script housed on a server, and run it from my computer I get this.

 

I did try but unfortunately it fails to work with Windows Server 2008, even when commenting out the "Vista" check and using messageboxes for a compiled exe.

I thought Windows Server 2008 came out 2 years later than Vista. 

Exit's suggestion is a great idea but may give a problem if a user's normal server is down and is given an alternation "workaround" shortcut to another server while the problem is sorted.

Link to comment
Share on other sites

I guess I'm a bit confused.

 

@ScriptDir just gives the server's path to it but not its name

Is it returning the mapped drive (e.g. X:blah)?

If so, could you just use DriveMapGet("X:") to get the UNC resource path of the mapped drive?  That should contain the server name, which you could then just parse out of the string.

It works for me in Windows 7 and 2008.

Link to comment
Share on other sites

What does @ScriptDir return for you? Is it over a drive letter share?  Or is it a UNC path?  Is the UNC path the IP or the Name?

You could parse out whatever it is, start with a letter, do a DriveMapGet(), if it's a named unc, just parse the string out of it.  

If it's an IP you'll need to figure out a way to reverse look it up (nslookup?) (why would it matter though?  If they are are executing via IP, can't you go back to the IP?) 

Link to comment
Share on other sites

I just tossed this together and it returns just the servers name in all 3 scenarios I tried (drive letter share, named UNC, IP UNC)

 #include <Inet.au3>
 
$sPath = _GetScriptServer()
ConsoleWrite($sPath & @CRLF)
 
Func _GetScriptServer()
Local $asReturn, $sHost
If StringLeft(@ScriptDir, 2) <> "\\" Then
If DriveGetType(StringLeft(@ScriptDir, 2)) = "Network" Then
$asReturn = StringSplit(StringTrimLeft(DriveMapGet(StringLeft(@ScriptDir, 2)), 2), "\", 2)
Return $asReturn[0]
EndIf
Else
$asReturn = StringSplit(StringTrimLeft(@ScriptDir, 2), "\", 2)
If StringIsAlNum($asReturn[0]) Then
; UNC via Name
Return $asReturn[0]
Else
; UNC via IP
TCPStartup()
$sHost = _TCPIpToName($asReturn[0])
TCPShutdown()
If StringIsAlNum($sHost) Then
Return $sHost
Else
$asReturn = StringSplit($sHost, ".", 2)
Return $asReturn[0]
EndIf
EndIf
EndIf
EndFunc
Link to comment
Share on other sites

YES!!! Your script works ... Thank you.

An XP client with a shortcut to a server executable with @ScriptDir will just return a drive letter path if a drive letter is mapped to the server,

but a UNC path if not.

In each case, your script does reveal the true servername so I now only need to compile once (not multiple times embedding the server name). 

Thank you again. :-)

Link to comment
Share on other sites

  • Solution

 

I just tossed this together and it returns just the servers name in all 3 scenarios I tried (drive letter share, named UNC, IP UNC)

 

 

 

I had to change this line:

From:

$asReturn = StringSplit(StringTrimLeft(@ScriptDir, 2), "", 2)

 

To:

$asReturn = StringSplit(StringTrimLeft(@ScriptDir, 2), ".", 2)

Inclusion of a full stop in the delimiter as some servernames can have domain names added (and full stops are not alphanumerics). 

 

but add the original line back in before the "IP" check.

Func _GetScriptServer()
Local $asReturn, $sHost
If StringLeft(@ScriptDir, 2) <> "\\" Then
    If DriveGetType(StringLeft(@ScriptDir, 2)) = "Network" Then
        $asReturn = StringSplit(StringTrimLeft(DriveMapGet(StringLeft(@ScriptDir, 2)), 2), "\", 2)
        Return $asReturn[0]
    EndIf
Else
    $asReturn = StringSplit(StringTrimLeft(@ScriptDir, 2), "\.", 2)
    If StringIsAlNum($asReturn[0]) Then
        ; UNC via Name
        Return $asReturn[0]
    Else
        ; UNC via IP
        $asReturn = StringSplit(StringTrimLeft(@ScriptDir, 2), "\", 2)
        TCPStartup()
        $sHost = _TCPIpToName($asReturn[0])
        TCPShutdown()
        If StringIsAlNum($sHost) Then
            Return $sHost
        Else
            $asReturn = StringSplit($sHost, ".", 2)
            Return $asReturn[0]
        EndIf
    EndIf
EndIf
EndFunc   ;==>_GetScriptServer
Edited by Tippex
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...