Welcome to MSDN Blogs Sign in | Join | Help

Free English version of Windows PowerShell Course book

Frank Koch has translated his Windows PowerShell course book into English and made it available for free.  You can get the free book here

 

Arul Kumaravel

Windows PowerShell Development Manager

Microsoft Corporation.

Posted by arulk | 0 Comments

How Can I Delete All the Duplicates in a Set of Processes But Keep the Oldest Process?

  This blog corresponds to the scripting guy column with the same title.   I am posting the script for doing the same with PowerShell

   1: $processes = get-wmiobject -query "Select * from win32_process where name = 'notepad.exe'"
   2:  
   3: if ($processes.count -le  2 )
   4: {
   5:   return
   6: }
   7:  
   8: $datetarget = [DateTime]::Now
   9:  
  10: foreach ($process in  $processes)
  11: {
  12:    $dateholder =  $process.CreationDate
  13:    $dateholder =  $process.ConvertToDateTime($dateholder)
  14:  
  15:    if ( $dateholder -le  $datetarget)
  16:    {
  17:     $processid =   $process.ProcessID
  18:         $datetarget  =  $dateholder
  19:    }
  20:  
  21: }
  22:  
  23:  
  24: $processes = get-wmiobject -query " select * from win32_process where name='notepad.exe' AND processID <> $processid"
  25:  
  26: foreach ($process in $processes)
  27: {
  28:    $process.Terminate()
  29: }
Posted by arulk | 2 Comments

How Can I Determine the Day of the Week?

 

Hey, PowerShell! I have a script that does certain management tasks based on the day of the week. I know how get the date in a script, but how can I tell whether it’s a Monday or a Tuesday or whatever?

This is very easy to do with Syste.Datetime class.

param ([system.datetime]$date = $([system.datetime]::now))
 
$dayofweek = $date.DayOfWeek
$intday = [int] $date.DayOfWeek
 
Write-Output "The day of given date is $dayofweek  and numerical value of day is $intday"

Let's see how this script works  The script begins by assigning the date from commandline or current date  to a variable named $date.

Next we use the DayofWeek  property to determine the day of the week for given date  DayofWeek is going to return one of the following value .  This is [System.DayOfWeek]  enum class values.

0      Sunday

1      Monday

2      Tuesday

3     Wednesday

4     Thursday

5      Friday

6      Saturday

if you want the numerical value of this enum, you can convert it to integer by using the [int] type converter.

Posted by arulk | 0 Comments

How Can I Determine the Date for the Friday in a Given Week?

Hey, Powershell! Given a date, can a script tell me the date that the Friday for that week occurs?

 

 Here’s a script that – given a date – will report back the date that the Friday of that week falls on:

 

param ([system.datetime]$date = $([system.datetime]::now))
 
$intday = [int] $date.DayOfWeek
$intadder =  5 - $intday
$dateFriday = $date.AddDays( $intadder)
 
Write-Output "The friday of the given date is  $dateFriday"
 

Let's see how this script works  The script begins by assigning the date from commandline or current date  to a variable named $date.

 

Next we use the DayofWeek  property to determine the day of the week for given date  DayofWeek is going to return one of the following value

 

0      Sunday

1      Monday

2      Tuesday

3     Wednesday

4     Thursday

5      Friday

6      Saturday

 

This gives us a value representing the day of the week; What we need to do now is programmatically determine the number of days between this date and the Friday of that week. That’s something we can do with a single line of code:

$intadder = 5 - $intDay

Confused? Don’t be; this actually makes sense. If you refer to the table you’ll see that Friday has a value of 5. If we take the value for Friday (5) and subtract the value for  given date we will get the days to add to get Friday.  That’s what we do with this line of code:

$dateFriday = $date.AddDays($intddder)

 

System.DateTime is a powerful class that allows you to do date manipulation very easily in PowerShell

 

----------------------------------------------------------------------------------------------------------------------

Arul Kumaravel

Development Manager

Windows PowerShell

Posted by arulk | 1 Comments

How Can I Determine the Beginning and Ending Date of the Previous Month?

Hey, PowerShell! Given a specific date, how can I determine the beginning and ending date of the previous month? In other words, given 8/11/2005, I need to get back 7/1/2005 and 7/31/2005.

