1

I'm new to PowerShell and I'm trying to save few clicks by creating a script for daily tasks (Get Username, Reset Password and Unlock Account).

Here is the issue: When I'm launching my script, Powershell will execute everything except cmdlets and will finish by them.

For instance, when I'm using the first choice (Get username ) it will prompt me the "Press enter to end the script" and then display the result of my GetUserName Function.

Please find my code below :

# Author : Maxime
# Creation date :  20/06/2018
# Version : 0.1
Function GetUserName($name)
{
    $u = 'surname -like "' +$name+ '"'
    $res = Get-ADUser -Filter $u | select Name, GivenName, Surname
    return $res
}


echo "Menu"
echo "------"
$choix = read-host " 1. Trouver utilisateur par nom `n 2. Reset Password `n 3. Deverrouiller compte `nChoix  "


if($choix = 1)
{
    $name = Read-Host "Entrez nom utilisateur "
    GetUserName($name)

}
elseif( $choix = 2) 
{
    $id = Read-Host "Entrez ID"
    Set-ADAccountPassword $id -confirm -Reset
}
elseif ($choix -= 3)
{
    $id = Read-Host "Entrez ID "
    Unlock-ADAccount -Confirm $id
}
else
{
    echo "Mauvais choix"
}



read-host "Press enter to end the script ..."

Edit:

When I'm launching the script without ISE:

enter image description here

And when I'm launching the script with the ISE, I can see that my result is displayed after the confirmation to close.

I would like the script to Display the information and then displaying the confirmation to close the window.

enter image description here

6
  • I don't really understand the problem. As far as I see it looks good the code ( I don't have a server to check right now). Commented Jun 20, 2018 at 10:02
  • Let met edit and add screenshot :) Commented Jun 20, 2018 at 10:07
  • When I test it it'll display output but after you press enter (I'm testing in ISE so I see it). Try to use this (save to variable and pipe to table: $res = GetUserName($name); $res|ft Commented Jun 20, 2018 at 10:10
  • @robdy, Exactly ! Your solution works, do you have any idea why it's displaying after the 'press enter' ? Commented Jun 20, 2018 at 10:14
  • Can be something related to this. However, I'm glad it works, let me convert it to an answer so you can accept it if you wish. Commented Jun 20, 2018 at 10:18

1 Answer 1

2

To ensure that output is displayed before prompt from Read-Host you can use:

$res = GetUserName($name)
$res | Format-Table

Very likely it's related with some delay from Select-Object function (also reported here).

Another option is to change this line

$res = Get-ADUser -Filter $u | select Name, GivenName, Surname

to

$res = Get-ADUser -Filter $u | fl Name, GivenName, Surname

When you use Format-List or Format-Table there's no delay and your output is displayed in correct order.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.