Monday, October 14, 2013

Powershell Tips : Use Only Powershell V2 cmdlets , with PowershellV3 Installed.

 

I use Windows 7 (64 bit) with Powershell V3 installed on it. Most of our users are still on PowerShell v2, Sometime when I am writing scripts for them i use some PowerShell v3 cmdlets accidently.

Normally I write the initially version of all scripts on PowerShell console and I do test my all script in the console only.

So I thought there must be a way to run PowerShell.exe with Version 2 only.

I found the solution and i thought I should share it with You.

It’s simple, Just run “Powershell.exe –Version 2” and this will open a PowerShell console with V2 cmdlets only.

Let’s test it.

When i run $PSVersionTable  in my PowerShell console, you can see that  my PSversion is 3.0.

14-10-2013 17-07-33

Now lets run “Powershell.exe –Version 2” .

14-10-2013 16-46-57

and new PowerShell console will open and let’s check it’s $PSVersionTable.

You can see that this PowerShell version of this console is 2.0.

14-10-2013 17-07-59

Now let’s check one more thing.  Let’s see if version 2 is having Version 3.0 cmdlets or not (just to make our self bit  more confident).

Let’s search for all cmdlets those has the word Update. The Version 3 has a new cmdlet named “Update-Help”.

Get-Command *update* -CommandType cmdlet

14-10-2013 17-08-23

and then run the same command on v2 PowerShell console.

You can check that , Update-Help cmdlet is not available on V2 console :)

14-10-2013 17-08-55

Now i can use it and create a V2 script more confidentiality, without worrying of using v3 cmdlets accidentally. . :) 

Note: I tested this on Windows 7 (64 bit) with PowerShell v3 installed.

Thanks for reading and your time,.

Thanks

Aman Dhally

clip_image001 clip_image002 clip_image003 clip_image005  clip_image007

Friday, October 11, 2013

Find Operating System Start time using Powershell.


Today, I have been asked  to do a strange task, My manager told me to find a Operating System Start time.

I was thinking about an half hour  on how to get this done, then i thought about Event Logs, every thing is recorded there, after a few minutes of hardworking , i have found the event which tells us about the Start time of the Operating System.

He wants a history of  start up time of all dates, and luckily windows keep history of start up time in Event Logs.

The Event ID : 12, shows a information on at what time the Operating system was  started.


11-10-2013 17-23-24
and we know how to use Powershell and  how to use Get-EventLog cmdlet.
and then it a matter of few seconds , and I have the list of Operating System’s start time.
run the below command to get the list of start time of the operating system.

Get-EventLog -Log System  |  Where {  $_.EventID -eq 12    } | Select TimeGenerated


Here is the result.

11-10-2013 20-15-13
Happy Weekend. Have fun :) .

Thanks
Aman Dhally
clip_image001 clip_image002 clip_image003 clip_image005  clip_image007

Wednesday, October 9, 2013

Enable useSSLtrasport in VMware Mirage using Powershell.

 

mirage

As i always says, Powershell is a Sanjivini  for system administrators, You are use Powershell to do anything you like to do, and it is bless when it comes to create a fix, patch, automate some tasks.

Do you ever used VMware Mirage? If not ? try it, it is a cool applications. We are using VMware mirage in our environment.

Today , we did some changes on our VMware Mirage server and we changes the server’s connection mode to basic to SSL.

No we need to change some settings on the Mirage clients, we have approx 500 Mirage client installed.

Then i tried to find the solution first on how to change the Mirage Client’s connection mode to SSL and I have found the solution here :  https://communities.vmware.com/message/2297120

The Solution is “Edit the Wanova.Desktop.service.exe.config file and modify the "Key"  "useSslTransport" to true and then restart the Wanova Mirage service

And when i was reading about the solution , few things start coming in my mind:

  1. Stop “Wanova Mirage server”  service | okey, i can use Stop-Service cmdlet.
  2. modify Wanova.Desktop.service.exe.config  file | XML can be done easily.
  3. Start “Wanova Mirage server”  service | Use Start-Service cmdlet.

