Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Friday, May 27, 2016 8:17 PM
i am writing a script to pull ad users who have not logged in within the last 90 days from a specific ou. that part works fine however when i try to get the users names and login times it just outputs as system string length what am i doing wrong
clear
$users = Get-ADUser -Filter 'enabled -eq $true' -SearchBase "ou=Administration - AD,OU=Regional Users,DC=pmh-mb,DC=ca"
$num=0
foreach ($user in $users)
{
$un = $users.samaccountname
$name = $user.Name
$usr = get-aduser $user -properties lastlogontimestamp | select lastlogontimestamp
$usr = $usr.lastlogontimestamp
$TIME = $usr
$DT = [DateTime]::FromFileTime($TIME)
$date = "{0:yyyy/MM/dd}" -f (Get-Date).adddays(-90)
if($dt -lt $date)
{
$num=$num+1
$csv = "$num / $name / $DT"
Export-Csv -Delimiter / -Path \\da3it162d\Share\accounts.csv -Append -InputObject $csv
Write-Host $csv
}
else {}
}
All replies (20)
Friday, May 27, 2016 8:44 PM ✅Answered | 1 vote
Here is a simple example:
Get-ADUser -properties lastlogonDate -Filter 'enabled -eq $true' -SearchBase 'ou=Administration - AD,OU=Regional Users,DC=pmh-mb,DC=ca' |
where{ $_.LastLogonDate -lt [datetime]::Today.adddays(-90) }|
select Name, LastLogonDate |
Export-Csv -Path \\da3it162d\Share\accounts.csv -NoTypeInformation
\(ツ)_/
Monday, May 30, 2016 4:53 PM ✅Answered | 1 vote
Sorry I had a typo:
Get-ADUser -properties lastlogonDate -Filter 'enabled -eq $true' -SearchBase 'ou=Administration - AD,OU=Regional Users,DC=pmh-mb,DC=ca' |
where{ $_.LastLogonDate -lt [datetime]::Today.adddays(-90) }|
select Name, LastLogonDate |
Export-Csv -Path C:\Users\nyakimishen\Desktop\Share\accounts.csv -NoTypeInformation
\(ツ)_/
Friday, May 27, 2016 8:38 PM
Look in Gallery for pre-written scripts that do this.
\(ツ)_/
Friday, May 27, 2016 9:19 PM
my script works its just when i go to view the excel sheet it shows this
Length
38
39
37
39
37
40
59
41
38
44
41
41
41
41
39
39
Friday, May 27, 2016 9:20 PM
ok, Use your script since it works.
\(ツ)_/
Saturday, May 28, 2016 2:01 AM
Hi nickil333,
This is because you are passing string ($name) to Export-CSV. See below :
You have to convert the string to an object and then pass it to Export-CSV.
Hope this helps.
Saturday, May 28, 2016 2:03 AM
I have modified your script accordingly.
clear
$users = Get-ADUser -Filter 'enabled -eq $true' -SearchBase "ou=Administration - AD,OU=Regional Users,DC=pmh-mb,DC=ca"
$num=0
foreach ($user in $users)
{
$un = $users.samaccountname
$name = $user.Name | Select-Object @{Name='Name';Expression={$_}}
$usr = get-aduser $user -properties lastlogontimestamp | select lastlogontimestamp
$usr = $usr.lastlogontimestamp
$TIME = $usr
$DT = [DateTime]::FromFileTime($TIME)
$date = "{0:yyyy/MM/dd}" -f (Get-Date).adddays(-90)
if($dt -lt $date)
{
$num=$num+1
$csv = "$num / $name / $DT"
Export-Csv -Delimiter / -Path \\da3it162d\Share\accounts.csv -Append -InputObject $csv
Write-Host $csv
}
else {}
}
I have not tested it though.
Monday, May 30, 2016 3:17 PM
so i tried that and it stil isnt exporting properly now i get this show up in my csv
#TYPE System.Object[]
Count Length LongLength Rank SyncRoot IsReadOnly IsFixedSize IsSynchronized
2 2 2 1 System.Object[] FALSE TRUE FALSE
2 2 2 1 System.Object[] FALSE TRUE FALSE
2 2 2 1 System.Object[] FALSE TRUE FALSE
2 2 2 1 System.Object[] FALSE TRUE FALSE
2 2 2 1 System.Object[] FALSE TRUE FALSE
2 2 2 1 System.Object[] FALSE TRUE FALSE
2 2 2 1 System.Object[] FALSE TRUE FALSE
Monday, May 30, 2016 3:39 PM
Is this the correct CSV you are looking at ? The one you pasted does not have any of the columns you are trying to export.
Monday, May 30, 2016 3:52 PM
Also, you should build the object and export it one time instead of appending each time within loop.
Move you export-csv outside foreach loop.
Monday, May 30, 2016 3:52 PM
ya this is what happens when i run my script
clear
$users = Get-ADUser -Filter 'enabled -eq $true' -SearchBase "ou=Administration - AD,OU=Regional Users,DC=pmh-mb,DC=ca"
$num=0
foreach ($user in $users)
{
$un = $users.samaccountname
$name = $user.Name
$usr = get-aduser $user -properties lastlogontimestamp | select lastlogontimestamp
$dn = $user.DistinguishedName
$usr = $usr.lastlogontimestamp
$TIME = $usr
$DT = [DateTime]::FromFileTime($TIME)
$date = "{0:yyyy/MM/dd}" -f (Get-Date).adddays(-365)
if($dt -lt $date)
{
$num=$num+1
$output="$dn / $DT"
out-file -InputObject $output C:\Users\nyakimishen\Desktop\Share\accounts.txt -append
Write-host $output
}
else {}
}
$csv= "C:\Users\nyakimishen\Desktop\Share\accounts.txt"
export-csv -InputObject "$csv" -path C:\Users\nyakimishen\Desktop\Share\accounts.csv -Delimiter "/"
updated script not much changed just printing dn instead of actual namebut ya when this script runs i get this as an output into my csv file
#TYPE System.Object[]
Count Length LongLength Rank SyncRoot IsReadOnly IsFixedSize IsSynchronized
2 2 2 1 System.Object[] FALSE TRUE FALSE
2 2 2 1 System.Object[] FALSE TRUE FALSE
2 2 2 1 System.Object[] FALSE TRUE FALSE
2 2 2 1 System.Object[] FALSE TRUE FALSE
2 2 2 1 System.Object[] FALSE TRUE FALSE
2 2 2 1 System.Object[] FALSE TRUE FALSE
2 2 2 1 System.Object[] FALSE TRUE FALSE
Monday, May 30, 2016 4:07 PM
You have probably messed up the whole script.
This is what i have modified . This should give you a column "Name" in csv.
$name = $user.Name | Select-Object @{Name='Name';Expression={$_}}
Monday, May 30, 2016 4:18 PM
clear
$users = Get-ADUser -Filter 'enabled -eq $true' -SearchBase "ou=Administration - AD,OU=Regional Users,DC=pmh-mb,DC=ca"
$num=0
foreach ($user in $users)
{
$un = $users.samaccountname
$name = $user.Name | Select-Object @{Name='Name';Expression={$_}}
$usr = get-aduser $user -properties lastlogontimestamp | select lastlogontimestamp
$dn = $user.DistinguishedName
$usr = $usr.lastlogontimestamp
$TIME = $usr
$DT = [DateTime]::FromFileTime($TIME)
$date = "{0:yyyy/MM/dd}" -f (Get-Date).adddays(-365)
if($dt -lt $date)
{
$num=$num+1
$output=$name , $DT
export-csv -InputObject $output C:\Users\nyakimishen\Desktop\Share\accounts.csv -append -delimiter ","
Write-host $output
}
else {}
}
gives me this
#TYPE System.Object[]
Count Length LongLength Rank SyncRoot IsReadOnly IsFixedSize IsSynchronized
2 2 2 1 System.Object[] FALSE TRUE FALSE
2 2 2 1 System.Object[] FALSE TRUE FALSE
2 2 2 1 System.Object[] FALSE TRUE FALSE
2 2 2 1 System.Object[] FALSE TRUE FALSE
2 2 2 1 System.Object[] FALSE TRUE FALSE
2 2 2 1 System.Object[] FALSE TRUE FALSE
2 2 2 1 System.Object[] FALSE TRUE FALSE
Monday, May 30, 2016 4:30 PM
Ok.Run this
clear
#$num=0
$users = Get-ADUser -Filter 'enabled -eq $true' -SearchBase "ou=Administration - AD,OU=Regional Users,DC=pmh-mb,DC=ca"|
ForEach-Object
{
# $un = $users.samaccountname
$name = $_.Name
$usr = get-aduser $_ -properties lastlogontimestamp | select lastlogontimestamp
$dn = $_.DistinguishedName
# $usr = $usr.lastlogontimestamp
# $TIME = $usr
$DT = [DateTime]::FromFileTime($usr)
$date = "{0:yyyy/MM/dd}" -f (Get-Date).adddays(-365)
if($dt -lt $date)
{
[PSCUSTOMOBJECT]@{
name = $Name
DName = $dn
DT=$dt
}
}
else {}
}|
export-csv -path C:\Users\nyakimishen\Desktop\Share\accounts.csv -NoTypeInformation
Monday, May 30, 2016 4:40 PM
Attributes File IsFilter IsConfiguration Module StartPosition DebuggerHidden Id Ast
System.Collections.Generic.List`1[System.Attribute] FALSE FALSE System.Management.Automation.PSToken FALSE 86d2624f-4ad5-4143-8076-17aa90708c8
Monday, May 30, 2016 4:44 PM
I gave the solution at the beginning. It works. Your script doesn't work because it is logically and technically incorrect.
See my original solution.
\(ツ)_/
Monday, May 30, 2016 4:46 PM
You cannot call a method on a null-valued expression.
At line:2 char:9
- where{ $_.LastLogonDate -lt [datetime].Today.adddays(-90) }|
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
always think before you click
Monday, May 30, 2016 4:48 PM
You cannot call a method on a null-valued expression.
At line:2 char:9
- where{ $_.LastLogonDate -lt [datetime].Today.adddays(-90) }|
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNullalways think before you click
Add "-EA 0" and the errors will go away. This is because accounts that do not logon have a null date. You could also use a ForEach-Object and use a try/catch in the loop.
\(ツ)_/
Monday, May 30, 2016 4:49 PM
I do not have AD module to test.but this output is not expected. Use Jrv's solution :
Get-ADUser -properties lastlogonDate -Filter 'enabled -eq $true' -SearchBase 'ou=Administration - AD,OU=Regional Users,DC=pmh-mb,DC=ca' |
where{ $_.LastLogonDate -lt [datetime].Today.adddays(-90) }|
select Name, LastLogonDate |
Export-Csv -Path C:\Users\nyakimishen\Desktop\Share\accounts.csv -NoTypeInformation
Monday, May 30, 2016 5:00 PM
thanks works perfectly
always think before you click