Powershell用户及其所在主机的列表
有人有什么建议吗?我需要从Active Directory生成一个用户和他们登录的计算机的列表。我希望得到如下内容: 用户名主机名 user.lastname ComputerA1 到目前为止,我得到了: 进入PSSession导入模块ActiveDirectory获取ADComputer -过滤器*-属性名称Get ADuser-过滤器*-属性*|导出csv'\\\AD\u UserLists.csv' 这是可行的。我可以从广告中生成一个计算机列表,也可以生成一个成人列表(尽管包含所有用户信息)。不幸的是,我无法将数据生成为单个CSV 建议/建议 Thanx, 大卫 我需要从Active Directory生成一个用户及其登录计算机的列表 此信息不存储在Active Directory中。您可以通过Active Directory审核检索此信息。否则,您将需要轮询每个单独的工作站 我需要从Active Directory生成一个用户及其登录计算机的列表Powershell用户及其所在主机的列表,powershell,Powershell,有人有什么建议吗?我需要从Active Directory生成一个用户和他们登录的计算机的列表。我希望得到如下内容: 用户名主机名 user.lastname ComputerA1 到目前为止,我得到了: 进入PSSession导入模块ActiveDirectory获取ADComputer -过滤器*-属性名称Get ADuser-过滤器*-属性*|导出csv'\\\AD\u UserLists.csv' 这是可行的。我可以从广告中生成一个计算机列表,也可以生成一个成人列表(尽管包含所有用户信息
此信息不存储在Active Directory中。您可以通过Active Directory审核检索此信息。否则,您需要轮询每个工作站。您可以使用wmi功能
Get-WmiObject -Class Win32_ComputerSystem -ComputerName "computersname" | Select-Object Name,Username
您可以使用wmi函数
Get-WmiObject -Class Win32_ComputerSystem -ComputerName "computersname" | Select-Object Name,Username
这里有一个方法可以得到你想要的。当计算机联机时,您必须针对AD计算机对象运行此操作,并获取无法访问的计算机的名称。像这样的
#grab the DN of the OU where your computer objects are located...
$OU = ("OU=Computers,DC=domain,DC=com")
#put your filtered results in $computers (I filtered for Enabled objects)...
$computers = @()
ForEach ($O in $OU) {
$computers += Get-ADComputer -SearchBase $O -filter 'Enabled -eq "True"' -Properties CN,distinguishedname,lastLogonTimeStamp | Select-Object CN,distinguishedname,lastLogonTimeStamp
}
#instantiate some arrays to catch your results
#collected user info
$userInfo = @()
#computers you cannot ping
$offline = @()
#computers you can ping but cannot establish WinRM connection
$winRmIssue = @()
#iterate over $computers list to get user info on each...
ForEach ($computer in $computers) {
#filter out System account SIDs
$WQLFilter = "NOT SID = 'S-1-5-18' AND NOT SID = 'S-1-5-19' AND NOT SID = 'S-1-5-20'"
$WQLFilter = $WQLFilter + " AND NOT SID = `'$FilterSID`'"
#set number of login events to grab
$newest = 20
#attempt to ping computer once by name. return 'true' is success...
if (Test-Connection -ComputerName $computer.CN -Count 1 -ErrorAction Stop -Quiet) {
#if ping is true, try to get some info...
Try {
#currently logged in user...
$user = Get-WmiObject -Class Win32_ComputerSystem -ComputerName $computer.CN | select -ExpandProperty username
#the most commonly logged in user, based on the past 20 log-ins...
$UserProperty = @{n="User";e={((New-Object System.Security.Principal.SecurityIdentifier $_.ReplacementStrings[1]).Translate([System.Security.Principal.NTAccount])).ToString()}}
$logs = Get-EventLog System -Source Microsoft-Windows-Winlogon -ComputerName $computer.CN -newest $newest | select $UserProperty
$freqent = $logs | Group User | Sort-Object Count | Select -First 1 | Select-Object -ExpandProperty Name
}
#catch any connection issues...
Catch {
$cantInvoke = [pscustomobject][ordered]@{
'Computer' = $computer.CN
'Message' = "Could not Invoke-Command. Probably a WinRM issue."
}
$winRMIssue += $cantInvoke
}
#custom psobject of gathered user info...
$userInfoObj = New-Object psobject -Property ([ordered]@{
'Computer' = $computer.CN
'LoggedInUser' = $user
'mostCommonUser' = $frequent
})
$userInfo += $userInfoObj
}
#if you could not ping the computer, gather that info here in a custom object...
else {
$noPing = [pscustomobject][ordered]@{
'Computer' = $computer.CN
'DN' = $computer.distinguishedname
'lastLogonDate' = [datetime]::FromFileTime($computer.lastLogonTimeStamp).toShortDateString()
}
$offline += $noPing
}
#then kick out the results to csv
$userInfo | Sort-Object Computer | export-csv -Path c:\path\file.csv -NoTypeInformation
$offline | Sort-Object lastLogonDate | export-csv -Path c:\path.file2csv -NoTypeInformation
$winRmIssue | Sort-Object Computer | export-csv -Path c:\path\file3.csv -NoTypeInformation
这里有一个方法可以得到你想要的。当计算机联机时,您必须针对AD计算机对象运行此操作,并获取无法访问的计算机的名称。像这样的
#grab the DN of the OU where your computer objects are located...
$OU = ("OU=Computers,DC=domain,DC=com")
#put your filtered results in $computers (I filtered for Enabled objects)...
$computers = @()
ForEach ($O in $OU) {
$computers += Get-ADComputer -SearchBase $O -filter 'Enabled -eq "True"' -Properties CN,distinguishedname,lastLogonTimeStamp | Select-Object CN,distinguishedname,lastLogonTimeStamp
}
#instantiate some arrays to catch your results
#collected user info
$userInfo = @()
#computers you cannot ping
$offline = @()
#computers you can ping but cannot establish WinRM connection
$winRmIssue = @()
#iterate over $computers list to get user info on each...
ForEach ($computer in $computers) {
#filter out System account SIDs
$WQLFilter = "NOT SID = 'S-1-5-18' AND NOT SID = 'S-1-5-19' AND NOT SID = 'S-1-5-20'"
$WQLFilter = $WQLFilter + " AND NOT SID = `'$FilterSID`'"
#set number of login events to grab
$newest = 20
#attempt to ping computer once by name. return 'true' is success...
if (Test-Connection -ComputerName $computer.CN -Count 1 -ErrorAction Stop -Quiet) {
#if ping is true, try to get some info...
Try {
#currently logged in user...
$user = Get-WmiObject -Class Win32_ComputerSystem -ComputerName $computer.CN | select -ExpandProperty username
#the most commonly logged in user, based on the past 20 log-ins...
$UserProperty = @{n="User";e={((New-Object System.Security.Principal.SecurityIdentifier $_.ReplacementStrings[1]).Translate([System.Security.Principal.NTAccount])).ToString()}}
$logs = Get-EventLog System -Source Microsoft-Windows-Winlogon -ComputerName $computer.CN -newest $newest | select $UserProperty
$freqent = $logs | Group User | Sort-Object Count | Select -First 1 | Select-Object -ExpandProperty Name
}
#catch any connection issues...
Catch {
$cantInvoke = [pscustomobject][ordered]@{
'Computer' = $computer.CN
'Message' = "Could not Invoke-Command. Probably a WinRM issue."
}
$winRMIssue += $cantInvoke
}
#custom psobject of gathered user info...
$userInfoObj = New-Object psobject -Property ([ordered]@{
'Computer' = $computer.CN
'LoggedInUser' = $user
'mostCommonUser' = $frequent
})
$userInfo += $userInfoObj
}
#if you could not ping the computer, gather that info here in a custom object...
else {
$noPing = [pscustomobject][ordered]@{
'Computer' = $computer.CN
'DN' = $computer.distinguishedname
'lastLogonDate' = [datetime]::FromFileTime($computer.lastLogonTimeStamp).toShortDateString()
}
$offline += $noPing
}
#then kick out the results to csv
$userInfo | Sort-Object Computer | export-csv -Path c:\path\file.csv -NoTypeInformation
$offline | Sort-Object lastLogonDate | export-csv -Path c:\path.file2csv -NoTypeInformation
$winRmIssue | Sort-Object Computer | export-csv -Path c:\path\file3.csv -NoTypeInformation
当每个工作站联机时,您必须对其运行脚本。从AD获取您的OU,使用-searchbase在每个对象上运行foreach循环,然后获取最常见的登录用户或当前登录的用户。有事件日志和WMI方法可以获取这两种方法之一。如果你没有答案,我明天会给你答案。现在,这就是您需要做的。您可以在foreach循环中使用Invoke命令,并将结果抛出到空数组中。设置try/catch块,如果可以ping机器(测试连接-computername$obj.name-Count 1-Quiet)…如果为true,则调用命令并运行查询。如果不是,则将不同的结果传递给数组。对于测试连接的结果,-Quiet开关将返回true或false。谢谢您的建议。您看到我的答案了吗?它对你有用吗?我看到了,但没能让它起作用——主要是因为我自己的失败和缺乏powershell知识。不过我确实找到了一份工作。需要使用两个不同的系统来提取数据并将其合并到单个Excel电子表格中。大家都出去了,但最后,管理层得到了他们需要的信息,我得到了更多的工作。谢谢你的帮助和帮助。稍后当类似的问题出现时,我可能会回到这篇文章。当每个工作站联机时,您必须对它们运行脚本。从AD获取您的OU,使用-searchbase在每个对象上运行foreach循环,然后获取最常见的登录用户或当前登录的用户。有事件日志和WMI方法可以获取这两种方法之一。如果你没有答案,我明天会给你答案。现在,这就是您需要做的。您可以在foreach循环中使用Invoke命令,并将结果抛出到空数组中。设置try/catch块,如果可以ping机器(测试连接-computername$obj.name-Count 1-Quiet)…如果为true,则调用命令并运行查询。如果不是,则将不同的结果传递给数组。对于测试连接的结果,-Quiet开关将返回true或false。谢谢您的建议。您看到我的答案了吗?它对你有用吗?我看到了,但没能让它起作用——主要是因为我自己的失败和缺乏powershell知识。不过我确实找到了一份工作。需要使用两个不同的系统来提取数据并将其合并到单个Excel电子表格中。大家都出去了,但最后,管理层得到了他们需要的信息,我得到了更多的工作。谢谢你的帮助和帮助。当类似的问题出现时,我可能会稍后再回到这篇文章。该死!但愿我早点看到。这正是我需要的。现在要记住如何创建一个变量以从主机工作站列表中读取。:)大卫,如果你仔细看我的回答,你接受的命令就在里面。它将为您获取当前登录的用户,但仅此而已。你可以在一个工作站上投票,却得不到任何信息,而我的答案告诉你谁最常登录计算机。它还回答了您关于变量和计算机列表的问题。在我的例子中,它们来自于广告,我以为你是从那里得到它们的!但愿我早点看到。这正是我需要的。现在要记住如何创建一个变量以从主机工作站列表中读取。:)大卫,如果你仔细看我的回答,你接受的命令就在里面。它将为您获取当前登录的用户,但仅此而已。你可以在一个工作站上投票,却得不到任何信息,而我的答案告诉你谁最常登录计算机。它还回答了您关于变量和计算机列表的问题。在我的例子中,它们来自广告,我以为你是从广告中得到它们的