Skip to main content

How do I ask a user to re-enter information?

Today in my PowerShell class, the question came up of how to ask a user for valid information if what they provided is not correct.  I did not want to go into parameter validation just yet so I did it in code.  This allowed me to demonstration a looping construct and how to use functions inside of your code.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

7

72

73

74

75

76

Function Get-MyEvents

{

    [cmdletBinding()]

    Param (

           [parameter(Mandatory=$true)]

           [String]$Logname,

           [Int]$Newest = 5)

 

    # -- Test-Log ----------------------------------------------------------------

    # Test to determine if the log file is valid.

    # Return code 0 for yes and 1 for no.

    Function Test-Log

    {

        Param (

        $LogName

        )

        $ErrorCode = 0

        Try

        {

            Get-EventLog -LogName $Logname -Newest 1 -ea Stop

        }

        Catch

        {

            $ErrorCode = 1         

        }

        Write-Output $ErrorCode

    }

 

    # -- Get-Info ----------------------------------------------------------------

    # Get the event log info.

    Function Get-Info

    {

        Param($LogName, $Newest)

        Get-EventLog -LogName $Logname -Newest $Newest

    }

 

    # -- Main Code ---------------------------------------------------------------

    # Set the error code to 1 (Unsuccessful)

    $ErrorCode = 1

 

    # Loop until the Errorcode is 0.

    Do

    {

        # Call Test-Log to retrieve an error code.

        $ErrorCode = Test-log -LogName $LogName

 

        # If the error code is 1, the ask for a valid file.

        If ($ErrorCode -eq 1)

        {

            $LogName = Read-Host('Please enter a valid log file')

          

        }

 

    } While ($ErrorCode -eq 1)

 

    # Get the log file information.

    Get-Info -LogName $Logname -Newest $Newest

  

    <#

    .SYNOPSIS

    This gets me event.

 

    .DESCRIPTION

    Gets what ever event log I say.

 

    .PARAMETER Logname

    This is the name of the logfile you wat to get info from.

 

    .PARAMETER Newest

    The number of events to return.

 

    .EXAMPLE

    Get-MyEvents -Logname System -Newest 10

    This recovered the newest 10 events from the system log.

    #>

}

 

In the Main Code, starting at line 37, I created a variable called $ErrorCode and set it to 1.  As long as this variable is equal to 1, the user would be asked for the name of a valid log file.

In line 45, I give the opportunity for that error code to be changed by calling the function Test-Log. This function will attempt to call a record from the event log specified by the user.  If it is successful, an ErrorCode of zero is returned.  If it is not, an ErrorCode of 1 is returned.

At line 48, if the ErrorCode is 1, the user is prompted for the name of a different log.  The loop then executes and the process starts over again.

Line 57 calls another function that actually gets the log information.  This function call could have been omitted and just instead move line 34 to line 57.  Again, I was demonstrating calling functions.

Comments

Popular posts from this blog

Adding a Comment to a GPO with PowerShell

As I'm writing this article, I'm also writing a customization for a PowerShell course I'm teaching next week in Phoenix.  This customization deals with Group Policy and PowerShell.  For those of you who attend my classes may already know this, but I sit their and try to ask the questions to myself that others may ask as I present the material.  I finished up my customization a few hours ago and then I realized that I did not add in how to put a comment on a GPO.  This is a feature that many Group Policy Administrators may not be aware of. This past summer I attended a presentation at TechEd on Group Policy.  One organization in the crowd had over 5,000 Group Policies.  In an environment like that, the comment section can be priceless.  I always like to write in the comment section why I created the policy so I know its purpose next week after I've completed 50 other tasks and can't remember what I did 5 minutes ago. In the Group Policy module for PowerShell V3, th

Return duplicate values from a collection with PowerShell

If you have a collection of objects and you want to remove any duplicate items, it is fairly simple. # Create a collection with duplicate values $Set1 = 1 , 1 , 2 , 2 , 3 , 4 , 5 , 6 , 7 , 1 , 2   # Remove the duplicate values. $Set1 | Select-Object -Unique 1 2 3 4 5 6 7 What if you want only the duplicate values and nothing else? # Create a collection with duplicate values $Set1 = 1 , 1 , 2 , 2 , 3 , 4 , 5 , 6 , 7 , 1 , 2   #Create a second collection with duplicate values removed. $Set2 = $Set1 | Select-Object -Unique   # Return only the duplicate values. ( Compare-Object -ReferenceObject $Set2 -DifferenceObject $Set1 ) . InputObject | Select-Object – Unique 1 2 This works with objects as well as numbers.  The first command creates a collection with 2 duplicates of both 1 and 2.   The second command creates another collection with the duplicates filtered out.  The Compare-Object cmdlet will first find items that are diffe

How to list all the AD LDS instances on a server

AD LDS allows you to provide directory services to applications that are free of the confines of Active Directory.  To list all the AD LDS instances on a server, follow this procedure: Log into the server in question Open a command prompt. Type dsdbutil and press Enter Type List Instances and press Enter . You will receive a list of the instance name, both the LDAP and SSL port numbers, the location of the database, and its status.