All templates
ScriptingAdvanced
Get Login Name Using Python
Robomotion•Updated 6 months ago

Overview
Retrieves the current Windows login user via an inline Python script. Handy for audit trails or user-scoped paths.
Get Login Name Using Python
Besides the Python built-in functions, Robomotion enables users to extend scripting capabilities using external modules. External modules facilitate code reuse and prevent users from redeveloping the same functionality.
What Get Login Name Using Python can do
Core.Process.StartProcessrunspowershellwith-NoProfile -Command $env:USERNAMEin the foreground and captures stdout intomsg.login_name_raw.Core.Programming.Functiontrims trailing CR/LF frommsg.login_name_rawand composesmsg.dialog_text = 'Machine login name: ' + user.Core.Dialog.MessageBoxtitledLogin name(typeinfo) displaysmsg.dialog_text, thenCore.Flow.Stop.
Behind the scenes
- The flow shells out to PowerShell rather than Python because
$env:USERNAMEis available on every stock Windows host, while a Python interpreter is not guaranteed. The template name is retained from the original; swap the process call forpython -c "import getpass; print(getpass.getuser())"if a Python runtime is required for parity. -NoProfileskips user profile scripts so login shells with slow or interactive profiles do not stall the flow.- The trim loop guards against both
\r\n(Windows) and\n-only (WSL/PowerShell Core) output so the dialog never shows a trailing blank line. getpass.getuser()(or$env:USERNAME) returns the environment user; on shared-session hosts this may differ from the interactive desktop user — use a Win32 API call if that distinction matters.