Search This Blog


Monday, September 21, 2015

PowerShell - SessionState

SessionState is an object that reflects the current state of your PowerShell environment. You can find this object in the $ExecutionContext automatic variable:


Drive                         : System.Management.Automation.DriveManagementIntrinsics
Provider                    : System.Management.Automation.CmdletProviderManagementIntrinsics
Path                         : System.Management.Automation.PathIntrinsics
PSVariable                : System.Management.Automation.PSVariableIntrinsics
LanguageMode          : FullLanguage
UseFullLanguageModeInDebugger : False
Scripts                      : {*}
Applications              : {*}
Module                     :
InvokeProvider           : System.Management.Automation.ProviderIntrinsics
InvokeCommand       : System.Management.Automation.CommandInvocationIntrinsics

PSVariable sub-object will retrieve the value of any variable and can also be used to modify variables:
$value = "Test"

Lets retrieve variable contents by using PSVariable:

Similarly use Set method to modify variable contents:
$executioncontext.SessionState.PSVariable.Set("value", 100)


Remove method will simply remove the variable:
Now if you try to get the contents of variable you will get nothing:

You can list down all the available methods in PSVariable object by following cmdlet:
$executioncontext.SessionState.PSVariable | Get-Member -MemberType Methods

Drive subobject lets you manage drives in PowerShell. You could retrieve the current drive by Current property:

GetAll() lists all available drives and is equivalent to the Get-PSDrive cmdlet:

Thursday, September 10, 2015

MS SQL Server - Configure / Send Emails by Database Mail

This post with written by following SQL 2008 R2.

MS SQL Server have the feature to send emails. Let see how we can start using it:

There we have a Stored Prcocedure named msdb.dbo.sp_send_dbmail used to send emails. But if you call this SP straight away like:
EXEC msdb.dbo.sp_send_dbmail  
         @recipients    = ''
        ,@subject        = 'Test Subject'
        ,@body            = 'Test Body'
        ,@body_format    = 'HTML' 

Enable Database Mail XPs

After running the above stored procedure, you may get this error:
SQL Server blocked access to procedure 'dbo.sp_send_dbmail' of component 'Database Mail XPs' because this component is turned off as part of the security configuration for this server. A system administrator can enable the use of 'Database Mail XPs' by using sp_configure. For more information about enabling 'Database Mail XPs', see "Surface Area Configuration" in SQL Server Books Online.

This is because first you have to configure your server for sending emails. To fix this error, you can connect to SQL Server Instance with System Administrator (SA) Privileges and execute this script to enable:


--Enable 'Show Advanced Options'
SP_CONFIGURE 'show advanced options', 1

-- Enable Database Mail XPs Advanced Options in SQL Server */
SP_CONFIGURE 'Database Mail XPs', 1

--Diable 'Show Advanced Options'
SP_CONFIGURE 'show advanced options', 0

Now after this if you try to run the same Stored Procedure to send your email, this time you might get this error:
No global profile is configured. Specify a profile name in the @profile_name parameter.

Configure Database Mail Profile Account

If you have not already configured Database Mail Profile Account, follow these steps to create a new one:
  1. Go to SQL Server Object Explorer.
  2. Expand Management Node.
  3. Right click on Database Mail and select Configure Database Mail and proceed with the wizard.

  4. From Select Configuration Task screen, select Set up Database Mail by performing the following tasks.
  5. Put your Profile name and Description, and in SMTP accounts grid, select Add to set up a new SMTP account.
  6. A popup box will appear, click on New Account.
  7. Now  provide all details for your SMTP account setup. I have setup with my yahoo account. If you use yahoo mail service, you have to provide following details for yahoo:

Server name:
Port number: 587
And mark the checkbox for Secure Connection (SSL).


       Rest of the things you provide your email id with password.
       Click Next and Finish the wizard.

Now you have setup this new SMTP account, then second thing you have to do is to Enable Database Mail XPs as described below.

Configure a Default or Global Database Mail profile

If you have already configured Database Mail Profile Account then you have to follow these steps:
  1. Go to SQL Server Object Explorer.
  2. Expand Management Node.
  3. Right click on Database Mail and select Configure Database Mail and proceed with the wizard.
  4. From Select Configuration Task screen, select Manage Profile Secrurity.
  5. Now select your desired profile (mark CheckBox as Public) and make it Default Profile by select Yes.
  6. Click Next and Finish the wizard.

Now you can send emails from SQL Server.

If you find this post helpful or have any suggestion, please post your valuable comments.

Wednesday, September 9, 2015

AngularJs routing not working

I started learning AngularJS, and faced problem that routing is not getting worked. It only displays my index page and I was unable to display my partial views. Even it was not showing any error message while try to trace using firebug.

These are the points you have to consider if you get stuck with Angular Routes not working:

1- While going to define your config for routing, remember to put 'ngRoute' dependency parameter.
For example if following is your code snippet:

var myApp = angular.module('myApp');
        controller: 'myController',
        templateUrl: 'view1.html'
    //code goes on...