The last thing which come in my mind , i can script it, i can script it.

And here is complete and i added comments so that it would be easily understandable.

Download link : http://gallery.technet.microsoft.com/Enable-useSSLtrasport-in-cb341ad5

# Notes :-

#             --->   Please run this script as "Administrator"

#        Tested on : 64 Bit Client of Mirage.

#                  : 64 Bit Windows 7 Professional. 

 

# testing for file first

 

$testWanovaFile = Test-Path 'C:\Program Files\Wanova\Mirage Service\Wanova.Desktop.Service.exe.config'

 

#

if ( $testWanovaFile -eq $true )

 

       {

 

              # Mapping confifuration file as XML

              [xml] $wanovaXmlFile =  get-content 'C:\Program Files\Wanova\Mirage Service\Wanova.Desktop.Service.exe.config'

 

              # Finding the useSSLTransport Attribute

              $useSslKey = $wanovaXmlFile.configuration.appSettings.add | where { $_.Key -match "useSslTransport" }

 

              # Processing

 

              # If the UseSSL is not set to true

              if ( $useSslKey.value  -ne $true )

                     {

                          

                           # Stopping Wanova Mirage Desktop Service

                           Write-Warning "Stopping Wanova Desktop Service."

                      Stop-Service "Wanova Mirage Desktop Service" -Force

                     

                           # Setting SSLkey value to True

                           Write-Warning "Setting useSslTransport value to true"

                           $useSslKey.value = "true"

                     

                           # Saving the file with changes

                           Write-Warning "Saving the Configuration file."

                      $wanovaXmlFile.Save('C:\Program Files\Wanova\Mirage Service\Wanova.Desktop.Service.exe.config')

                     

                           # Staring the Service again

                           Write-Host "Starting Wanova Desktop Service." -ForegroundColor 'Green'

                           Start-Service "Wanova Mirage Desktop Service"

 

                     }

 

              else {

 

                           Write-Host "Value already set to true " -ForegroundColor 'Green'

 

                     }

      

       }

 

else

       {

      

       Write-Warning "Mirage Doesn't seem installed on this $env:COMPUTERNAME laptop"

      

       }     

 

###---- end of the script

Moral of the story: if you know PowerShell, you can fix every ( except broken heart ) thing.

I tested this on : Windows 7 64 Bit and with Mirage 64 Bit Client.

Download link : http://gallery.technet.microsoft.com/Enable-useSSLtrasport-in-cb341ad5

Thanks for your time.

Thanks

Aman Dhally

clip_image001 clip_image002 clip_image003 clip_image005  clip_image007

Tuesday, October 8, 2013

ON-OFF Keyboard Lock keys {Caps,Scroll,Num} Using PowerShell.

 

There is no need to prove that PowerShell is a very cool technology, we already proved it many times.

For me PowerShell is like a blank white canvas and you are painter, now it is up to us and on our imaginations what we can create.

we can create a beautiful usable scripts things using colour full lines.

Ok! now lets come on point,

Today one of my user complained that his password is not working, I reset his password but he still complaining about it, I asked him to check of his CAPSLOCK , key is on , he checked and told me that CAPSLOCK key on/off indicator light is not working,

Finally i went to this seat and i checked , his CAPSLOCK was on and somehow the indicator light was not working,

Once his CAPSLOCK key was set to off , his password started working again.

This problem give me an idea about , what if, i want to check the status of lock keys?, what if I want to on/off lock keys using PowerShell.

After few hours of work, i am successfully able to create a PowerShell script ( Ya , I am using .NET objects too.) for this task.

The script is straight forward and i am added lots of comments in to the script for easy understanding.

When you download the script and run it first time, it will ON your all lock keys :) . (pretty cool) .

For a basic understanding.  i am using “[System.Windows.Forms.Control]::IsKeyLocked('CapsLock')”  to check the status of the CAPSLock key, if the status is true that means Caps lock is ON and if the status is false them that means CAPS Lock is off.

