Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

Help with a line of code

$
0
0

I got this snippet of code from another script that someone had written.  I have a script that emails managers of users who have not logged on recently to ask where they are and I wanted to put the number of days since they last logged in.

 

$Numberod=Get-ADUsercadcscw-PropertiesLastLogon|Select-object @{E={$($(Get-Date) - $([DateTime]::FromFileTime($_.LastLogon))).Days}}

 

write-host"Not logged in for $Numberod Days"

 

PS U:\> C:\scripts\GetDaysBetween.ps1

Not logged in for @{$($(Get-Date) - $([DateTime]::FromFileTime($_.LastLogon))).Days=128} Days

So I have two things, as you can see from the output I don’t get just 126 for $Numberod I get my whole hash array. I tried -expandproperty but that didn't work.

Second is, I don’t know what this bit is doing (well I do in some sense, it gets the number of days between the last logon and today)

@{E={$($(Get-Date) - $([DateTime]::FromFileTime($_.LastLogon))).Days}}

I know that @{ } encloses a hash table, Get-Date gets the date today and [DateTime] is a .net function, but I don’t know where that .net code is starting and finishing nor what :: does$_.LastLogon is the object I returned with the get-aduser command and piped to the next step

It looks like there is a bit of maths going on in this step

(Get-Date) - $([DateTime]::FromFileTime($_.LastLogon).

I know E is Expression but I’ve not used Expressions before so I’m not familiar with them. There are quite a few $ in there too. 

So if someone could tell what I need to do to get just 126 returned and if they could also explain what is going on I’d appreciate that.

 

Jonathan


Viewing all articles
Browse latest Browse all 6937

Trending Articles