2014-07-01 68 views
0

嘗試使用Get-WmiObject獲取遠程PC上當前或最近一次登錄的用戶名,然後將該值存儲到變量中,以便日後使用它作爲路徑的一部分。Powershell - 在遠程主機上登錄用戶並將用戶名作爲變量登錄

我嘗試這個,但我的變量只是空白。有任何想法嗎?提前致謝。

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

回答

0

在結束時取出索引片([0]),因爲使用此類時不需要索引片([0])。你只會從的Win32_ComputerSystem

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

我還沒有得到任何價值 – superKing

+0

當你剛剛查詢的Win32_ComputerSystem找回一個值,是有顯示用戶名屬性了什麼? 'Get-WmiObject -ComputerName $ thaTargetHost -Class Win32_ComputerSystem |選擇用戶名' – boeprox

+0

Win32_ComputerSystem沒有用戶名屬性 –