variable / command to get Hostname

RAL6000
Advocate

variable / command to get Hostname

RAL6000
Advocate
Advocate

Has Anyone an idea how to get the Computers Hostname within a macro?

 

I already have the variable "user_id()" for getting the Username. But i also would like to have the Hostname.

 

 


- - - - - - - - - - - - - - - - - - - - - - - - - -
kind regards Stefan,
Milling robot integrator | Germany
0 Likes
Reply
Accepted solutions (1)
1,070 Views
2 Replies
Replies (2)

Anonymous
Not applicable
Accepted solution

These two macros return Hostname and HostID using the ipconfig / all command, but you can use hostname or getmac / v instead

GetHostName.mac:

FUNCTION MAIN() {
   STRING bat= macro_path(0)+"\getHost.bat"
   STRING file1= macro_path(0)+"\getHost.txt"
   STRING file2= macro_path(0)+"\getHost.OK"
   IF file_exists($file1) {
      DELETE FILE $file1
   }
   IF file_exists($file2) {
      DELETE FILE $file2
   }
   STRING cmd='ipconfig /all | find  "Host Name" >'+$file1+'"' +crlf+'ECHO OK >"'+$file2+'"'
   FILE OPEN $bat FOR WRITE AS "output"
   FILE WRITE $cmd TO "output"
   FILE CLOSE "output"
   OLE FILEACTION "OPEN" $bat
   WHILE NOT file_exists($file2) {
      INT I=0
   }
   STRING HostText=""
   FILE OPEN $file1 FOR READ AS "file1"
   FILE READ $HostText FROM "file1"
   FILE CLOSE "file1"  
   DELETE FILE $bat
   DELETE FILE $file1
   DELETE FILE $file2
   STRING LIST HostName=tokens($HostText,":")
   IF size($HostName) == 2 {
      MESSAGE INFO 'Host Name: '+trim($HostName[1])
   } ELSE {
      MESSAGE INFO 'Something wrong :('
   }
}

GetHostId.mac:

FUNCTION MAIN() {
   STRING bat= macro_path(0)+"\getHostID.bat"
   STRING file1= macro_path(0)+"\getHostID.txt"
   STRING file2= macro_path(0)+"\getHostID.OK"
   IF file_exists($file1) {
      DELETE FILE $file1
   }
   IF file_exists($file2) {
      DELETE FILE $file2
   }
   STRING cmd='ipconfig /all | find  "Physical Address" >'+$file1+'"' +crlf+'ECHO OK >"'+$file2+'"'
   FILE OPEN $bat FOR WRITE AS "output"
   FILE WRITE $cmd TO "output"
   FILE CLOSE "output"
   OLE FILEACTION "OPEN" $bat
   WHILE NOT file_exists($file2) {
      INT I=0
   }
   STRING LIST HostText={}
   FILE OPEN $file1 FOR READ AS "file1"
   FILE READ $HostText FROM "file1"
   FILE CLOSE "file1"
   DELETE FILE $bat
   DELETE FILE $file1
   DELETE FILE $file2
   INT I= 0
   STRING MSG=""
   WHILE $I < size($HostText) {
      STRING LIST HostId=tokens($HostText[$I],":")
      IF size($HostId) == 2 {
         $MSG=$MSG+trim($HostId[1]) +crlf
      }
      $I=$I+1
   }
   IF length($Msg) >0 {
      MESSAGE INFO $MSG
   } ELSE {
      MESSAGE INFO 'Something wrong :('
   }
}

RAL6000
Advocate
Advocate

Thank you very much, you gave me the Right idea to create a macro based on your Suggestion to fit my Needs.

 

Command to use it:

CALL GetInfoViaCMD($RETURN_VALUE, "computername")

 

Functions.inc

// ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//Function to get Windows Informations with the help of a batch command
// INFO: https://de.wikibooks.org/wiki/Batch-Programmierung:_Batch-Befehle
// IDEA: https://forums.autodesk.com/t5/powermill-forum/variable-command-to-get-hostname/td-p/8989217
//CALL GetInfoViaCMD($RETURN_VALUE, "computername")
FUNCTION GetInfoViaCMD(
OUTPUT STRING ret_info
STRING type
){
	//Set paths for temporary files
		STRING parentfolder = "C:\Windows\Temp\" //Alternativ: macro_path(0), but in my case its contect is empty/undefined
		STRING path_bat 	= $parentfolder + "temp_GetInfosViaCMD.bat"	//batch file which contains the Commands for the questioned informations
		STRING path_bat_done= $parentfolder + "temp_GetInfosViaCMD.DONE"//will be created after Batch file commands have been processed, this is needed that our macro waits for the batch file to be processed complety
		STRING path_result  = $parentfolder + "temp_GetInfosViaCMD.txt" //consists Results exported during Batch Commands
	//Delete temporary file, in case it already exists
		IF file_exists($path_bat) {
			DELETE FILE $path_bat
		}
		IF file_exists($path_bat_done) {
			DELETE FILE $path_bat_done
		}	
		IF file_exists($path_result) {
			DELETE FILE $path_result
		}		
	//create Batch Command
		//Batch has several lines and will look like:
		// (
		// ECHO %computername%
		// ECHO %username%
		// ECHO %userprofile%
		// ) > "C:\Windows\Temp\... .txt"
		STRING $cmd_lines 	 = '(' +crlf+ 'ECHO %computername%' +crlf+ 'ECHO %username%' +crlf+ 'ECHO %userprofile%' +crlf+ ') > ' + '"'+$path_result+'"'
		STRING $cmd_bat_done = 'ECHO BATCH_FILE_DONE > ' + '"'+$path_bat_done+'"'
	//create .bat File with the command lines and start batch
		FILE OPEN $path_bat FOR WRITE AS "output"
			FILE WRITE $cmd_lines TO "output"
			FILE WRITE $cmd_bat_done TO "output"
		FILE CLOSE "output"
		OLE FILEACTION "OPEN" $path_bat
		
	//Open result File written from batch commands and read into internal variable
		STRING LIST HostTextList = {}
		//wait for Batch processed and creates the .txt File
		WHILE NOT file_exists($path_bat_done) {
			//wait for .bat file is executed and .DONE file is created as signal to go ahead
		}	
		FILE OPEN $path_result FOR READ AS "results"
			FILE READ $HostTextList FROM "results"
		FILE CLOSE "results" 
		//Delete Temporary files:
		DELETE FILE $path_bat
		DELETE FILE $path_bat_done
		DELETE FILE $path_result

	//set results from internal variable to output variable	depending on type
		SWITCH type {
			CASE "hostnameANDusername"
				$ret_info = "Hostname: " + $HostTextList[0] + " | Username: " + $HostTextList[1]
			BREAK
			
			CASE "computername"
			CASE "hostname"
			CASE "HostName"
				$ret_info = $HostTextList[0]
				BREAK	
				
			CASE "username"
			Case "user"
				//Alternative exists inside PowerMill: user_id()
				$ret_info = $HostTextList[1]
				BREAK
				
			CASE "userprofile"
				$ret_info = $HostTextList[2]
				BREAK
				
			DEFAULT
				$ret_info = "error"
				BREAK
		}

	//Close Function
}

- - - - - - - - - - - - - - - - - - - - - - - - - -
kind regards Stefan,
Milling robot integrator | Germany