Kaygee
 New Member Posts:5

 |
| 27 Jun 2010 10:47 PM |
|
I am trying to get a list of servers from AD and dump them into excel, detailing name, o/s, creationdate and lastlogontimestamp
The issue I have is that I cannot get anything useful from the data for lastlogontimestamp. My understanding is that it is a number based on time elapsed from 1601. Could someone please help me with code modification to get a date for lastlogontimestamp instead of an elapsed time number?
Code I am using as follows:
$strCategory = "computer" # type of object to be seached for, AD attribute $objDomain = New-Object System.DirectoryServices.DirectoryEntry # Selects the domain as the one the user is connected to $objSearcher = New-Object System.DirectoryServices.DirectorySearcher $objSearcher.SearchRoot = $objDomain $objSearcher.Filter = "(&(objectCategory=computer)(!operatingSystem=Windows XP Professional)(!operatingSystem=EMC Celerra File Server)(!operatingSystem=Mac OS X)(!operatingSystem=Windows 7 Enterprise)(!operatingSystem=Windows 7 Professional)(!operatingSystem=Windows Vista™ Enterprise)(!operatingSystem=Windows Vista™ Ultimate))" # the above lines filter what out o/s' that should not be included in results $colProplist = "name", "operatingSystem", "createtimestamp", "lastlogontimestamp" # create an array of properties to be returned foreach ($i in $colPropList){$objSearcher.PropertiesToLoad.Add($i)} $colResults = $objSearcher.FindAll() $colResults | foreach { $objItem = $_.Properties $obj = new-object psobject add-member -inp $obj noteproperty ServerName $($objItem.name) add-member -inp $obj noteproperty OperatingSystem $($objItem.operatingsystem) add-member -inp $obj noteproperty Created $($objItem.createtimestamp) add-member -inp $obj noteproperty LastLogon $($objItem.lastlogontimestamp) $obj } | export-csv c:\scripts\servers.csv -noType
Example output:
ServerName OperatingSystem Created LastLogon Server1 Windows Server 2003 13/10/2005 01:28 1.29214E+17 Server2 Windows Server 2003 21/02/2006 00:02 1.29217E+17 Server3 Windows Server? 2008 Standard 02/07/2009 01:35 1.29216E+17
ie. lastlogontimestamp is outputting number such as 1.29214E+17 |
|
|
|
|
Shay Levy PowerShell MVP, Admin
 Veteran Member Posts:1362

 |
|
Kaygee
 New Member Posts:5

 |
| 28 Jun 2010 03:53 PM |
|
Ok so I have edited the line in question to read as follows:
add-member -inp $obj noteproperty LastLogon $([DateTime]::FromFileTime($objItem.ConvertLargeIntegerToInt64($objItem.lastlogontimestamp.Value)))
Now I get the following error message:
Method invocation failed because [System.DirectoryServices.ResultPropertyCollection] doesn't contain a method named 'ConvertLargeIntegerToInt64'. At C:\scripts\serverst.ps1:17 char:108 + add-member -inp $obj noteproperty LastLogon $([DateTime]::FromFileTime($objItem.ConvertLargeIntegerToInt64 <<<< ($objItem.lastlogontimestamp))) + CategoryInfo : InvalidOperation: (ConvertLargeIntegerToInt64:String) [], RuntimeException + FullyQualifiedErrorId : MethodNotFound |
|
|
|
|
cameronove
 Basic Member Posts:332

 |
| 28 Jun 2010 05:16 PM |
|
Similar to Shay's try this: Get-Date -Date ([DateTime]::FromFileTime([Int64]::Parse($ADUser.lastlogontimestamp))) |
|
|
|
|
Kaygee
 New Member Posts:5

 |
