I am able to grabbed log in user per the powershell cmdlet http://www.vexasoft.com/ . It works great and i am not sure how to get the computername with it also . Here is the code i have tried I am trying to line up computer name and who is logged in …I want a csv file.
The csv file contains lots of information but has computer name which is grabs that fine and outputs the current login users. The array has all the computer names if that makes sense.
Here is the command that works for me http://www.vexasoft.com/pages/get-currentuser
$secpasswd = ConvertTo-SecureString "password!" -AsPlainText -Force
$mycreds = New-Object System.Management.Automation.PSCredential ("admin", $secpasswd)
$ComputerName = import-csv C:\Users\mm73493\Desktop\test.csv | select "ComputerName"
$ComputerName | Foreach-object {
$Computer = $_
ForEach-Object { New-Object psobject -Property @{Computer=$Computer} } |
Select-Object Computer}
$ComputerName | Get-RegistryKey HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Authentication\LogonUI -ValueName LastLoggedOnUser -Credential $mycreds
#$registry = Get-RegistryKey HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Authentication\LogonUI -ValueName LastLoggedOnUser -ComputerName 550ACB-4192 -Credential $mycreds
#Write-Host ("this is "+ $values.computer)
#Write-Host ("this is" + $registry)
Does below work for you?
$secpasswd = ConvertTo-SecureString 'password!' -AsPlainText -Force
$mycreds = New-Object System.Management.Automation.PSCredential ('admin', $secpasswd)
$ComputerName = import-csv C:\Users\mm73493\Desktop\test.csv | Select-Object 'ComputerName'
$Result = foreach ($Computer in $ComputerName) {
$CurrentUser = $Computer | Get-RegistryKey HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Authentication\LogonUI -ValueName LastLoggedOnUser -Credential $mycreds
New-Object psobject -Property @{
Computer = $Computer
CurrentUser = $CurrentUser
}
}
$Result | Format-Table -AutoSize
Thank you but it did not work. It gave this error . So it does not like how it is doing the computer name. I even wrote the developer but he said that is outside the scope and the cmdlet works fine. Not sure what to fix.
Get-RegistryKey : Could not connect to computer ‘550ACB-4193’.
At line:7 char:33
I did figure out a way to get it but I do not know how to pipe all the info out to csv file …here is the command . I would love to have the one’s it can not connect to to list the computer name and unable to connect. instead of the huge error.
$secpasswd = ConvertTo-SecureString "xxxx!" -AsPlainText -Force
$mycreds = New-Object System.Management.Automation.PSCredential ("admin", $secpasswd)
$servers = (import-csv C:\Users\mm73493\Desktop\test.csv).ComputerName
foreach($server in $servers)
{
$info = Get-ComputerInfo -ComputerName $server | select ComputerName
$user = Get-RegistryKey HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Authentication\LogonUI -ValueName LastLoggedOnUser -ComputerName $server
$info | Add-Member -MemberType NoteProperty -Name LastLoggedOnUser -Value $user
$info
}
Here is the result from that query.
ComputerName LastLoggedOnUser
------------ ----------------
550ACB-3771 HSC\fg74433
550ACB-4192 HSC\ej19875
ACBWNG-3704 HSC\ts30521
550ACB-0748 HSC\rb43669
Get-ComputerInfo : Could not connect to computer '550ACB-4193'.
At line:7 char:13
+ $info = Get-ComputerInfo -ComputerName $server | select ComputerN ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ResourceUnavailable: (:) [Get-ComputerInfo], COMException
+ FullyQualifiedErrorId : ComputerNotFound,Vexasoft.Cmdlets.GetComputerInfo
Get-RegistryKey : Could not connect to computer '550ACB-4193'.
At line:8 char:13
+ $user = Get-RegistryKey HKLM:\SOFTWARE\Microsoft\Windows\CurrentV ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ResourceUnavailable: (:) [Get-RegistryKey], COMException
+ FullyQualifiedErrorId : ComputerNotFound,Vexasoft.Cmdlets.GetRegistryKey
ACBWINGS-... HSC\kn30297
550ACB-2701 HSC\ah00051
PS C:\Windows\System32\WindowsPowerShell\v1.0>
What’s in your csv file, ips? If you already have the computername why are you trying to retrieve the computername?
if(test-connection $computername -count 1){
$currentuser = Get-RegistryKey HKLM:\S…
[pscustomobject]@{
Computer = $computername
CurrentUser = $currentuser
}
}else{
[pscustomobject]@{
Computer = $computername
CurrentUser = ‘offline’
}
}
I have computer name but could not figure out how to grab it and show it without pulling it again. I would love to use the array already and just grab it but I could not send all the info to a csv file.
"ComputerName","lastlogondate","OperatingSystem","OU"
"550ACB-3771","11/15/2015 2:19:40 PM","Windows 7 Professional","OU=ACB OU=550"
"550ACB-4192","11/9/2015 8:00:38 AM","Windows 7 Professional","OU=ACB OU=550"
if you dont want it to output to a csv file remove the “| Export-Csv -Path c:\somepath\somefile.csv”
$info = @()
$secpasswd = ConvertTo-SecureString "xxxx!" -AsPlainText -Force
$mycreds = New-Object System.Management.Automation.PSCredential ("admin", $secpasswd)
$servers = (import-csv C:\Users\mm73493\Desktop\test.csv) | select ComputerName
foreach($server in $servers)
{
if(Test-Connection $server.ComputerName -BufferSize 16 -Count 1 -Quiet){
$user = Get-RegistryKey HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Authentication\LogonUI -ValueName LastLoggedOnUser -ComputerName $server.computername
$info += New-Object PSObject -Property([ordered]@{ComputerName = $server.ComputerName; LoggedinUser = $user})
}
else{
$info += New-Object PSObject -Property([ordered]@{ComputerName = $server.ComputerName; LoggedinUser = "Offline"})}
}
$info
$info | Export-Csv "c:\temp\loggedinusers.csv"
Mark - odd as it did not work as it changed the computername into object name . I know it only likes having computer name as computer ‘550ACB-4193’
Get-RegistryKey : Could not connect to computer '@{ComputerName=CFBNEU-3555}'.
At line:7 char:14
+ ... $user = Get-RegistryKey HKLM:\SOFTWARE\Microsoft\Windows\CurrentV ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ResourceUnavailable: (:) [Get-RegistryKey], COMException
+ FullyQualifiedErrorId : ComputerNotFound,Vexasoft.Cmdlets.GetRegistryKey
Get-RegistryKey : Could not connect to computer '@{ComputerName=CFBNEU-4003}'.
At line:7 char:14
+ ... $user = Get-RegistryKey HKLM:\SOFTWARE\Microsoft\Windows\CurrentV ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ResourceUnavailable: (:) [Get-RegistryKey], COMException
+ FullyQualifiedErrorId : ComputerNotFound,Vexasoft.Cmdlets.GetRegistryKey
Get-RegistryKey : Could not connect to computer '@{ComputerName=NEUCFB-0823}'.
At line:7 char:14
+ ... $user = Get-RegistryKey HKLM:\SOFTWARE\Microsoft\Windows\CurrentV ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ResourceUnavailable: (:) [Get-RegistryKey], COMException
+ FullyQualifiedErrorId : ComputerNotFound,Vexasoft.Cmdlets.GetRegistryKey
fixed again
added test connection as not to generate an error if a machine is turned off
Mark - close as it is now saying which computers are offline but it does not record computer and logged in user anymore. I am still trying to work on it but swamped at work. I noticed the computernames of the computers that it appears it is missing them or something. I just get this without piping it to csv on the screen. It skips the one’s that are online. Then I tried to export to csv and it just gave a lenth value . Just one computer too.
550ACB-4192 is Offline!
ACBWNG-3704 is Offline!
550ACB-4193 is Offline!
550ACB-4189 is Offline!
550ACB-3772 is Offline!
AIMACB-0176 is Offline!
AIMACB-3093 is Offline!
AIMACB-3115 is Offline!
AIMACB-3092 is Offline!
Type $info and see what’s stored in that variable
Try what I have up top and let me know
Oh it is in the info variable. But when I pipe it out with export csv it only shows length 11. I was trying to have the offline computers and computers logged in both in csv file. So it looks like only the computers online go to the info variable and not the offline ones
550ACB-4192 is Offline!
ACBWNG-3704 is Offline!
550ACB-4193 is Offline!
ACBWINGS-2703 is Offline!
550ACB-2701 is Offline!
550ACB-4191 is Offline!
550ACB-4196 is Offline!
550ACB-3772 is Offline!
PS C:\Users\mm73493> $info
ComputerName LoggedinUser
------------ ------------
550ACB-3771 HSC\mb32876
550ACB-0748 HSC\rb43669
550ACB-3774 HSC\eb45708
550ACB-4189 HSC\ll65153
550ACB-4550 HSC\tj73828
550ACB-4553 hsc\wb51726
550ACB-4552 HSC\rb43669
AIMACB-0229 HSC\ab77393
So it would be like this
ComputerName LoggedinUser
------------ ------------
550ACB-3771 HSC\mb32876
550ACB-4192 Offline
550ACB-0748 HSC\rb43669
550ACB-3774 HSC\eb45708
550ACB-4189 HSC\ll65153
550ACB-4550 HSC\tj73828
550ACB-4553 hsc\wb51726
550ACB-4552 HSC\rb43669
AIMACB-0229 HSC\ab77393
Then I should be able to save that to csv. Of course it is in the same order as the original computers. So it matches up to the original csv file. As I will import it in.
Mark that is stellar work. It works like a champ and I learned so much by reading your code and what you did. Thank you so much. I wish i was as good as you with powershell. Thanks for all the lessons.
LOL actually I have only been doing powershell for a little over 2 months the real big hitters are the guys like Anthony Krebs, Don Jones and Curtis Smith. I would be posting questions every day but I know there busy so I exhaust the internet first. I try and help out as much as I can on this site with my very limited knowledge. If you look above Anthony Krebs provided a solution just like mine the only difference was I added the error handling for offline servers so you would not receive that misleading error about computer does not exist. Just remember in powershell there are many ways to do things always challenge yourself and what I like to do is save my first attempts and revisit them a month later you will be surprised at how much you will have improved in such a short time.