Friday, October 25, 2013

Working with PowerShell Preference Variables: $OFS (Output field Separator).

 

When you open PowerShell console, type $ and start pressing ‘tab’, you will amaze that there are many pre-built variables exists there.

One of the variable is $OFS, this is a preference variable and full meaning of $OFS is ‘Output Fields Separator’.

What it does?

It specified the character to use as separator while returning the result from the array. By default when the results of an array come, it has a single space as a separator.

By default the value of  $OFS is null.

Let me show to you.

I am using PowerShell version 3.0. , and this $OFS variable is exists in PowerShell version 2 too.

1

When you type and run the  $OFS variable the result is blank.

2

For testing, let’s create a array of the script and checked the output.

[string]$myStringArray = 'Aman', 'Singh' , 'Dhally'

3

Now, Lets run the “$myStringArray ” variable.

You can see that, all items in our array result are separated by a single space.

4

Now let’s assign a semi-colon as a separator in $OFS variable.

 $OFS = ";"

4.0

Now create a array of string variable same as before.

Now , you can see, when you run the $myStringArray variable this time, all items of the array are separated with semi-colon rather then like single space before.

6

You can use comma as a separator,

7

or anything you like,  ;)

8

this is very handy, when you are planning to export data in to CSV files, you can have the option to choose separators of your choice  rather then using the single space.

Happy Learning.

Tested on Windows 7 [64 bit]

Regards

Aman Dhally

 
clip_image001 clip_image002 clip_image003 clip_image005  clip_image007

Wednesday, October 23, 2013

PowerShell Script : Find the name of Installed Antivirus on local or remote laptop.

 

Do you ever want to know what antivirus installed on remote laptop, or does remote system has a antivirus or not, knowing this is  always a pain, normally you have to call user and ask them,.

Today i have found a cool namespace named “"root\SecurityCenter2" ”  and this name space has a class “AntiVirusProduct ”, you just need to query that class and it will show you the name { and few more info}  of the antivirus installed.

Download Link : http://gallery.technet.microsoft.com/Get-The-Name-of-Installed-b10fd073 

You can download a little script { if you like } from the above link and when i run it, you can see that it is showing me the name of antivirus installed on my laptop.

23-10-2013 18-14-35

You can run the above script on remote laptop too.

function Get-AntivirusName {

 

[cmdletBinding()]   

param (

[string]$ComputerName = "$env:computername" ,

$Credential

)

 

 

       BEGIN

              {

                     # Setting WMI query in a variable

              $wmiQuery = "SELECT * FROM AntiVirusProduct"

 

              }

 

 

       PROCESS

              {

                     # doing getting wmi

               

                     $AntivirusProduct = Get-WmiObject -Namespace "root\SecurityCenter2" -Query $wmiQuery  @psboundparameters # -ErrorVariable myError -ErrorAction 'SilentlyContinue'         

            Write-host $AntivirusProduct.displayName -ForegroundColor Cyan

 

              }

 

       END {

 

              }

} #end  of the function

 

Thanks for reading.

Aman Dhally

Tested on : Windows7 [64 bit]

 
clip_image001 clip_image002 clip_image003 clip_image005  clip_image007

Tuesday, October 22, 2013

PowerShell Tip : Find Default Printer Installed using Powershell.

 

The job of System Administrators are very adventurous, sometime you have to find a little-little settings and sometime you have to write a big-big,  hundred’s of line’s scripts.

Today, my task was to find a default printer installed on laptops. To solve it , i used the Win32_Printer class.

You can run the below query on your laptop and this will show you the default installed  printer on your system.

To run the below command on the remote system you can –Computer parameter and make sure you are proper admin rights to access the system.

We are using Get-WmiObject cmdlet and then we are running a WQL query to select everything from Win32_Printer class and choose to show that result where default value is true.

Simple

Get-WmiObject -Query " SELECT * FROM Win32_Printer WHERE Default=$true"

21-10-2013 17-53-29

If you want to see all printers installed on your laptop, you can run the below code and this will show you the list of all printers,  with their Port Name and location.

Get-WmiObject -Query " SELECT * FROM Win32_Printer" | Select Name, Default, PortName,Location

21-10-2013 13-53-47

I love WMI .

Happy learning . ;o) .

Tested on : Windows7 [64 bit]

Thanks
Aman Dhally
clip_image001 clip_image002 clip_image003 clip_image005  clip_image007

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