Quantcast
Channel: PowerShell General
Viewing all articles
Browse latest Browse all 10624

Formatting output

$
0
0

When I run this part of my script

#Gets the LastLogon from each DC, turns into a readable output, and the replicated lastLogonDate for the user that is currently $Exists in a ForEach loop

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

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

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

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

$lastLogonDate=Get-ADUSer$Exists-Properties*|Select-Object-ExpandPropertylastLogonDate

 

I have an issue with the formatting of the four $DC0x statements

If I output to the console, by typing $DC01, I get

09/07/2014 13:26:36 

 

In the CSV file I build I get

@{LastLogon=09/07/2014 13:26:36}

In the past I have used –ExpandProperty to strip off the @{Lastlogon=….} bit,  like I do on the line for $lastLogonDate.  If I try that on the $DC01 line I get no output to the screen or in the CSV file; which to me looks the variable is empty.

So what have I got wrong?

 

For completeness this is the whole script

$HRCheck=new-objectpsobject

 

#Gets the prior month year to use when creationg the CSV file

$Month=(get-date).AddMonths(-1).ToString("yyyy-MMMM")

 

 

$WasHere=import-csv'C:\PurgeUserRecords\From HR\Test.csv'

#Checks if a user with that name exits in Active Directory

$WasHere|Foreach-object{

$Name=$_.CN

$Exists=Get-ADObject-Filter {Name-eq$Name} -Properties*|Select-Object-ExpandPropertysamaccountname

 

#Gets the LastLogon from each DC, turns into a readable output, and the replicated lastLogonDate

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

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

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

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

$lastLogonDate=Get-ADUSer$Exists-Properties*|Select-Object-ExpandPropertylastLogonDate

 

#Gets the User's Manager and gets the Name instead of the CN (as it is easire to read)

$LM=Get-ADUser$Exists-Properties*|Select-Object-ExpandPropertyManager

$Manager=Get-ADUser$LM|Select-Object-ExpandPropertyName

 

#Check what variables actually are

write-host$Exists

$DC01

$DC02

$DC03

$DC04

write-host$lastLogonDate

write-host$Manager

 

#Builds table to create CSV output

$HRCheck|add-membernotepropertyDisplayName$Name

$HRCheck|add-membernotepropertyDC01$DC01

$HRCheck|add-membernotepropertyDC02$DC02

$HRCheck|add-membernotepropertyDC03$DC03

$HRCheck|add-membernotepropertyDC04$DC04

$HRCheck|add-membernotepropertylastLogonDate$lastLogonDate

$HRCheck|add-membernotepropertyManager$Manager

$HRCheck|Export-csvc:\LeftIn-$Month.csv

}

 

 

 

 

 

 


Viewing all articles
Browse latest Browse all 10624

Trending Articles