Advanced Windows PowerShell Scripting Video Training

Advanced Windows PowerShell Scripting Video Training
Advanced Windows PowerShell Scripting Video Training

Monday, November 18, 2013

Windows 8 Keyboard Layout is Wrong in Virtual Lab

This one popped up in a class this morning using virtual labs.  While attempting to hit the “\” key, the “#” character was being displayed.  In the lower right hand corner, take a look and the language.

image

It says English, but what about the keyboard layout. Click on ENG to see what it is actually set to:

image

In this case, the keyboard layout is English (United Kingdom).  Click on the US keyboard layout to fix this problem.

Wednesday, November 6, 2013

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.

Tuesday, November 5, 2013

Watch those Verbs!

A good question came up in class today.  We were working with the registry and a student needed to know how to change the value of a registry property using New-PropertyItem.  Well, we can’t exactly do that.  Remember that PowerShell has different verbs that describe the intent of the cmdlet.  The Approved Verbs for Windows PowerShell list these verbs.

  • New creates
  • Set changes
  • Remove destroys

The verbs are there to help us find what we need.  For example, what if I need to change an IP address, but I do not know the cmdlet?   Try this:

Get-Command Set-*ip*.  From experience, I knew that there is a good chance that IP would be in the noun.  From the verb list, I know to look for “Set” verbs.  Here is the difference. 

PS C:\> Get-Command set-*ip* | Measure-Object | Select -ExpandProperty Count

20

 

PS C:\> Get-Command *ip* | Measure-Object | Select -ExpandProperty Count

187

The first number, 20, shows how many possible cmdlets returned when you know to use the “Set” verb.  Without it, you would have to look at 187 cmdlets.

Take the time to get to know the PowerShell verbs.  they will help make your PowerShell usage much more efficient and effective.

Monday, November 4, 2013

What time is it???

Today I’m virtually delivering a class to IT pros in India, The United Kingdom, and the United States.  Time difference can be a bit of an issue.  I’m directing time based for the class in GMT (Greenwich Mean Time).  Of course since this is a PowerShell course, I’m doing this in PowerShell.

Two of the methods provided to us from the .NET framework System.DateTime is ToLocalTime and ToUniversalTime

So let’s say that I need everyone to be back in the virtual classroom in an hour and a half.  I can run this command to get the equivalent time in GMT.

((Get.Date).AddHours(1.5)).ToUniversalTime()

Let’s say that the GMT time will be 18:30.

For the class to convert the GMT time to their local time:

(Get-Date –Date “18:30Z”).ToLocalTime()

Problem solved!