TO  ON/Off the lock key, i am adding Wscript.Shell and  a Powershell Object  “$keyBoardObject = New-Object -ComObject WScript.Shell”  and then I am using  SendKey method to ON lock keys “$keyBoardObject.SendKeys("{CAPSLOCK}")”.

Download Complete Code : http://gallery.technet.microsoft.com/ON-OFF-Keyboad-Lock-keys-6ba9885c

Num-Lock,-Caps-Lock-and-Scr

 

# Creating a WScript.Shell onject

 $keyBoardObject = New-Object -ComObject WScript.Shell

 

 

# Getting the status of Keys using .Net Object , this result of the ISkeyLock is in Boolean

 

# Capsock

 $capsLockKeySatus = [System.Windows.Forms.Control]::IsKeyLocked('CapsLock')

 

 

 

if ( $capsLockKeySatus -eq $false )

       {

              Write-Host "Capslock key is off"

             

              # if you want to ON the CapsLock {IF CAPSLOCK IS ALREADY OFF }, then please unComment the below Command.

                $keyBoardObject.SendKeys("{CAPSLOCK}")

       }

 

else {              

              Write-Host "Capslock key is On"

             

              # if you want to OFF the CapsLock Key {IF CAPSLOCK IS ALREADY on }, then please unComment the below Command.

              #-->   $keyBoardObject.SendKeys("{CAPSLOCK}")

             

       }

 

Download Complete Code : http://gallery.technet.microsoft.com/ON-OFF-Keyboad-Lock-keys-6ba9885c

I hope you will like it and found it useful.

Thanks

Aman Dhally

clip_image001 clip_image002 clip_image003 clip_image005  clip_image007

Saturday, October 5, 2013

Getting Last Installation Success Date and Last Search Success date of Windows Updates using Powershell.

 

Everyday is a new day , with new problems and new issues, sometime at users side and sometime at Server side. The life in IT is quite happening.

These new issues and problems leads to new solutions and new scripts.

In my environment I asked few users to run their windows updates, but as you know user never listen ;o), So i thought to find out which users had install and run windows update and which is not.

The first thing which come in my mind is to check any com object available to windows update.

Luckily i found a “Microsoft.Update.AutoUpdate” com object and i thought lets give it a try.

I create a new Powershell Object using New-Object cmdlet.

$windowsUpdateObject = New-Object -ComObject Microsoft.Update.AutoUpdate

05-10-2013 00-22-27

My this new variable has a property of Results. So when I access the .Result property using dot notation, i have found the result what i was looking for,  The windows last Search success date and Windows update last installation date.

$windowsUpdateObject.Results

05-10-2013 00-58-11

 

$windowsUpdateObject.Results.LastInstallationSuccessDate

 

05-10-2013 00-27-33

 

$windowsUpdateObject.Results.LastSearchSuccessDate

 

05-10-2013 00-27-57

You can see the windows automatic basic setting using .Settings property.

$windowsUpdateObject.Settings

05-10-2013 01-03-07

The most cool thing which i have found is ShowSettingsDialog(), method. When you run this method it opens a Windows Update configuration GUI.

$windowsUpdateObject.ShowSettingsDialog()

05-10-2013 01-04-02

I hope you like it.

Thanks for your time and Happy Weekend.

Thanks

Aman Dhally

 

clip_image001 clip_image002 clip_image003 clip_image005  clip_image007

Wednesday, October 2, 2013

I am a PowerShell MVP now.

 

Yesterday, to be truthful few hours ago, I have received an email from Microsoft saying that they awarded me with the MVP award.

It is a big day of my life. It’s a honour to receive this award.  There are plenty of thing to say, but I am saving them for a next blog post.

I would like to say thanks to all PowerShell users and community members those supported me in all years and this encourage me write more blogs and  sharing scripts.

Knowledge has to be share not to be store or keep secret, I think that is the main motto of MVP’s.

I am very happy to be the part of MVP programme and to be the part of one of the best technical community.

And Yes, now i can use this Picture :)

MVP_FullColor_ForScreen

Aman Dhally


clip_image001 clip_image002 clip_image003 clip_image005clip_image007