Powershell provides access to .Net framework.  We can use .Net framework  DateTime class to solve the problem.  Let's write a script to solve this.

First, we need to get the date for which we need to determine the beginning and ending date of previous month. We can do this using PowerShell Param statement

 

Param ([System.Datetime] $date = $(get-date))

In the above statement, we are declaring a parameter of type Syste.DateTime.  We are also initializing it to current date when no parameter is specified.  It works as follows.  Let's say that we have saved the above as script file  BeginEndPrevMonth.ps1 then

.\BeginEndPrevMonth.ps1  11/18/2006

will set the $date variable to  11/18/2006.  However, if you don't specify the parameter like

.\BeginEndPrevMonth.ps1

then PowerShell parser will execute the  $(get-date)  which will get you the current date and time and this value gets assigned to $date.

 

Let's move on.  Now that we have a date,  we need to find the beginning and endind date of the previous month of this date.  Let's start the end date.  if I have a date like  8/11/2005,  then I find out the last date of previous month by subtracting the number of days in the current date.  In the example, if I subtract 11 then I will get 7/31/2005.  We can accomplish this in the script as follows

 

First,  determine the number of days in given date

$numdays = $date.Day

Once we have determined the number days,  we need to subtract this from the current date to get the last date of previous month.  There is no subract method available in  Datetime class. however, there is a AddDays method.  We can use this method to subtract days by passing in a  negative value to add. 

 

$prevmonthlastday = $date.AddDays(- $numdays)

We are calling  AddDays method in DateTime with  - $numdays to subtract the number of days.  We use the similar logic to subtract the number of days from  $prevmonthlastday to determine that last day of previou to previous month and then add 1 day to get the first date.

 

 

$numdays = $prevmonthlastday.Day
$prevmonthfirstday = $prevmonthlastday.AddDays( -$numdays + 1)

 

The full script is  as follows

 

 

Param ([system.Datetime] $date = $(get-date))

Write-Output $("The selected date is {0}" -f $date )


$numdays = $date.Day


$prevmonthlastday = $date.AddDays(-$numdays)

Write-Output $("Last date of previous month of given date is {0}" -f $prevmonthlastday )

$numdays = $prevmonthlastday.Day
$prevmonthfirstday = $prevmonthlastday.AddDays( -$numdays + 1)

Write-Output $("First date of previous month of given date is {0}" -f $prevmonthfirstday)

 

There you go ,  a script to determine beginning and ending date of previous month of a given date.

-----------------------------------------------------------------------------------------------------------------------------------------------------------------

Arul Kumaravel

Development Manager

Windows PowerShell

 

 

Posted by arulk | 0 Comments

Transcript of Solving Management Problems with Windows PowerShell

Along with Jeffrey, I presented Solving Management Problems with Windows PowerShell session at TechEd Barcelona.   Many people asked for the script examples  I used during the presentation. Attached zip file contains transcript as well as input files used during presentation.

------------------------------------------------------------------------------------------------------------------- 

Arul Kumaravel [MSFT]

Development Manager,

Windows PowerShell,

My PowerShell blog: http://blogs.msdn.com/arulk

Posted by arulk | 1 Comments

Attachment(s): TechEd.zip

Windows PowerShell 1.0 Launched

I am here at TechED-IT Forum at  Barcelona, Spain.   Today, in his keynote address Bob Muglia, Senior Vice President, Microsoft Corporation, officially launched Windows PowerShell 1.0.  Jeffrey Snover showed an awesome demo of using PowerShell to manage IIS 7  in Longhorn  Server.

 

 

PowerShell has been multiyear effort by many members of Windows PowerShell team.  We would like to take this time to thank all our early adopters and customers who provided us with valuable feedback in shaping up this product. It is with pride that we launch this product. 

 

PowerShell is ready for primetime to solve management problems one script at a time.

 

Posted by arulk | 0 Comments

Reading Attributes of Directory Service Objects

The previous blogs described how to create an OU, a user account, and a group, and set the description attribute on each of these objects. The next common task is to read an attribute of each object.

Reading an Active Directory object’s attributes involves two simple steps:

  1. Connect to the Active Directory object you want to read.
  2. Read one or more of the object’s attributes.

