+ Post New Thread
Results 1 to 2 of 2
Scripts Thread, Powershell: $var[x] returning Character x not Line/Item x? in Coding and Web Development; Not so long ago a few of you helped me write a script to scan the event log and return ...
  1. #1


    Join Date
    Jan 2012
    Posts
    2,615
    Thank Post
    934
    Thanked 350 Times in 266 Posts
    Rep Power
    212

    Powershell: $var[x] returning Character x not Line/Item x?

    Not so long ago a few of you helped me write a script to scan the event log and return the last logged-on users. I'm looking to expand that, given that all I ever do with these results is e-mail them off to the relevant head of department and be done with it. It's not my duty to chase the kids or dish out punishments. So! I figure why not just automate this bit, too?

    Sending an e-mail is rather simple, I've tested it and it works:
    Code:
    Send-MailMessage -smtpserver 'EMAIL SERVER' -To "HOD" -Cc "ME" -From "ME" -Subject $computername -Body "Blah blah blah"
    Now I'm fairly certain I can find a way to apply variables into that (Already tested with the Subject and that worked). Unfortunately, I'm having a little bit of a struggle cutting down the actual event log so I can assign the required values into a canned e-mail and fire it off with minimal user intervention.
    Since the following line works, I figure I could just use $event.message[12] and work from there:
    Code:
    $fulllogs += ([string]"Log $count`r`n----------" + $newtime + "----------`r`n" + $event.message + "`r`n" -replace [string]"(?s)Detailed.*key was requested.", "---------- Event message end ----------`r`n`r`n")
    Apparently, however, that doesn't work. Instead of returning " Account Name: $username" (the 13th line) it returns "a" (the 13th character as the event.message always starts with An account was successfully logged on.)

    I can't convert it with [array], I can't grab it with Get-Content, and I can't just directly reference it as $event.message[12] nor System.Diagnostics.Eventing.Reader.EventRecord.Message.. So I'm a little confused as to where to go from here. It feels like I have everything in place except this one problem of character vs line, where, up until now, $var[x] has always returned the line or item.
    Last edited by Garacesh; 26th November 2013 at 02:07 PM.

  2. #2


    Join Date
    Jan 2012
    Posts
    2,615
    Thank Post
    934
    Thanked 350 Times in 266 Posts
    Rep Power
    212
    Aha! Getting somewhere..
    Code:
    $testvar += $newtime + ([regex]::split($event.message, "`r`n") | Where {$_ -match '	Account Name:		'+ "[0-9a-zA-Z]*"}) + "`r`n"
    Unfortunately, this pulls back two results per event log
    Account Name: MACHINENAME$
    Account Name: USERNAME

    But the machine name always ends in '$' and since I'm explicitly specifying only 0-9, a-z and A-Z (I also tried [[:alnum:]]) so as far as I see, the line with Machinename$ shouldn't be matched. But as far as I'm aware, * means 'zero or more of the previous character', so it should only match zero or more characters that are numbers or letters.

    I've also tried using [^-\$]*, which if I'm right, should be 'any character that is not a - or a $', but it still brings back both lines.

    Gaaaah! >.<


    Woohoo! Figured it out.
    Technically, "LOC-000$" still met the requirements.. It started with Account Name: and then had zero or more characters that were not a dash or a dollar sigh (in this case, L)

    Code:
    $testvar += $newtime + ([regex]::split($event.message, "`r`n") | Where {$_ -match '	Account Name:		'+ "[^-\$]*$"}) + "`r`n"
    sorted me right
    Last edited by Garacesh; 27th November 2013 at 12:24 PM.

SHARE:
+ Post New Thread

Similar Threads

  1. Replies: 34
    Last Post: 20th September 2013, 01:50 PM
  2. Group Policy - Settings report shows Not Configured Items
    By Arcolite in forum Windows Server 2008 R2
    Replies: 20
    Last Post: 25th July 2013, 04:32 AM
  3. GPResult returns "Error: Not Found"
    By philjones2000 in forum Windows
    Replies: 15
    Last Post: 18th November 2011, 09:47 AM
  4. [CLOSED] Misc: sponsors logo not in line
    By FN-GM in forum EduGeek.net Site Problems
    Replies: 0
    Last Post: 2nd August 2008, 12:40 AM
  5. HP laptop not returned
    By Ayaz in forum Hardware
    Replies: 11
    Last Post: 1st August 2008, 11:34 AM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •