我的powershell脚本确定远程Windows 7计算机的当前用户并将输出
userId=DOMAIN\username
如果当前没有用户登录,则脚本将输出
userId=No One Currently Logged In
如果脚本无法访问远程计算机的WMI,则脚本将输出
userId=CannotConnectToWMI
我运行脚本并运行WBEMTEST以确认是否可以在远程计算机上访问WMI。
我真的很困惑因为昨天下午,我能够在几台远程机器上访问WMI,今天早上我不能。下面是一张图表:
为什么会这样? 如何确保WMI始终可访问?我昨天发布了另一个关于WMI的问题,https://stackoverflow.com/questions/19409747/wbemtest-to-windows-7-says-the-rpc-server-is-unavailable
请帮忙
@vonPryz
该脚本具有Test-Connection。以下是整个脚本
$line_array = @()
$multi_array = @()
[hashtable]$my_hash = @{}
$Sender_IP = $NULL
$bios = $NULL
$wmi = $NULL
foreach ($i in $args){
$line_array+= $i.split(" ")
}
foreach ($j in $line_array){
$multi_array += ,@($j.split("="))
}
foreach ($k in $multi_array){
$my_hash.add($k[0],$k[1])
}
$Sender_IP = $my_hash.Get_Item("sender-ip")
try{
Test-Connection $Sender_IP -count 1 -ErrorAction Stop | out-null
}
catch [Exception]
{
$userId = "userId=CannotPing"
return $userId
}
try{
$wmi = gwmi -class win32_computerSystem -computer $Sender_IP -ErrorAction Stop
}
catch [Exception]{
$userId = "userId=CannotConnectToWMI"
return $userId
}
try{
$userId = ($wmi).username
}
catch [Exception]{
$userId = "userId=CannotFindLastUserLoggedOn"
return $userId
}
if ($userId -ne $NULL){
$userID = "userId="+$userId
return $userId
}
elseif ($userID -eq $NULL)
{
$userId = "userId=No One Currently Logged In"
return $userId
}
修改
我正在远程访问这些计算机以检查DCOM权限,然后我意识到其中一个变成了Windows XP。似乎IP地址正在切换到不同的计算机。我将根据完全合格的域名进行比较。
答案 0 :(得分:0)
将Test-Connection
添加到您的脚本中,并且只有在ping主机成功完成时才尝试WMI。
答案 1 :(得分:0)
我目前正在测试几个Windows 7的IP地址。当我转发到一个麻烦的IP地址时,我注意到它变成了Windows XP。然后我意识到计算机的IP地址每隔几天就会发生变化,所以10.10.10.10可能属于ComputerA.contoso.com一天,几天之后,可能属于ComputerB.contoso.com。
在我对一堆计算机进行任何测试之前,我会根据他们的完全合格域名进行测试,然后在执行任何测试之前找到相应的IP地址。