You have to change it by putting 'ngRoute' dependency.

var myApp = angular.module('myApp', ['ngRoute']);
        controller: 'myController',
        templateUrl: 'view1.html'
    //code goes on...

2- You may be started Angular JS demos by referencing angular.js library. Note that for routing get worked, we have to reference another library angular-route.js (

3- It should work now. If still not work, try to run your application by hosting in a webserver. For example try run it through Visual Studio

Tuesday, September 1, 2015

PowerShell - ScriptBlock

The ScriptBlock is a special form of command. It can contains multiple lines of code. It is defined by braces. You can put the call operator to execute a scriptblock: 
& { "Get date: " + (get-date) }

The call operator normally runs only a single commands. With script block you can run multiple lines of instructions since scriptblocks can consist of any number of commands.
& {Get-Process | Where-Object { $_.Name -like 'a*'}}

Similarly you can pass the same sort of command block to Invoke-Expression cmdlet, and you waill get the same results.
Invoke-Expression 'Get-Process | Where-Object { $_.Name -like "a*"}'

Just remember to put code after Invoke-Expression in single quotation marks. If you use double quotation marks, PowerShell will replace all the variable names in the string with the variable contents.

A scriptblock can also uses the param statement to define a parameter. You could easily define your own anonymous scriptblock with arguments. The following scriptblock accepts two parameters and multiplies them:
{ param($value1, $value2) $value1 * $value2 }

To invoke the scriptblock, use the call operator:
& { param($value1, $value2) $value1 * $value2 } 10 5

Monday, August 31, 2015

PowerShell - The Call Operator "&"

The call operator "&" is a powerful feature in PowerShell. If you place this operator in front of a string, or a string variable, the string will be interpreted as a command and executed just as if you had input it directly into the console. Let put a command (Dir) in a variable and if you output the contents of the variable, only string will be output:

$myCommand = "Dir"

If you type the call operator "&" in front of it, the command will be executed:
& $myCommand

The call operator will not run an entire instruction line but only single command. If you
had also specified arguments for the command, you will get an error:
$myCommand = "Dir C:\"
& $

Event if you put an space in command name, you will get same error. For example:
$myCommand = "Dir "
& $
 Also generates the error.

Another way to assign a command to a variable is using Get-Command cmdlet, and then use the same call operator to invoke this command.
$myCommand = Get-Command Dir
& $

But keep in mind that & $myCommand calls the actual command in $myCommand. You may specify any arguments after it, but you can't put the arguments directly in $command because the call operator always executes only one single command without arguments. So following call will work:
$myCommand = "Dir"
& $
myCommand "c:\"

Sunday, August 30, 2015

PowerShell - Workflow

Workflow functionality with PowerShell gives you the benefits of automation capabilities.  For example, you need to perform a long-running task that combines multiple steps in a sequence, wrokflows will do the job.

For creating and calling, it is same like a function, lets define our first workflow
workflow Workflow1 {
"My workflow is started"
"Task1 is completed"
"Task2 will take some time - 5 seconds"
Start-Sleep -Seconds 5
"Task3 completed"
"Workflow completed"

We can run the workflow just like any other PowerShell function. It looks and acts just like a normal function. Simply call workflow as:

Output of our workflow is:
My workflow is started
Task1 is completed
Task2 will take some time - 5 seconds
Task3 completed
Workflow completed

We can use the PowerShell Get-Command to get details about the workflow we created.
Get-Command Workflow1

Adding -syntax parameter, will display the syntax tempalte for calling workflow.
Workflow1 [<WorkflowCommonParameters>] [<CommonParameters>]

If you have a large no of activities within a workflow, and at some point we want to save a particular state of the workflow. For this we need to simply call this cmdlet within the workflow:

Workflow is an extensive topic in PowerShell. Here I created a simplest workflow just for starting purpose. Hopefully will try to post more on this topic as I get time. If you want to read more on this topic please tell me by your valuable comments, that will help for future posts.

PowerShell - Create / Import / Remove Module

You can write PowerShell script and save in separate file for reuse in different situations. Lets first create the module.

Create a file with extension .psm1, and place a sample function like this:

function Get-TestMessage()
    Write-Host("Get-TestMessage function is involed")

Now we have a TestModule.psm1 file with one function Get-TestMessage(). Place this module either in one of the PowerShell default module folders that you can query by this special variable $env:PSModulePath. Or also you can place anywhere on your disk, but in this case you have to define the complete file path for the module while importing.

Now, lets create another module, lets say WorkingModule.psm1. Here we want to use a function from the TestModule. We have to import TestModule by this cmdlet:
Import-Module "C:\PATH_TO_YOUR_MODULES_FOLDER\TestModulePSM.psm1"

And now we can simply call our function and it works.

You can find the available functions defined within a module by passing the module name in -module parameter for Get-Command cmdlet.
Get-Command -Module TestModule

In our case it will only return one function.

Once you finished work with a module, you can remove that module by simply call this cmdlet.
Remove-Module TestModulePSM

If this article helps you then please post your valuable comments.