0

I'm writing a simple script which displays a menu where one can choose from various scripts. One of those scripts should return the location which got a user account locked.

The script I want to use is this one here: https://gallery.technet.microsoft.com/scriptcenter/Get-LockedOutLocation-b2fd0cab

I added three lines to the end of the script so that I can read the output before the window gets closed:

    $username = Read-Host 'Please enter a username'
    Get-LockoutLocation -Identity $username
    Read-Host 'Press a key to quit'

The script gets executed with this command:

   start powershell -ArgumentList '.\get-lockout-location.ps1'

When I execute the script it first returns the domain controller which locked the account. However the next thing which appears is the line which asks for a key press to quit. And at the end comes the output from the event log.

Does someone know what I have to change so that the key press lines is the last thing that appears? I tried already quite a lot of things (executing from a different file, piping the output, removing the process part, putting the Read-Host line to a different position)and none of it worked.

Nebucatnetzer
  • 15
  • 1
  • 5
  • Possible duplicate of [Unable to Pause or Sleep after Select-Object](https://stackoverflow.com/questions/34835327/unable-to-pause-or-sleep-after-select-object) – user4003407 Jul 14 '17 at 18:21

1 Answers1

0

This happens because the author of that script forces formatting on the first set of output, but not the second.

Change the last Foreach() loop to:

$Events = Foreach($Event in $LockedOutEvents) 
{             
   If($Event | Where {$_.Properties[2].value -match $UserInfo.SID.Value}) 
   {  

      $Event | Select-Object -Property @( 
        @{Label = 'User';               Expression = {$_.Properties[0].Value}} 
        @{Label = 'DomainController';   Expression = {$_.MachineName}} 
        @{Label = 'EventId';            Expression = {$_.Id}} 
        @{Label = 'LockedOutTimeStamp'; Expression = {$_.TimeCreated}} 
        @{Label = 'Message';            Expression = {$_.Message -split "`r" | Select -First 1}} 
        @{Label = 'LockedOutLocation';  Expression = {$_.Properties[1].Value}} 
      ) 

    }#end ifevent 

}#end foreach lockedout event 
$Events |Format-List
Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206