Powershell-在远程主机上登录用户并将用户名存储为变量

Powershell-在远程主机上登录用户并将用户名存储为变量,powershell,wmi,wmi-query,wmic,Powershell,Wmi,Wmi Query,Wmic,尝试使用Get-WmiObject获取远程pc上当前或上次登录的用户名,然后将该值存储到变量中,以便以后将其用作路径的一部分 我尝试了这个,但是我的变量是空的。有什么想法吗?提前谢谢 $gottenUserName = (Get-WmiObject -ComputerName $thaTargetHost -Namespace root\cimv2 -Class Win32_ComputerSystem)[0].UserName; 由于使用此类时不需要索引项[0],因此请取出末尾的索引项。您将

尝试使用Get-WmiObject获取远程pc上当前或上次登录的用户名,然后将该值存储到变量中,以便以后将其用作路径的一部分

我尝试了这个,但是我的变量是空的。有什么想法吗?提前谢谢

$gottenUserName = (Get-WmiObject -ComputerName $thaTargetHost -Namespace root\cimv2 -Class Win32_ComputerSystem)[0].UserName;

由于使用此类时不需要索引项[0],因此请取出末尾的索引项。您将仅从Win32_计算机系统中获取一个值

$gottenUserName = (Get-WmiObject -ComputerName $thaTargetHost -Namespace root\cimv2 -Class Win32_ComputerSystem).UserName

我仍然没有得到任何值,当您只是查询Win32_ComputerSystem时,用户名属性中是否显示了任何内容?获取WmiObject-ComputerName$thaargethost-Class Win32|U ComputerSystem |选择用户名Win32|U ComputerSystem没有用户名属性Y它有用户名属性。看见属性描述在页面底部附近。啊,它不是显示的属性之一,但确保它在那里。谢谢@boeprox