我需要什么,我希望是一个非常简单的方法在PowerShell中做到这一点(我运行PowerShell脚本远程使用绝对管理):PowerShell的:检查计算机登出
if (computer is logged out)
{
<run script>
}
else
{
exit
}
大部分是我在搜索过程中发现的问题围绕着与用户登录/注销相关的更复杂的事情展开。我基本上只需要知道电脑当前是否处于登录提示符状态。
感谢您的任何和所有帮助 - 你们真棒。
我需要什么,我希望是一个非常简单的方法在PowerShell中做到这一点(我运行PowerShell脚本远程使用绝对管理):PowerShell的:检查计算机登出
if (computer is logged out)
{
<run script>
}
else
{
exit
}
大部分是我在搜索过程中发现的问题围绕着与用户登录/注销相关的更复杂的事情展开。我基本上只需要知道电脑当前是否处于登录提示符状态。
感谢您的任何和所有帮助 - 你们真棒。
根据您的环境中,这可能是一个雷区这里讨论:
综上所述,使用来自SysInternals Suite可用PSLoggedOn工具,同时小心翼翼地筛选出可以返回的任何服务帐户。为了防止腐烂链接这里是从脚本专家文章上面的使用例子:
$Computers = @(
, "PC001"
, "PC002"
, "PC003"
, "PC004"
)
Foreach ($Computer in $Computers)
{
[object[]]$sessions = Invoke-Expression ".\PsLoggedon.exe -x -l \\$Computer" |
Where-Object {$_ -match '^\s{2,}((?<domain>\w+)\\(?<user>\S+))|(?<user>\S+)'} |
Select-Object @{
Name='Computer'
Expression={$Computer}
},
@{
Name='Domain'
Expression={$matches.Domain}
},
@{
Name='User'
Expression={$Matches.User}
}
IF ($Sessions.count -ge 1)
{
Write-Host ("{0} Users Logged into {1}" –f $Sessions.count,
$Computer) -ForegroundColor 'Red'
}
Else
{
Write-Host ("{0} can be rebooted!" -f $Computer) `
-ForegroundColor 'Green'
}
}
如上所述here,您可以使用下面的代码片段让所有登录的用户。请注意,这将包含用户,如系统,本地服务和网络服务。
Get-WmiObject Win32_LoggedOnUser -ComputerName "myMachine" |
Select Antecedent -Unique |
% {
"{0}\{1}" -f $_.Antecedent.Split('"')[1], $_.Antecedent.Split('"')[3]
}
如果你想看看,才为人们在某些领域,你可以稍微修改它是这样的:
Get-WmiObject Win32_LoggedOnUser -ComputerName "myMachine" |
Select Antecedent -Unique |
% {
$domain = $_.Antecedent.Split('"')[1]
if($domain -eq "myDomain") {
"{0}\{1}" -f $domain, $_.Antecedent.Split('"')[3]
}
}
使用WMI如果可能的话:
$info = gwmi -class win32_computerSystem -computer sist002ws -ea silentlycontinue | Select-Object username
if ($info.username.Length -gt 0)
{$Message = $info.username}
else
{ $Message = "No user is logged in locally"}
$message
这将让你所有Interactive/RemoteInteractive(终端服务会话)登录用户。您可能想要将更多LogonType添加到过滤器。没有结果意味着在没有用户登录。
http://msdn.microsoft.com/en-us/library/windows/desktop/aa394189(v=vs.85).aspx
Get-WmiObject Win32_LogonSession -ComputerName Server1 -Filter 'LogonType=2 OR LogonType=10' |
Foreach-Object { $_.GetRelated('Win32_UserAccount') } |
Select-Object Caption -Unique