Tuesday 20 January 2015

PowerShell Variables

Dealing with Variables in PowerShell.


Variables play a important role in any programming language and PowerShell is no exception for it.
Understanding Variables and playing with them is the key to essentially make your code more effective and helps us to present the code in more human readable format.

Here we would start with the basics of variable usage and declaration, calling and assigning stuff to them.

NOTE: I would keep this current line as note on all my other posts, which I would strongly recommend.Fire PowerShell Console and start typing along with me.

#Variables to store your stuff
#Assigning a variable


$FirstVar=33
${First Var}="PowerShell"

#To Get Output a variable from PS.

$MyVar
${My Var}
Write-Output $MyVar

#Strongly declare a  type of variable it would be.

#Here we tell the type of data we store in the respective variable.

[String]$MyName="VenuGopal"
 [int]$Number="123"

#Accepts only integer values below and we tried to add string and it would throw us error.
 [int]$Oops="Venu"


#Accepting value from console, storing and output the variable.
[string]$ComputerName=Read-host "Enter Computer Name"
Write-Output $ComputerName

#Assigning computer name to variable and calling that variable from a Commandlet.
$computerName="localhost"
Get-service -name bits -ComputerName "$ComputerName" |
    Select MachineName, Name, Status


Dealing with Quotes 

$ps="PowerShell"

# Mostly we use double quotes, it is used to display test along with variables that would be resolved later as per ther script together

"This is a variable $ps, and $ps is Fun!!"


#Single Quotes produce output as it is, the function, special stuff are simply ignored!

'This is a variable $ps, and $ps is Fun!!'

# the ` (the key on the tilt key above the tab would allow to ignore the next characters functionality if it had any.
#Typically the "/" "\" or some need where we have to nullify the force of some special character.. Soon you will get into this as we progress deeper :)


"This is a variable `$ps, and $ps is Fun!!"


#Output for the above three lines.

This is a variable PowerShell, and PowerShell is Fun!!
This is a variable $ps, and $ps is Fun!!
This is a variable $ps, and PowerShell is Fun!!

As said, we would gradually add more complex stuff as we progress.

Saturday 17 January 2015

6 Powershell Cmdlets to start with.

6 Powershell Commands to start with.



      Powershell is the new management tool of the Microsoft, they have been promoting PS in every aspect. With Powershell there are a lot many managements tasks that could be achieved much quicker and ability to work in much detailed manner which the GUI fails to provide. As a systems administrator we would have to be familiar with some important yet basic commands to get started with. Included is some of the one liners that would certainly speed up our day to day activities and ease server management activities.

I would like to start off with the basic commands the moment we login to server to check something..

1.Check service status. (Get-Service)

As a recommendation from my end. Launch Powershell as Administrator and start typing the commands as we see in this page.

Lists all the services.( observer the output, you would see Status, Name and DisplayName)

PS C:\>get-service



Find service with Name ( observer the output, you would see Status, Name and DisplayName), Here we are querying with Name )

PS C:\>get-service -name bits



Find service with Name ( observer the output, you would see Status, Name and DisplayName), Here we are querying with DisplayName.Using wildcards "*" for search)




2.Check Process status. (Get-Process)

This command is used to check the running processes on the system.

Lists all the processes running on the system
PS C:\get-process 

 

Find process with Name ( observer the output, you would see Status, Name and DisplayName), Here we are querying with Name )

PS C:\> get-process -name notepad


Sort process with highest utilization

PS C:\>Get-Process | Sort-Object WS -Descending



3. Get-Command

         The Get-Command is one of the most useful cmdlets in the whole of PowerShell, as it will help you getting to know PowerShell by letting you search for certain cmdlets. Using Get-Command alone would provide all the commands, so we have to ensure we provide some parameters for the cmdlet to search.


To get the cmdlets

PS C:\> get-command -CommandType cmdlet


4. Get-Help

          Once you have found the cmdlet you are looking for using Get-Command, you are going to want to know the syntax and how you can use that specific cmdlet. This is where Get-Help will do the rest.
Get-Help give indepth details of each cmdlet, its usage and syntax. In addition it also provides examples when '-examples' switch is used with it.