| 28 Jun 2010 05:37 PM |
|
How should I incorporate that into the line:
should it be:
add-member -inp $obj noteproperty LastLogon $Get-Date -Date ([DateTime]::FromFileTime([Int64]::Parse($objItem.lastlogontimestamp))
? |
|
|
|
|
cameronove
 Basic Member Posts:332

 |
| 28 Jun 2010 06:47 PM |
|
Well I prefer to create my objects the quick way. $variable = '' | Select property1,property2,property3... $variable becomes an object with three noteproperties in case name property1,2,3. Here is how I'd conclude your script :
$obj = '' | select ServerName,OperatingSystem,Created,LastLogon
. . .
{
$obj.ServerName = $objItem.name
$obj.OperatingSystem = $objItem.operatingsystem
$obj.Created = $objItem.createtimestamp
$obj.LastLogon = Get-Date -Date ([DateTime]::FromFileTime([Int64]::Parse($objItem.lastlogontimestamp))) $obj
} | Export-Csv c:\scripts\servers.csv -NoTypeInformation |
|
|
|
|
Kaygee
 New Member Posts:5

 |
| 28 Jun 2010 08:24 PM |
|
Nope, all thts done is generate eleventy billion errors :-( |
|
|
|
|
Shay Levy PowerShell MVP, Admin
 Veteran Member Posts:1362

 |
| 29 Jun 2010 12:13 AM |
|
Kaygee, my solution was using PowerShell v2. Cameron, is this realy works for you, cause it failes for me: PS > $user = [adsi]"LDAP://CN=Shay Levy,OU=Users,DC=Domain,DC=com" PS > [DateTime]::FromFileTime([Int64]::Parse($user.lastlogontimestamp)) Exception calling "Parse" with "1" argument(s): "Input string was not in a correct format."
|
|
Shay Levy Windows PowerShell MVP
http://PowerShay.com
PowerShell Community Toolbar
Twitter: @ShayLevy |
|
|
cameronove
 Basic Member Posts:332

 |
| 29 Jun 2010 08:26 AM |
|
Sorry Shay you are correct it doesn't work that way. But I also couldn't get it to work with your call. I found that .lastlogontimestamp returns a System.__ComObject which has a method gethashcode(). However, that doesn't have the correct information in it either as you can see from the date below. PS C:\Windows> [DateTime]::FromFileTime([Int64]::Parse($cameron.lastlogontimestamp.gethashcode())) Sunday, December 31, 1600 7:00:05 PM When you use ADSI does it return the actual int64 when you reference lastlogontimestamp or an object?
|
|
|
|
|
cameronove
 Basic Member Posts:332

 |
| 29 Jun 2010 08:31 AM |
|
However this worked perfectly for me and as you can see I used the Parse method that I noted above...
$obj = '' | select ServerName,OperatingSystem,Created,LastLogon $strCategory = "computer" # type of object to be seached for, AD attribute $objDomain = New-Object System.DirectoryServices.DirectoryEntry # Selects the domain as the one the user is connected to $objSearcher = New-Object System.DirectoryServices.DirectorySearcher $objSearcher.SearchRoot = $objDomain $objSearcher.Filter = "(&(objectCategory=computer)(!operatingSystem=Windows XP Professional)(!operatingSystem=EMC Celerra File Server)(!operatingSystem=Mac OS X)(!operatingSystem=Windows 7 Enterprise)(!operatingSystem=Windows 7 Professional)(!operatingSystem=Windows Vista™ Enterprise)(!operatingSystem=Windows Vista™ Ultimate))" # the above lines filter what out o/s' that should not be included in results $colProplist = "name", "operatingSystem", "createtimestamp", "lastlogontimestamp" # create an array of properties to be returned foreach ($i in $colPropList){$objSearcher.PropertiesToLoad.Add($i)} $colResults = $objSearcher.FindAll() $colResults | foreach { $objItem = $_.Properties $obj.ServerName = $objItem.name $obj.OperatingSystem = $objItem.operatingsystem $obj.Created = $objItem.createtimestamp $obj.LastLogon = Get-Date -Date ([DateTime]::FromFileTime([Int64]::Parse($objItem.lastlogontimestamp))) $obj }
|
|
|
|
|
cameronove
 Basic Member Posts:332

 |
| 29 Jun 2010 08:48 AM |
|
Ok so not perfectly. I was reviewing my results and found one error. It was because lastlogontimestamp was null. So here is a solution for when the script encounters a null lastlogontimestamp attribute... I also got rid of the Get-Date because in the script I pulled that from I used that cmdlet to format the date. This just returns a raw date now...
$obj = '' | select ServerName,OperatingSystem,Created,LastLogon $strCategory = "computer" # type of object to be seached for, AD attribute $objDomain = New-Object System.DirectoryServices.DirectoryEntry # Selects the domain as the one the user is connected to $objSearcher = New-Object System.DirectoryServices.DirectorySearcher $objSearcher.SearchRoot = $objDomain $objSearcher.Filter = "(&(objectCategory=computer)(!operatingSystem=Windows XP Professional)(!operatingSystem=EMC Celerra File Server)(!operatingSystem=Mac OS X)(!operatingSystem=Windows 7 Enterprise)(!operatingSystem=Windows 7 Professional)(!operatingSystem=Windows Vista™ Enterprise)(!operatingSystem=Windows Vista™ Ultimate))" # the above lines filter what out o/s' that should not be included in results $colProplist = "name", "operatingSystem", "createtimestamp", "lastlogontimestamp" # create an array of properties to be returned foreach ($i in $colPropList){$objSearcher.PropertiesToLoad.Add($i)} $colResults = $objSearcher.FindAll() $colResults | foreach { $objItem = $_.Properties $obj.ServerName = $objItem.name $obj.OperatingSystem = $objItem.operatingsystem $obj.Created = $objItem.createtimestamp if($objItem.lastlogontimestamp -ne $null){ $obj.LastLogon = [DateTime]::FromFileTime([Int64]::Parse($objItem.lastlogontimestamp)) } $obj } | ft -auto
|
|
|
|
|
cameronove
 Basic Member Posts:332

 |
| 29 Jun 2010 09:00 AM |
|
Wow...sorry for not getting it completely right the first time. One more time. If the lastlogontimestamp is null you need to have the column let you know if it is not known. The last script was putting the previous lastlogon in the place of the unknown attribute. This corrects that by adding an else and putting 'UNKNOWN' in its place. Again, sorry for the crappy scripting (I should have worked it all the way through)...
$obj = '' | select ServerName,OperatingSystem,Created,LastLogon $strCategory = "computer" # type of object to be seached for, AD attribute $objDomain = New-Object System.DirectoryServices.DirectoryEntry # Selects the domain as the one the user is connected to $objSearcher = New-Object System.DirectoryServices.DirectorySearcher $objSearcher.SearchRoot = $objDomain $objSearcher.Filter = "(&(objectCategory=computer)(!operatingSystem=Windows XP Professional)(!operatingSystem=EMC Celerra File Server)(!operatingSystem=Mac OS X)(!operatingSystem=Windows 7 Enterprise)(!operatingSystem=Windows 7 Professional)(!operatingSystem=Windows Vista™ Enterprise)(!operatingSystem=Windows Vista™ Ultimate))" # the above lines filter what out o/s' that should not be included in results $colProplist = "name", "operatingSystem", "createtimestamp", "lastlogontimestamp" # create an array of properties to be returned foreach ($i in $colPropList){$objSearcher.PropertiesToLoad.Add($i)} $colResults = $objSearcher.FindAll() $colResults | foreach { $objItem = $_.Properties $obj.ServerName = $objItem.name $obj.OperatingSystem = $objItem.operatingsystem $obj.Created = $objItem.createtimestamp $obj.LastLogon = if($objItem.lastlogontimestamp -ne $null){ [DateTime]::FromFileTime([Int64]::Parse($objItem.lastlogontimestamp)) }else{'UNKNOWN'} $obj } | ft -auto
I tested this and came up error free and with expected results... |
|
|
|
|
Kaygee
 New Member Posts:5

 |
| 29 Jun 2010 10:46 PM |
|
Thanks for your help everyone but I got the answer on another forum.
Actually had the whole code rewritten by an MVP, but to keep it simple the thing that most needed to change was to change as follows:
this line:
add-member -inp $obj noteproperty LastLogon $($objItem.lastlogontimestamp)
becomes:
add-member -inp $obj noteproperty LastLogon $(([DateTime]::FromFileTime($($_.Properties['lastlogontimestamp']))))
|
|
|
|
|