I want to distribute a script to a range of PCs that uses their names as a variable.
WinAPI32.GetComputerName doesn't work in DelphiScript since you can't pass parameters by reference.
I don't want to use the example that depends on interrogating the log as I don't intend for the client PCs to maintain a log.
Is there another way to get a PC's name at run-time?
You can convert this to run in DelphiScript as well:
strComputer = "."
strMsg = ""
Set objWMIService = GetObject("winmgmts:\\" & strComputer)
Set colItems = objWMIService.ExecQuery("Select * from Win32_ComputerSystem",,48)
For Each objItem In colItems
strMsg = strMsg & "Manufacturer: " & objItem.Manufacturer & vbCrLf
strMsg = strMsg & "Model: " & objItem.Model & vbCrLf
strMsg = strMsg & "Name: " & objItem.Name
MsgBox strMsg, vbInformation, "WMI"
If you have time, look into the PrimalScript IDE 30 day trial. Even if you decide not to purchase it you will get much more information about using the WMI. I use the PrimalScript IDE with TC...it makes for quick development. I like the WMI Wizard, OLE Server panel, and the Snippet functionality. Maybe you will find other uses.
Thanks. I actually meant the name of the PC in the network, which I found in the registry and am hoping is in the same place in all versions of Windows:
</font><blockquote><font size="1" face="Verdana, Arial, Helvetica">code:</font><hr /><pre style="font-size:x-small; font-family: monospace;">function GetComputerName;
end;</pre><hr /></blockquote><font size="2" face="Verdana, Arial, Helvetica">
In my example above the objItem.Name does return the name of the computer just as the GetComputerName API function and as your GetComputerName function. I included the objItem.Manufacturer and objItem.Model to demonstrate how you can return other computer specific properties. This may not be of importance to you but I use many of the WMI functionality for gathering metrics as our scripts run.
By the way I did find the computer name in the above mentioned Registry locations for all of our XP Pro machines and all of our 2000 Server machines. All of our 2003 Server machines had Control001 and Control002 nodes rather than the Control only node. However, this may only be specific to our OS configurations.
You can also get the computer name using the ComputerName property of the WScript.Network OLE object:
CompName := Sys.GetOleObject('WScript.Network').ComputerName;
All good stuff - opened up a whole new line of enquiry - thanks!
you can use foloowing:
gl_szTestBox = tsEnviron( "COMPUTERNAME" );
function tsEnviron( szVariable )
var WshShell = new ActiveXObject("WScript.Shell");
var szExpanded = WshShell.ExpandEnvironmentStrings("%" + szVariable + "%");
if( szExpanded == szVariable )