The goal of the three scripts in this blog will be to read the description attribute of the HR OU, the MyerKen user account, and the Atl-Users group and display their values on the screen.

Reading an Attribute of an OU

The  following script reads and displays the description attribute of the OU named HR in the na.fabrikam.com domain. To carry out this task, the script performs the following steps:

  1. Connect to the HR OU object in the na.fabrikam.com domain.
  2. Read the object’s description attribute.

Reading the description Attribute of an OU

$objOU = [ADSI]"LDAP://localhost:389/OU=HR,dc=NA,dc=fabrikam,dc=com"

$objOU.Get("description")

When this script runs in the na.fabrikam.com domain, it echoes the description of the HR OU to the command window, as shown:

Human Resources

Reading an Attribute of a User Account

The following script reads and displays the description attribute of the user account named MyerKen, located in the HR OU of the na.fabrikam.com domain.

  1. Connect to the MyerKen user account object in the HR OU of the na.fabrikam.com domain.
  2. Read the object’s description attribute.

Reading the description Attribute of a User Account

$objUser = [ADSI]"LDAP://localhost:389/CN=MyerKen,OU=HR,dc=NA,dc=fabrikam,dc=com"

$objUser.Get("description")

When this script runs in the na.fabrikam.com domain, it echoes the description of the user account to the command window, as shown:

HR employee

Reading an Attribute of a Group

The following script reads and displays the description attribute of a global group named Atl-Users, located in the HR OU of the na.fabrikam.com domain.

  1. Connect to the Atl-Users group in the HR OU of the na.fabrikam.com domain.
  2. Read the object’s description attribute.

Reading the description Attribute of a Group

$objGroup =[ADSI]"LDAP://localhost:389/CN=Atl-users,OU=HR,dc=NA,dc=fabrikam,dc=com"

$objGroup.Get("description")

When this script runs in the na.fabrikam.com domain, it echoes the description of the group to the command window, as shown:

Atlanta users

Important observations about the scripts in this blog

  • They perform the same basic steps: They connect to an Active Directory object and read an attribute of the object.
  • They use the same method (Get) without regard to the class of object being read.

As demonstrated in this blog, the process for reading attributes is uniform from one object to the next. In fact, within a particular task, the steps you follow from one object to the next are consistent. This consistency empowers you to write scripts that can read thousands of attributes from the many objects stored in Active Directory.

Posted by arulk | 4 Comments

New community project created for Windows PowerShell Extensions

We have worked with CodePlex team to create a new Community Project for Windows PowerShell.   Keith Hill, MVP for Windows PowerShell, will be the project coordinator.  Microsoft employees won't be able to participate in the community projects directly due to legal issues. However, I sincerely hope that community participates in this project and contribute.

 

You can access the project at

http://www.codeplex.com/Wiki/View.aspx?ProjectName=PowerShellCX

 

Existing  Windows PowerShell GotDotNet project at

http://www.gotdotnet.com/workspaces/workspace.aspx?id=bd8982c2-9e47-4793-b7c8-35a4c9105330

will be migrated over to this project.

 

 

Arul Kumaravel

Development Manager

Windows PowerShell

Microsoft Corporation

 

Posted by arulk | 0 Comments

Technorati Profile

Here is my Technorati profile information for this blog. Technorati Profile
Posted by arulk | 0 Comments

Modifying Directory Service Objects with Windows PowerShell

Modifying an object is equivalent to writing an attribute to an existing object in Active Directory. If an attribute contains a value, modifying it will clear the existing value and replace it with a different value.

 

Typically, the type of modification you make to an object will depend on the type of object you want to modify and various characteristics of the attribute — for example, whether the attribute holds a single value or multiple values. For simplicity, however, the following task descriptions illustrate how to write a single value to the same attribute in three different objects.

 

Modifying attributes of Active Directory objects involves three basic steps:

1.      Connect to the Active Directory object you want to modify.

2.      Modify one or more of the object’s attributes.

3.      Commit the change to Active Directory.

 

The goal of the three scripts in this section is to write an attribute to each of the objects created in "Creating Directory Service Objects"  blog. The objects include the HR OU, the MyerKen user account, and the Atl-Users global group. The description attribute is contained in all three of these objects, so it is used as the attribute to modify.

 

