\nIf computer is in use, it should retrieve “name”, “city” and “department” from Get-ADUser of the logged in user.<\/p>\n<\/li>\n<\/ol>\n
So far, I only manage to achieve 1 & 2 but struggling with to achieve number 3. I would appreciate any guidance here. Thanks in advance.<\/p>\n
$Computer = ‘PC2335’\nIf (Test-Connection -BufferSize 32 -Count 2 -ComputerName $Computer -Quiet) {\ntry{\n$User = $null\n$User=get-wmiobject win32_computersystem -comp $computer | Select -ExpandProperty UserName -ErrorAction Stop\n$Domain,$UserID=$User -split \"\\\\\" \nGet-ADUser -Identity $UserID -Properties Name,Title,LastLogon | \nSelect-Object Name,City,Department |\nFormat-List\n}\ncatch {\"$Computer is online. No user is logged in\"; return}\n \n} Else {\n\nWrite-Host \"$Computer is offline\"\n\n}\n<\/code><\/pre>","upvoteCount":3,"answerCount":8,"datePublished":"2018-01-14T13:35:38.000Z","author":{"@type":"Person","name":"jonmcclusky","url":"https://community.spiceworks.com/u/jonmcclusky"},"acceptedAnswer":{"@type":"Answer","text":"\n\n
<\/div>\n
jonmcclusky:<\/div>\n
\nThanks JitenSh. Works perfectly. Is there anyway to incorporate get-childitem of c\\users\\USER_ID in this script so it could also retrieve “lastwritetime” of the user logged in by looking at $userid?<\/p>\n<\/blockquote>\n<\/aside>\n
you should have opened a new topic actually, mark answer as best and close this<\/p>\n
$users = import-csv c:\\users.csv |select -exp samaccountname\n $computers=\"\"\nForEach ($COMPUTER in $computers)\n {if(!(Test-Connection -Cn $computer -BufferSize 16 -Count 1 -ea 0 -quiet))\n {write-host \"Cannot reach $computer its offline\" -f red}\n \nelse {\n if ((Test-Path -Path \\\\$computer\\c$)){\n TRY{\n$data=foreach ($user in $users){\nGet-ChildItem \"\\\\$computer\\c$\\users\\$($user)\\ntuser.dat\" -force |select LastWriteTime,@{n=\"User\";e={$user}}\n }\n Write-Host \"Sucess on $computer\" -BackgroundColor Green\n }\n Catch {$error[0].exception.message}\n }\n }\n }\n \n $data |export-csv c:\\data.csv -NoTypeInformation\n<\/code><\/pre>","upvoteCount":2,"datePublished":"2018-01-16T15:08:44.000Z","url":"https://community.spiceworks.com/t/logged-on-to-computer/628570/6","author":{"@type":"Person","name":"jitensh","url":"https://community.spiceworks.com/u/jitensh"}},"suggestedAnswer":[{"@type":"Answer","text":"Hello all,<\/p>\n
A little help here pls. I have my below script which returns 80% info that I want but can’t seem to get it to work exactly how I want. I would like it to achieve all of the 3.<\/p>\n
\n\nThe script checks if the computer is online or not.<\/p>\n<\/li>\n
\nChecks if the computer is online AND if a user is logged on or not.<\/p>\n<\/li>\n
\nIf computer is in use, it should retrieve “name”, “city” and “department” from Get-ADUser of the logged in user.<\/p>\n<\/li>\n<\/ol>\n
So far, I only manage to achieve 1 & 2 but struggling with to achieve number 3. I would appreciate any guidance here. Thanks in advance.<\/p>\n
$Computer = ‘PC2335’\nIf (Test-Connection -BufferSize 32 -Count 2 -ComputerName $Computer -Quiet) {\ntry{\n$User = $null\n$User=get-wmiobject win32_computersystem -comp $computer | Select -ExpandProperty UserName -ErrorAction Stop\n$Domain,$UserID=$User -split \"\\\\\" \nGet-ADUser -Identity $UserID -Properties Name,Title,LastLogon | \nSelect-Object Name,City,Department |\nFormat-List\n}\ncatch {\"$Computer is online. No user is logged in\"; return}\n \n} Else {\n\nWrite-Host \"$Computer is offline\"\n\n}\n<\/code><\/pre>","upvoteCount":3,"datePublished":"2018-01-14T13:35:38.000Z","url":"https://community.spiceworks.com/t/logged-on-to-computer/628570/1","author":{"@type":"Person","name":"jonmcclusky","url":"https://community.spiceworks.com/u/jonmcclusky"}},{"@type":"Answer","text":"check this out<\/p>\n
$computers=\"computername\"\n\n ForEach ($COMPUTER in $computers)\n {if(!(Test-Connection -Cn $computer -BufferSize 16 -Count 1 -ea 0 -quiet))\n {write-host \"cannot reach $computer\" -f red}\n Else {\n TRY{\n $ErrorActionPreference = \"Stop\"\n\n $userid=(get-wmiobject Win32_ComputerSystem -ComputerName $computer).UserName.Split('\\')[1]\n \n }\n\n Catch\n {\n Write-Host \"$($computer) \" -BackgroundColor red -NoNewline\n Write-Warning $Error[0] \n }\n }\n }\n \n \n If ( ! (Get-module ActiveDirectory )) {\n Import-Module ActiveDirectory -verbose\n Cls\n }\n Foreach($user in $userid)\n Get-ADUser -Identity $User -Properties Department,city,Title,LastLogondate | \nSelect-Object Name,City,Department,LastLogondate |\nFormat-List\n<\/code><\/pre>","upvoteCount":3,"datePublished":"2018-01-15T05:38:56.000Z","url":"https://community.spiceworks.com/t/logged-on-to-computer/628570/2","author":{"@type":"Person","name":"jitensh","url":"https://community.spiceworks.com/u/jitensh"}},{"@type":"Answer","text":"Try something like this:<\/p>\n
$names = Get-WmiObject win32_useraccount -Property name,sid | select name,sid\n\n$hello = Get-CimInstance win32_userprofile -Property special,loaded,sid | select special,loaded,sid | ?{($_.special -eq $false) -and ($_.loaded -eq $true)}\n\nforeach ($name in $names){\n\nif($name.sid -match $hello.sid){\n\nwrite-host $name.name\n\n}\n\n}\n<\/code><\/pre>","upvoteCount":2,"datePublished":"2018-01-15T05:49:09.000Z","url":"https://community.spiceworks.com/t/logged-on-to-computer/628570/3","author":{"@type":"Person","name":"icanfixit","url":"https://community.spiceworks.com/u/icanfixit"}},{"@type":"Answer","text":"\n\n
<\/div>\n
JitenSh:<\/div>\n
\ncheck this out<\/p>\n
$computers=\"computername\"\n\n ForEach ($COMPUTER in $computers)\n {if(!(Test-Connection -Cn $computer -BufferSize 16 -Count 1 -ea 0 -quiet))\n {write-host \"cannot reach $computer\" -f red}\n Else {\n TRY{\n $ErrorActionPreference = \"Stop\"\n\n $userid=(get-wmiobject Win32_ComputerSystem -ComputerName $computer).UserName.Split('\\')[1]\n \n }\n\n Catch\n {\n Write-Host \"$($computer) \" -BackgroundColor red -NoNewline\n Write-Warning $Error[0] \n }\n }\n }\n \n \n If ( ! (Get-module ActiveDirectory )) {\n Import-Module ActiveDirectory -verbose\n Cls\n }\n Foreach($user in $userid)\n Get-ADUser -Identity $User -Properties Department,city,Title,LastLogondate | \nSelect-Object Name,City,Department,LastLogondate |\nFormat-List\n<\/code><\/pre>\n<\/blockquote>\n<\/aside>\nMissed Curly brackets here, If Multiple users<\/p>\n
Foreach($user in $userid){\n Get-ADUser -Identity $User -Properties Department,city,Title,LastLogondate | \nSelect-Object Name,City,Department,LastLogondate |\nFormat-List\n}\n<\/code><\/pre>","upvoteCount":1,"datePublished":"2018-01-15T07:51:34.000Z","url":"https://community.spiceworks.com/t/logged-on-to-computer/628570/4","author":{"@type":"Person","name":"jitensh","url":"https://community.spiceworks.com/u/jitensh"}},{"@type":"Answer","text":"Thanks JitenSh. Works perfectly. Is there anyway to incorporate get-childitem of c\\users\\USER_ID in this script so it could also retrieve “lastwritetime” of the user logged in by looking at $userid?<\/p>","upvoteCount":0,"datePublished":"2018-01-16T13:41:51.000Z","url":"https://community.spiceworks.com/t/logged-on-to-computer/628570/5","author":{"@type":"Person","name":"jonmcclusky","url":"https://community.spiceworks.com/u/jonmcclusky"}},{"@type":"Answer","text":"\n\n
<\/div>\n
JitenSh:<\/div>\n
\n<\/blockquote>\n $users = import-csv c:\\users.csv |select -exp samaccountname\n $computers=\"\"\nForEach ($COMPUTER in $computers)\n {if(!(Test-Connection -Cn $computer -BufferSize 16 -Count 1 -ea 0 -quiet))\n {write-host \"Cannot reach $computer its offline\" -f red}\n \nelse {\n if ((Test-Path -Path \\\\$computer\\c$)){\n TRY{\n$data=foreach ($user in $users){\nGet-ChildItem \"\\\\$computer\\c$\\users\\$($user)\\ntuser.dat\" -force |select LastWriteTime,@{n=\"User\";e={$user}}\n }\n Write-Host \"Sucess on $computer\" -BackgroundColor Green\n }\n Catch {$error[0].exception.message}\n }\n }\n }\n \n $data |export-csv c:\\data.csv -NoTypeInformation\n<\/code><\/pre>\n<\/blockquote>\n<\/aside>\nThis is working as expected I wrote it without testing make sure csv has header as samaccount name or any name but should match this samccount or any header<\/p>\n
|select -exp samaccountname<\/p>\n
and $computers=list of computers.either text or csv<\/p>\n
I wrote One more for computers and all its users<\/p>\n
ForEach ($COMPUTER in ($computers))\n {if(!(Test-Connection -Cn $computer -BufferSize 16 -Count 1 -ea 0 -quiet))\n {write-host \"Cannot reach $computer its offline\" -f red}\n \nelse {\n if ((Test-Path -Path \\\\$computer\\c$)){\n TRY{\n $users = Get-ChildItem \\\\$computer\\c$\\users |?{$_.name -notlike '*Public*' -and $_.name -notlike '*Default*' }\n$data=foreach ($user in $users){\nGCI \"$($user.fullname)\\ntuser.dat\" -force |select LastWriteTime,@{n=\"User\";e={$user}}\n }\n Write-Host \"Sucess on $computer\" -BackgroundColor Green\n }\n Catch {$error[0].exception.message}\n }\n }\n }\n \n $data |export-csv c:\\data.csv -NoTypeInformation\n<\/code><\/pre>","upvoteCount":0,"datePublished":"2018-01-16T16:16:07.000Z","url":"https://community.spiceworks.com/t/logged-on-to-computer/628570/7","author":{"@type":"Person","name":"jitensh","url":"https://community.spiceworks.com/u/jitensh"}},{"@type":"Answer","text":"I’ve modified your earlier script and it’s working. I’ll take a look at the last one too. Thanks.<\/p>","upvoteCount":1,"datePublished":"2018-01-16T16:19:50.000Z","url":"https://community.spiceworks.com/t/logged-on-to-computer/628570/8","author":{"@type":"Person","name":"jonmcclusky","url":"https://community.spiceworks.com/u/jonmcclusky"}}]}}
Hello all,
A little help here pls. I have my below script which returns 80% info that I want but can’t seem to get it to work exactly how I want. I would like it to achieve all of the 3.
The script checks if the computer is online or not.
Checks if the computer is online AND if a user is logged on or not.
If computer is in use, it should retrieve “name”, “city” and “department” from Get-ADUser of the logged in user.
So far, I only manage to achieve 1 & 2 but struggling with to achieve number 3. I would appreciate any guidance here. Thanks in advance.
$Computer = ‘PC2335’
If (Test-Connection -BufferSize 32 -Count 2 -ComputerName $Computer -Quiet) {
try{
$User = $null
$User=get-wmiobject win32_computersystem -comp $computer | Select -ExpandProperty UserName -ErrorAction Stop
$Domain,$UserID=$User -split "\\"
Get-ADUser -Identity $UserID -Properties Name,Title,LastLogon |
Select-Object Name,City,Department |
Format-List
}
catch {"$Computer is online. No user is logged in"; return}
} Else {
Write-Host "$Computer is offline"
}
3 Spice ups
jitensh
(JitenSh)
January 15, 2018, 5:38am
2
check this out
$computers="computername"
ForEach ($COMPUTER in $computers)
{if(!(Test-Connection -Cn $computer -BufferSize 16 -Count 1 -ea 0 -quiet))
{write-host "cannot reach $computer" -f red}
Else {
TRY{
$ErrorActionPreference = "Stop"
$userid=(get-wmiobject Win32_ComputerSystem -ComputerName $computer).UserName.Split('\')[1]
}
Catch
{
Write-Host "$($computer) " -BackgroundColor red -NoNewline
Write-Warning $Error[0]
}
}
}
If ( ! (Get-module ActiveDirectory )) {
Import-Module ActiveDirectory -verbose
Cls
}
Foreach($user in $userid)
Get-ADUser -Identity $User -Properties Department,city,Title,LastLogondate |
Select-Object Name,City,Department,LastLogondate |
Format-List
3 Spice ups
icanfixit
(Chris Walten)
January 15, 2018, 5:49am
3
Try something like this:
$names = Get-WmiObject win32_useraccount -Property name,sid | select name,sid
$hello = Get-CimInstance win32_userprofile -Property special,loaded,sid | select special,loaded,sid | ?{($_.special -eq $false) -and ($_.loaded -eq $true)}
foreach ($name in $names){
if($name.sid -match $hello.sid){
write-host $name.name
}
}
2 Spice ups
jitensh
(JitenSh)
January 15, 2018, 7:51am
4
JitenSh:
check this out
$computers="computername"
ForEach ($COMPUTER in $computers)
{if(!(Test-Connection -Cn $computer -BufferSize 16 -Count 1 -ea 0 -quiet))
{write-host "cannot reach $computer" -f red}
Else {
TRY{
$ErrorActionPreference = "Stop"
$userid=(get-wmiobject Win32_ComputerSystem -ComputerName $computer).UserName.Split('\')[1]
}
Catch
{
Write-Host "$($computer) " -BackgroundColor red -NoNewline
Write-Warning $Error[0]
}
}
}
If ( ! (Get-module ActiveDirectory )) {
Import-Module ActiveDirectory -verbose
Cls
}
Foreach($user in $userid)
Get-ADUser -Identity $User -Properties Department,city,Title,LastLogondate |
Select-Object Name,City,Department,LastLogondate |
Format-List
Missed Curly brackets here, If Multiple users
Foreach($user in $userid){
Get-ADUser -Identity $User -Properties Department,city,Title,LastLogondate |
Select-Object Name,City,Department,LastLogondate |
Format-List
}
1 Spice up
Thanks JitenSh. Works perfectly. Is there anyway to incorporate get-childitem of c\users\USER_ID in this script so it could also retrieve “lastwritetime” of the user logged in by looking at $userid?
jitensh
(JitenSh)
January 16, 2018, 3:08pm
6
you should have opened a new topic actually, mark answer as best and close this
$users = import-csv c:\users.csv |select -exp samaccountname
$computers=""
ForEach ($COMPUTER in $computers)
{if(!(Test-Connection -Cn $computer -BufferSize 16 -Count 1 -ea 0 -quiet))
{write-host "Cannot reach $computer its offline" -f red}
else {
if ((Test-Path -Path \\$computer\c$)){
TRY{
$data=foreach ($user in $users){
Get-ChildItem "\\$computer\c$\users\$($user)\ntuser.dat" -force |select LastWriteTime,@{n="User";e={$user}}
}
Write-Host "Sucess on $computer" -BackgroundColor Green
}
Catch {$error[0].exception.message}
}
}
}
$data |export-csv c:\data.csv -NoTypeInformation
2 Spice ups
jitensh
(JitenSh)
January 16, 2018, 4:16pm
7
JitenSh:
$users = import-csv c:\users.csv |select -exp samaccountname
$computers=""
ForEach ($COMPUTER in $computers)
{if(!(Test-Connection -Cn $computer -BufferSize 16 -Count 1 -ea 0 -quiet))
{write-host "Cannot reach $computer its offline" -f red}
else {
if ((Test-Path -Path \\$computer\c$)){
TRY{
$data=foreach ($user in $users){
Get-ChildItem "\\$computer\c$\users\$($user)\ntuser.dat" -force |select LastWriteTime,@{n="User";e={$user}}
}
Write-Host "Sucess on $computer" -BackgroundColor Green
}
Catch {$error[0].exception.message}
}
}
}
$data |export-csv c:\data.csv -NoTypeInformation
This is working as expected I wrote it without testing make sure csv has header as samaccount name or any name but should match this samccount or any header
|select -exp samaccountname
and $computers=list of computers.either text or csv
I wrote One more for computers and all its users
ForEach ($COMPUTER in ($computers))
{if(!(Test-Connection -Cn $computer -BufferSize 16 -Count 1 -ea 0 -quiet))
{write-host "Cannot reach $computer its offline" -f red}
else {
if ((Test-Path -Path \\$computer\c$)){
TRY{
$users = Get-ChildItem \\$computer\c$\users |?{$_.name -notlike '*Public*' -and $_.name -notlike '*Default*' }
$data=foreach ($user in $users){
GCI "$($user.fullname)\ntuser.dat" -force |select LastWriteTime,@{n="User";e={$user}}
}
Write-Host "Sucess on $computer" -BackgroundColor Green
}
Catch {$error[0].exception.message}
}
}
}
$data |export-csv c:\data.csv -NoTypeInformation
I’ve modified your earlier script and it’s working. I’ll take a look at the last one too. Thanks.
1 Spice up