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

Date Time formating changes in script

$
0
0

I have a bigger script that has a section like this in it were I ask each DC for the last login and convert it to a readable format and I think formatting is the issue but I don’t know how to solve it.

 

$DC01=Get-ADUser $_.SamAccountName -Serverdc01-PropertiesLastLogon|Select-Object @{n='LastLogon';e={[DateTime]::FromFileTime($_.LastLogon)}} |Select-Object-ExpandPropertyLastLogon

$DC02=Get-ADUser$_.SamAccountName -Serverdc02-PropertiesLastLogon|Select-Object @{n='LastLogon';e={[DateTime]::FromFileTime($_.LastLogon)}} |Select-Object-ExpandPropertyLastLogon

$DC03=Get-ADUser$_.SamAccountName -Serverdc03-PropertiesLastLogon|Select-Object @{n='LastLogon';e={[DateTime]::FromFileTime($_.LastLogon)}} |Select-Object-ExpandPropertyLastLogon

$DC04=Get-ADUser$_.SamAccountName -Serverdc04-PropertiesLastLogon|Select-Object @{n='LastLogon';e={[DateTime]::FromFileTime($_.LastLogon)}} |Select-Object-ExpandPropertyLastLogon

 

If I now check that data and manipulate it I get the following

PS U:\> $dc01

20 April 2015 16:20:46

 

PS U:\> $dc02

22 April 2015 08:39:03

 

PS U:\> $dc03

17 April 2015 15:32:07

 

PS U:\> $dc04

22 April 2015 08:14:11

 

I like the format 22 April 2015 08:14:11 and want to keep it like that

 

If I now add them to a variable the format changes to American date format of MM/DD/YYYY HH:MM:SS. I want to either keep is as 22 April 2015 08:14:11 or have UK format of DD/MM/YYY  HH:MM:SS.  The server I’m running this on has it’s region set to UK

 

$LastTrueLogon="$dc01","$dc02","$dc03","$dc04"

PS U:\> $LastTrueLogon

04/20/2015 16:20:46

04/22/2015 08:39:03

04/17/2015 15:32:07

04/22/2015 08:14:11

 

$LastTrueLogon | Sort-Object | Select-Object -Last 1

04/22/2015 08:39:03

 

It is because a report is being generated for the service desk and it confuses them, I removed the times in the text below to compact it down (It still doesn't line up in the post), but I want to retain it in my report

 

DisplayName            SamAccountName    EmployeeNumber    DC01                        DC02                        DC03                        DC04                        LastTrueLogon

Young, Sarah            casepsy                    32081A                    05/12/2014             13/07/2014             28/08/2014             05/12/2014                12/05/2014


Viewing all articles
Browse latest Browse all 6937

Trending Articles