Modifying an Attribute of an OU

 

The following script modifies the description attribute of the OU named HR in the na.fabrikam.com domain. The description attribute is assigned the value Human Resources. To carry out this task, the script performs the following steps:

1.      Connect to the HR OU object in the na.fabrikam.com domain.

In contrast with the create task, the HR OU is referred to as an object rather than a container because the task completed in this script is to write an attribute of an object.

2.      Modify the object’s attributes by assigning the description attribute the value Human Resources.

3.      Commit the change to the OU in Active Directory.

 

Writing the description Attribute to an OU

 

$objOU = [ADSI]"LDAP://localhost:389/ou=HR,dc=NA,dc=fabrikam,dc=com"

$objOU.Put("description", "Human Resources")

$objOU.SetInfo()

 

 

Modifying an Attribute of a User Account

 

The following script modifies the description attribute of the user account named MyerKen in the HR OU of the na.fabrikam.com domain. The description attribute is assigned the value HR employee. To carry out this task, the script performs the following steps:

1.      Connect to the MyerKen user account object in the HR OU of the na.fabrikam.com domain.

2.      Modify the object’s attributes by assigning the description attribute the value HR employee.

3.      Commit the change to the user account in Active Directory.

 

Writing the description Attribute to a User Account

 

$objUser = [ADSI]"LDAP://localhost:389/cn=MyerKen,ou=HR,dc=NA,dc=fabrikam,dc=com"

$objUser.Put("description", "HR employee")

$objUser.SetInfo()

 

Modifying an Attribute of a Group 

The following script modifies the description attribute of the group account named Atl-Users in the HR OU of the na.fabrikam.com domain. The description attribute is assigned the value of Atlanta users. To carry out this task, the script performs the following steps:

1.      Connect to the Atl-Users group in the HR OU of the na.fabrikam.com domain.

2.      Modify the object’s attributes by assigning the description attribute the value Atlanta users.

3.      Commit the change to the group in Active Directory.

 

Writing the description Attribute to a Group

 

$objGroup = [ADSI]"LDAP://localhost:389/cn=Atl-users,ou=HR,dc=NA,dc=fabrikam,dc=com"

$objGroup.Put("description", "Atlanta users")

$objGroup.SetInfo()

Important observations about the scripts in this section are:

  • They perform the same basic steps: They connect to an Active Directory object, modify an attribute of the object, and write the change to the corresponding Active Directory object.
  • They use the same method (Put) without regard to the class of object being modified.

 

Arul Kumaravel

Development Manager

Windows PowerShell

Microsoft Corporation

 

PSMDTAG:FAQ: How to modify directory Services objects ?

PSMDTAG:FAQ: How to modify  ADSI objects?

 

 

Posted by arulk | 5 Comments

WindowsITPro article on Windows PowerShell

Windows IT Pro Magazine has an article on Windows PowerShell

http://www.windowsitpro.com/Windows/Article/ArticleID/50565/50565.html

Check it out.

Arul Kumaravel

Development Manager

Windows PowerShell

Microsoft Corporation

Posted by arulk | 0 Comments

ADSI Scripting with Windows PowerShell.

We have made significant improvements in ADSI support in the upcoming release of Windows PowerShell RC2.  In this and future blogs, I will talk about how to use Windows PowerShell for ADSI Scripting.  Windows Scripting Guide 2000 provided scripting examples with VBS. The content for this blog is adapted from Windows 2000 Scripting guide.

 

ADSI Scripting with Windows PowerShell

 

 

 

Active Directory management is all about management of directory objects from creation to deletion.   There are four main categories of tasks in active directory management

 

  1. Create
  2. Modify
  3. Read
  4. Delete

 

 

We will take a look at how to do these tasks using Windows PowerShell Scripts.  

 

 

Creating Directory Service Objects

 

 

Creating Active Directory objects involves four basic steps:

  1. Connect to the Active Directory container that will store the new object.
  2. Create the object.
  3. Set the object’s mandatory attributes, if necessary.
  4. Commit the new object to Active Directory.

The goal of the three scripts in this section is to create an OU named HR (Human Resources department), a user account named MyerKen in the HR OU, and a group named Atl-Users, also in the HR OU.

 