If you need help with examples for any command we need to add the switch -examples to the command.


 5.Start and Stop Service

       To stop service, issue cmdlet stop-service followed by the service name. 


         To start service, issue cmdlet start-service followed by the service name.


Note: get-service, stop-service and start-service accepts wild cards for searching the name of the service. So Don't try stop-service -name *. It would case new issues then we already have :) .


6.Stop-Process.

        Sometimes, a process will freeze up. When this happens, you can use the Get-Process command to get the name or the process ID for the process that has stopped responding. You can then terminate the process by using the Stop-Process command. You can terminate a process based on its name or on its process ID. For example, you could terminate Notepad by using one of the following commands:

PS C:\>Stop-Process -Name notepad
PS C:\>Stop-Process -ID 6088



  • Also with the above examples, we could get familiar with the type of errors we get in PowerShell.
  • We opened a notepad in step 1, checked its details in step 2, killed it using -name parameter in step 3.
  • We opened a notepad in step 4, checked its details in step 5, killed it using -ID parameter in step 6.

7.Set-ExecutionPolicy

Microsoft has disabled scripting by default in an effort to prevent malicious code from executing in a PowerShell environment.
You can use the Set-ExecutionPolicy command to control the security surrounding PowerShell scripts.
The options available:

  • Restricted — Restricted is the default execution policy and locks PowerShell down so that commands can be entered only interactively. PowerShell scripts are not allowed to run.
  • All Signed — If the execution policy is set to All Signed then scripts will be allowed to run, but only if they are signed by a trusted publisher.
  • Remote Signed — If the execution policy is set to Remote Signed, any PowerShell scripts that have been locally created will be allowed to run. Scripts created remotely are allowed to run only if they are signed by a trusted publisher.
  • Unrestricted — As the name implies, Unrestricted removes all restrictions from the execution policy.

Adding more.......

Friday 16 January 2015

PowerShell Help! It really helps! and with Examples!

PowerShell Help! It really helps! and with Examples!


The built in Help functionality in PowerShell is very systematically designed to act as your first point of contact if you need any help during your journey.

The Get-Help cmdlet displays information about Windows PowerShell concepts and commands, including cmdlets, functions, CIM commands, workflows, providers, aliases and scripts.

To get help for a Windows PowerShell command, type "Get-Help" followed by the command name, such as: Get-Help Get-Process. To get a list of all help topics on your system, type: Get-Help *. You can display the entire help topic or use the parameters of the Get-Help cmdlet to get selected parts of the topic, such as the syntax, parameters, or examples.

Get-Help gets the help content that it displays from help files on your computer. Without the help files, Get-Help displays only basic information about commands. Some Windows PowerShell modules come with help files. However, beginning in Windows PowerShell 3.0, the modules that come with Windows do not include help files. To download or update the help files for a module in Windows PowerShell 3.0, use the Update-Help cmdlet. 

Here I would give you some examples that demonstrate how useful the get-help command can be in PowerShell. Powershell commandlets are arranged in such a way that it almost allows you to "think what you want to do" and type "to get it". In simple terms its "think & type"



Some example based scenarios:


Get-Help with examples....



Get-help is very powerful cmdlet that can be your first step to try before you need any support from external sources.

Sunday 4 January 2015

First Time PowerShell user?

First Time PowerShell user?

    I hear a lot and infact felt isolated when I've launched the PowerShell console for the first time and I think the unix console windows might have scared us to core to exhibit this common behaviour for windows guys or we are GUI stuck! :)
 
How to overcome the First Time issues?
    Step 1
         Just launch it believing its the extended version of Windows Command Prompt with a different color theme and I would like to highlight this "PowerShell supports all native windows commands".
When you launch for the first time.... try the dir, cd , date, time, ping etc it works!

   Step 2.
        Pin the PowerShell icon to the 'taskbar' on your system and start working with it by slowly replacing the 'cmd.exe' to PowerShell and start with the basic commands in PowerShell.

   Step 3.
        This would allow you to explore the new powerful console and what it could offer you on the long run and avoid cmd.exe as much as possible.

