Advanced Windows PowerShell Scripting Video Training

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

Thursday, October 30, 2014

Getting the Last Modification Time of a GPO

Ideas are rolling in my 6293: Troubleshooting and Supporting Windows® 7 in the Enterprise class here in Yokosuka, Japan.  Although PowerShell is not part of this class, the only answer to many questions is with PowerShell.  OK, the best answer is with PowerShell.

The problem that the client was facing is that a problem may mysteriously pop up.  The question, is there a way to determine if Group Policy has recently been modified?  The GPO object contains both a creation, and the last modification time stamp.  The code below will let you either gather the modification time stamps for all GPOs, or those that have be modified within a specific time period.

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

 

 

Function Get-GPOModifications

{

    Param (

        [INT]$Hours = 0,

        [INT]$Days = 0

    )

   

    # The IF statement will execute only if the HOURS or DAYS

    # parameters are used.  If not, all GPOs will be reported.

    If (($Hours -ne 0) -or ($Days -ne 0))

    {

     Get-GPO -All  |

        Where-Object {$_.ModificationTime -gt ((Get-Date).AddHours((-1*$Hours)+(-1*$Days*24)))} |

        Select-Object -Property DisplayName, ModificationTime

    }

    Else

    {

     Get-GPO -All  |

        Select-Object -Property DisplayName, ModificationTime

    }

<#

.SYNOPSIS

Recovers the last modification time of all group policies

 

.DESCRIPTION

Recovers the last modification time of all group policies or allows you to search for GPOs that have been recently modified.

 

.PARAMETER Days

Returns the GPOs that have been modified within the provided number of days.

 

.PARAMETER Hours

Returns the GPOs that have been modified within the provided number of hours.

 

.EXAMPLE

Get-GPOModifications

 

Returns all GPOs in the current domain with their latest modification times.

 

.EXAMPLE

Get-GPOModifications -Hours 2

Returns all GPOs that have been modified within the last 2 hours.

 

.NOTES

===============================================================================

Copyright MCTExpert of Arizona 2014.  All Rights Reserved.

- Code is provided as is and without warranty.

- User assumes all risk and liability.

===============================================================================

#>

} # End: Function Get-GPOModifications

Monday, October 27, 2014

Use PowerShell to Add and Update to an Image

Yesterday I posted about a question that I received in a Windows 7 class here in Yokosuka, Japan.  It was how to add an update while servicing an image.  Well, because of the technology used, we used the DOS program called DISM.  PowerShell V3 running on Windows 8 and up has the DISM module.  So to bring things into the modern era, here is how to do the same thing with PowerShell.

First we mount the image.

PS E:\> Get-WindowsImage -ImagePath install.wim | Mount-WindowsImage -Path img -Index 1

 

The Get-WindowsImage cmdlet will get the Install.wim object.  We can then pipe that to Mount-WindowsImage.  We need to provide the –Path to the directory that will hold the mounted image and the index number of the image that we want to work with.

Now we can add the package to the image:

PS E:\> Add-WindowsPackage -Path Img -PackagePath Windows6.1-KB976264-v2-x86.msu

 

Now we can dismount the image and Commit the changes.

PS E:\> Dismount-WindowsImage -Path IMG -Save

 

Compared to yesterday’s post, the PowerShell way is a lot easier to understand.

 

Sunday, October 26, 2014

Use DISM to add a Windows Update to an Image

Today in my Windows 7 class here in Japan, I was asked if it is possible to apply an update to windows while servicing an image. That little voice in my mind said yes, but I had to look it up. So, I set up a VM with Windows 7 and downloaded KB976264 for Windows 7. This is a file with a .MSU file extension.

clip_image002

Here you can see the Install.wim fir Windows 7 and the KB article.

Next, I mount the image.

clip_image004

I then add the update to the image.

clip_image006

Next I commit the updates. In the image below, I used the Discard switch. If I wanted to keep the changes, I would have used the Commit switch.

clip_image008

 

Not to bad.  Tomorrow I will do the same process, but this time with PowerShell.

Wednesday, October 15, 2014

How to set PowerShell to Always Open in Version 2

One of the things that I love most about being an MCT is that I get to meet a lot of people who look at things differently.  For example, in my PowerShell class this afternoon, I had one individual ask if it was possible set PowerShell to open in V2 by default. They were in a mixed environment between V2, 3, and 4. My first instinct was to add this line to their PowerShell profile. 

PowerShell -Version 2

Then I thought about it.  I would much rather have version 5 as my default and just create a different icon for version 2.  So this is what I did.

Right click your desktop and select New –> Shortcut.

In the Type the location of the Item: box, enter PowerShell.exe –Version 2 and click Next

image

In the Type a name for this shortcut box, I gave the name PowerShell V2 and clicked Finish.

image

I then opened the icon that it created.

image

When you type Get-Host the Version property should show that you are using V2.  If you want to switch to V5 in this windows, just type PowerShell –Version 5 and press Enter.

Tuesday, October 14, 2014

How to Extract the Username of the ManagedBy property of an Organizational Unit

This morning in my PowerShell class, we looked at creating calculated properties.  Fortunately for us, I found one of our fellow IT professionals on PowerShell.com that needed a little bit of help that involved calculated properties.  This individual was attempting to enumerate all of the Organizational Units in their Active Directory domain and extract the username in the MangedBy property.  The thing is, the ManagedBy property gives you the distinguished name of the user, not the user name.

Get-ADOrganizationalUnit -filter * |

Select-Object -Property Name, ManagedBy 

Groups                                                                                         

Printers                                                                                       

Printers                                         CN=James,CN=Users,DC=Adatum,DC=com            

Printers                                                                          

By using calculated properties and controlling the order of operation, we can perform an extra step and extract the username using the distinguished name.

1

2

3

4

Get-ADOrganizationalUnit -filter * |

Select-Object -Property Name,

@{N="ManagedBy";E={Get-ADUser -Identity $_.ManagedBy |

Select -expandProperty SamAccountName}}

Line 1 gathers all Organizational Unit objects in Active Directory

Line 2 selects out the Name property and leaves a comma so we can continue on the next line.

Line 3 starts our calculated property.  We give new our property the name ManagedBy. The value, or expression, of this property is actual the result of the PowerShell command Get-ADUser and the result of line 4.  The expression in line 3 gets the User object for whom ever is in the Organizational Unit’s ManagedBy property.  It then sends that object to Select-Object to extract out the actual SamAccountName.  The SamAccountName is the actual value of our calculated property.

Thursday, October 2, 2014

Set PowerShell as Your Default Instead of the Command Prompt

As most of you know who have been in my classes, I generally do everything in PowerShell.  Yes, the Command Prompt is still there. But most of the time I just work in the PowerShell console.  For you Windows 8.1 users, give this a try.

Press Windows Key + X

 

image

Notice that the Command Prompt is listed.  Well, that does not help us PowerShell users out.  We can change this behavior in Windows 8.1.

From the Desktop, right click the Taskbar and click Properties

Click the Navigation tab.

Now check Replace Command Prompt with Windows PowerShell in the menu when I right-click the lower-left corner or press Windows Key + X.

 image

Click OK

Now press Windows Key + X and see what pops up.

image

This is just a short cut to getting you on the right path.