trimstart() method not modifying variable RRS feed

  • Question

  • Hi -

    I'm trying to write a script that will show me the last time a 2012 computer in each of several child OUs logged in to the domain. I've got most of the script working, but for some reason, the trimstart(<string>) and trimend(<string>) aren't working. It's not modifying the date retrieved. Instead, I get something like "@{lastlogondate=10/08/2017 19:09:59}". I'd like to remove the "@{lastlogondate=" and the "}" at the end and just have the date and time. Here's the code I used. $dn is the variable for the distinguishedName of the OU I'm querying:

    [string]$llts2012 = Get-ADComputer -filter {OperatingSystem -Like '*2012*'} -SearchBase $dn -SearchScope Subtree -Properties lastlogondate | select-object -property lastlogondate | sort lastlogondate -descending | select-object -first 1
    $llts2012 = $llts2012.trimstart("@{lastlogondate=")
    $llts2012 = $llts2012.trimend("}")

    I've also tried "$llts2012 = $llts2012.replace("@{lastlogondate=",""), but that doesn't work either. I get the same result.

    I get that the contents of $llts2012 is an array of characters, but I've strongly-typed the variable as a string and run $llts2012 | gm to verify that it is a string.

    The weird part is, if I run this exact same code except for variable names for other OSes (Win 2008, Win 2016, etc.) it work perfectly. 2012 is the only OS that has a problem. I don't think it's OS related, but it's just weird that it works for all of the other OSes in all of the OUs, just not 2012.

    Any suggestions would be greatly appreciated.

    Thank you.

    • Edited by GA123 Monday, October 9, 2017 5:34 PM
    Monday, October 9, 2017 5:33 PM

All replies

  • Your code does not make any sense.  What is it you are trying to do.  You cannot use trimstart to modify a date and the string you have doe's not have the value you expect.

    Get-ADComputer -filter { OperatingSystem -Like '*2012*' } -SearchBase $dn  -Properties lastlogondate | 
    	Sort-Object lastlogondate -descending |
    	select-object lastlogondate -first 1


    • Proposed as answer by I.T Delinquent Tuesday, October 10, 2017 10:13 AM
    Monday, October 9, 2017 9:00 PM
  • I'm trying to find out when the last time a computer running a given OS logged in from each of the child OU's. I grab a list of all of the last logon dates for all of computers running that OS in each OU, then sort them in descending order (so the most recent login is at the top of the list), then select that first one. If I run the code manually, it does generate a string object in the format of  "@{lastlogondate=9/17/2017 10:27:14}" for example. I'm just trying to remove the  "@{lastlogondate=" at the beginning and the "}" at the end and leave the date and time.

    If you know of a better way, I'd appreciate any input.


    Tuesday, October 10, 2017 2:46 PM
  • The code I'm posted works as required.  It returns the lastlogondate. It is a date object and not a string so it cannot be manipulated as a string.

    Please try the code I posted to understand what is happening. In PowerShell all things are objects. We normally do not use or want strings except when saving to a file.

    Learn PowerShell: https://mva.microsoft.com/en-us/training-courses/getting-started-with-microsoft-powershell-8276


    • Edited by jrv Tuesday, October 10, 2017 3:05 PM
    Tuesday, October 10, 2017 3:04 PM
  • Try to pipe the variable to 'Out-String'
    Tuesday, October 10, 2017 6:19 PM
  • Hi,

    I tried your code snippet as it was. It returned the date, but in the "@{lastlogondate=11/8/2016 12:55:20 AM}" format. I'm trying to remove the extra text and leave JUST the date and time. Any suggestions?

    Tuesday, October 10, 2017 8:57 PM
  • No it doesn't return that because you changed the code.

    The output is NOT a string it is a date object.

    As you can clearly see the object is datetime object and cannot be "trimmed" and you cannot put it in quotes.

    This is what happens when turned into a string:

    PS C:\scripts> "$lastlogon"
    @{lastlogondate=10/06/2017 09:22:21}
    PS C:\scripts>

    You must get the property as I have done above and it will be the date. When that is quoted or output to a file it will be what you want.

    Learn PowerShell: https://mva.microsoft.com/en-us/training-courses/getting-started-with-microsoft-powershell-8276


    • Edited by jrv Tuesday, October 10, 2017 9:40 PM
    Tuesday, October 10, 2017 9:35 PM
  • Thanks for the suggestion. I noticed that when I run the command from the command console, the output is as you said. However, I'm trying to run the output via an Export-CSV command, and that appears to change the formatting of the output. It still comes out in the format of  "@{lastlogondate=9/17/2017 10:27:14}" .

    $results = [pscustomobject][ordered]@{"OU" = $ouname;"Last Win2008 Computer Logon" = $llts2008;"Last Win2012 Computer Logon" = $llts2012} |Export-Csv-PathC:\Stats.csv-Append-NoTypeInformation

    I've looked in several places on line, and I can't find a way to correct that issue. I understand that I'm dealing with a date object and that you can't manipulate the text. I just couldn't figure out another way to get the formatting the way I wanted.


    • Edited by GA123 Thursday, October 12, 2017 2:49 PM
    Thursday, October 12, 2017 2:48 PM
  • The command you just posted is incorrect.  You cannot capture output on both ends of a pipeline.  It also tells us nothing about how you created the variables.

    You are also write code that you don't need to write.  Just use a "select" to reference the properties you want to output and the system will take care of the rest.

    I strongly recommend that you take the time to learn basic PowerShell as it is clear that you have not understood the previous answers.

    Please do not post colorized code as it is unreadable in most browsers and is damaged when you paste it.

    Get-ADComputer -filter { OperatingSystem -Like '*2012*' } -SearchBase $dn -Properties lastlogondate |
    	Sort-Object lastlogondate -descending |
    	select-object Name, lastlogondate| 
    	Export-Csv-PathC:\Stats.csv -Append -NoTypeInformation 
    This will correctly output dates.


    Thursday, October 12, 2017 9:09 PM