Creating an OU

 

The following script creates an OU named HR in the na.fabrikam.com domain. All mandatory attributes of an OU are automatically assigned a value by Active Directory. Therefore, the step that sets mandatory attributes does not appear in this script

To carry out this task, the script performs the following steps:

  1. Connect to the na.fabrikam.com domain container.
  2. Create an OU object named HR.
  3. Commit the new OU to Active Directory.

 Creating an OU

 

$objDomain = [ADSI]"LDAP://localhost:389/dc=NA,dc=fabrikam,dc=com"

$objOU = $objDomain.Create("organizationalUnit", "ou=HR")

$objOU.SetInfo()

 

Creating a User Account

The following script  creates a user account named MyerKen in the OU named HR. The HR OU is located in the na.fabrkam.com domain. To carry out this task, the script performs the following steps:

  1. Connect to the HR OU container in the na.fabrikam.com domain.

HR is the OU that was created by running the previous script

  1. Create a user account named MyerKen.

Using an uppercase letter for the first letter of the last and first name is not necessary. However, the case is preserved when the object is saved to Active Directory. Therefore, users will be able to distinguish the last name from the first name when searching Active Directory.

  1. Set the sAMAccountName mandatory attribute to the value myerken.

There is no need to capitalize the first letter of the last and first name for this attribute’s value because, typically, users do not perform user account searches on the sAMAccountName attribute.

  1. Commit the new user account to Active Directory.

Creating a User Account

 

$objOU = [ADSI]"LDAP://localhost:389/ou=HR,dc=NA,dc=fabrikam,dc=com"

$objUser = $objOU.Create("user", "cn=MyerKen")

$objUser.Put("sAMAccountName", "myerken")

$objUser.SetInfo()

 

Creating a Group

The following script creates a global group named Atl-Users in the OU named HR, located in the na.fabrikam.com domain. To carry out this task, the script performs the following steps:

  1. Connect to the HR OU container in the na.fabrikam.com domain.
  2. Create a group named Atl-Users.

By default, the script creates a global group.

  1. Set the sAMAccountName mandatory attribute to a value of Atl-Users.

Like creating a user account, creating a security group requires a single mandatory attribute, sAMAccountName.

  1. Commit the new group account to Active Directory.

Creating a Group

 

$objOU = [ADSI]"LDAP://localhost:389/ou=HR,dc=NA,dc=fabrikam,dc=com"

$objGroup = $objOU.Create("group", "cn=Atl-Users")

$objGroup.Put("sAMAccountName", "Atl-Users")

$objGroup.SetInfo()

 

Important observations about the scripts in this section are:

  • They perform the same basic steps: They connect to an Active Directory container, create an object, set the object’s mandatory attributes (if necessary), and commit the object to Active Directory.
  • They use the same method (Create) without regard to the class of the object being created.
  • The script parameters are the only parts of the scripts that are different. Each script contains the class name (organizationalUnit, user, and group) identifying the type of object to create and the object’s corresponding attributes (the new object’s name and the user’s and group’s mandatory sAMAccountName attribute).

 

We will look into other tasks in future blog postings.

 

Arul Kumaravel

Development Manager

Windows PowerShell

Microsoft Corporation

 

PSMDTAG:FAQ: How to create directory Services objects

PSMDTAG:FAQ: How to create ADSI objects?

Posted by arulk | 5 Comments

Windows PowerShell blog for French users

Jacques Barathon has created a new  Windows PowerShell blog.  What is unique about this blog is that it is entirely in French.  If you are on the French speaking administrator who is reluctant to try out Windows PowerShell due to lack of documentation then you should definitely check out his blog.

Speaking of documentation in other languages, Windows PowerShell team is hard at work to localize the product and content to all Windows Server supported languages.  Expect to see localized content available soon.

 

Posted by arulk | 0 Comments

Training course for Windows PowerShell from DesktopEngineer.com

I came across this recently,DesktopEngineer.com has acourse on Windows PowerShell for Administrators. 

You can visit their website at http://desktopengineer.com/ps310 for more details.

Posted by arulk | 0 Comments
More Posts Next page »
 
Page view tracker