Lost Password? No account yet? Register
Member Area

Software Installation Technologies

May 17th

Home arrow Community arrow External Blogs arrow Scripting

Scripting
Arul Kumaravel's WebLog
Windows PowerShell technology blog

  • 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.



  • 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: }


  • 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.



  • 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



  • 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

     

     




Visitors: 501140

Extended Menu