我有一个从 Jenkins 服务器启动的程序。
我使用 pyinstaller 创建了一个 exe 并将其安装在两台计算机上。 然后詹金斯称他们俩。
起初我使用os.path.expanduser('~'(来获取用户路径,但它返回
"C:WindowsSystem32configsystemprofile"
在我尝试使用os.environ['USERPROFILE']获取用户名后,仍然得到:
"C:WindowsSystem32configsystemprofile"
最后,我找到了一个不需要操作系统模块的不同解决方案并尝试了:
import ctypes.wintypes
CSIDL_PERSONAL = 5 # My Documents
SHGFP_TYPE_CURRENT = 0 # Get current, not default value
buf= ctypes.create_unicode_buffer(ctypes.wintypes.MAX_PATH)
ctypes.windll.shell32.SHGetFolderPathW(None, CSIDL_PERSONAL, None,
SHGFP_TYPE_CURRENT, buf)
print(buf.value)
这在我的日志值中没有给出任何值。
如果我在本地运行 exe,所有值都会正常返回。 我运行的批处理命令是
start /wait C:JenkinsResourcesMarinaMain.exe
所以我画了一个空白,当我远程调用它时,我如何让这个程序找到它所在的计算机的用户文件夹。
因此,为了使它起作用,我首先下载了Windows Power Shell插件。 重新启动后,我使用了以下命令:
$PCAndUserName = Get-WMIObject -class Win32_ComputerSystem | select username
$UserName = [string]$PCAndUserName
$UserName = $UserName.split("\")[-1]
$UserName = $UserName -replace ".{1}$"
$UserName
这给了我计算机上当前用户的名称,无论我是否使用 Jenkins 远程启动。