Bottom line for this simple strategy is to allow you to get more familiar with PowerShell, and yay! it looks cool.

Some DOS to PS basics for you to try ..

Change a Directory
  • DOS: cd
  • PowerShell: Set-Location
List Files in a Directory
  • DOS: dir
  • PowerShell: Get-ChildItem
Rename a File:
  • DOS: rename
  • PowerShell: Rename-Item 
Find Date:

  • DOS : date
  • PowerShell: Get-Date

We would certainly get into depth of each command, but I would recommend you to observer the PowerShell commandlets carefully.

PowerShell commandlets follow a Verb-Noun format. Example (get-date : get-verb, date-noun)

Give it a try: Guess what would be the PowerShell commandlet for checking all the services on a system?

If your answer is (Get-Service) .. way to go! if not, still long way to go :)

Procedure to Pin PowerShell to Taskbar and How to launch PowerShell console.

Its important to launch PowerShell with "Elevated Privileges" on a system to avoid any issues while executing the commandlets.

Right Click on Windows PowerShell and select "Pin to Taskbar"







Then Right Click on the PowerShell Icon and Select "Run as Administrator". Just remember this should be the standard procedure of launching PowerShell to avoid any errors related to permissions.

















The PowerShell Console should always have "Administrator" on the Titlebar just to be sure you are Administrator and powershell is running with Administrator privileges.



Welcome to my Blog!

Hello Guys!


This Blog is targetted to System Administrators who are ready to learn PowerShell.

This Blog would get into details of each commandlet in PowerShell. Once the post is ready it would be published the link for that command would be added here in this home page.

I would try my best to keep it informative along with supporting blogs, articles and videos to follow along as we build this Blog!

Keep watching this space for updates.....

Post:1 [04-Jan-2015]
Brief on PowerShell

Regards,
Another PowerShell Guy!

Why new Scripting Language and why PowerShell?

My title
Why PowerShell? : Lets hear from the Microsoft:

 "It's safe to say that the single most important skill a Windows administrator will need in the coming years is proficiency with Windows PowerShell." Such a statement is hard to ignore isn't it?

         It's an eight year journey for me in the Information Technology and not even a single year is the same for the System Administrators out there including me.. It's quite common we would tend to learn new techniques every day to fix a problem or to simplify a task and most importantly we would rely Operations Management concepts and on the reporting and monitoring tools to give us a call in middle of night :) to fix a problem or to keep track of things. This has been a constant trend for sometime (just the tools and issues being different :) ).

        PowerShell started around 2006 when I started my career in IT Industry fixing desktops though, but over the period of time there was an idea, an idea that I always wanted to work and was at very primitive stage then, it's 'Automation'.

        Few years ago I had a chance to work with CFEngine which is more like the DSC in Powershell and I was thrilled to see what it could do to Servers, this had helped me to get into scripting with some level or seriousness.

       It brought something new to how we used to deal with things at work.The scripts not only made the work simpler but also provide re-usability, portability and Admin-friendly which is very much important and DSC opened a new window for Automation in Windows Technology.

      Personally I believe, scripting is important for any Systems Administrator out there and PowerShell is the Microsoft's answer to it for Windows platform and if you are Windows guy? It's never too late to have your share of PowerShell.

I've tried to put few highlights that should keep you going with PowerShell.



1: PowerShell is here to stay.
PowerShell modules would allow management of various Server Roles like AD, IIS, Exchange etc.,

2: New Microsoft products would leverage PowerShell.
Most of the Microsoft Products would eventually support PowerShell and the upcoming Products would be PowerShell integrated.

3: PowerShell usage is simpler than GUI.
Though Microsoft is known for their complex GUI, PowerShell would allow Management from console.

4: It can make your life easier
It’s re-usability, simplicity, remote management and portability would definitely make your life a lot easier when it comes to Management

5: You would see PowerShell questions in your MS certifications.
Yes, that’s right!!

6: As said Microsoft says it's important.
I would really like to re-iterate this. PowerShell is important, sooner or later everyone has to admit the fact as its designed for Administrators with our imputs.

7.Easier to pull reports and statistics.
 Exporting content to files is easier with PowerShell.


So, lets get started!!