Thursday, 29 October 2015

SSRS Parameter Displays Date and Time


Another trick and tip to my blog!


If you want to show the parameters with data and time option both below is a quick way to do it.



Normally date/time parameters only display the date however if you add below expression now() then the parameter value shows current date and time.


If you want one date/time parameter to show a day before current date then use DateAdd option as below.

=DateAdd("d",-1,Now())


When you run the report final result will look like below.



Friday, 5 June 2015

Active Directory bulk photo upload using powershell

 
I found this blog which explains how to bulk upload Active Directory photo after resizing it to a certain limitation using PowerShell. This blog is very helpful and it really worked for me. It also explains how to do it, what are the constraints and most importantly the code. If you have an assignment to upload photos to Active Directory without using third party tool this blog provides you the solution. 

http://www.theagreeablecow.com/2013/08/automatically-re-size-and-import-photos.html

Monday, 11 May 2015

SSRS report throws rsErrorExecutingCommand?


When you run SSRS report in Visual Studio you may not come across this error but when you deploy to report server even though you give right permission and select correct data source your report is still giving above error message. If you get this error first check DB level permission.

in my example I have used a stored procedure in the report. I can run the report without any error in Visual Studio but when I deployed to the server I first could not figure out why this error was throwing. The I checked the DB level permission for the SSRS user. I noticed that I haven't given Execute permission to execute the stored procedure.

When you run the report in Visual Studio it uses permission for localhost but when you run it in report server you need to allocate the required permission to select, execute queries in that database.

Hope this helps. Happy reporting time!

Wednesday, 29 April 2015

How to delete Maintenance plan when you get error message “Exception has been thrown by the target of an invocation (mscorlib)


Use MSDB


Step 1:
Execute the below query to obtain the Maintenance plan name and Id
SELECT NAME, ID FROM MSDB..SYSMAINTPLAN_PLANS

Step 2:
Replace the Id obtained from Step 1 into the below query and delete the entry from log table
DELETE FROM SYSMAINTPLAN_LOG WHERE PLAN_ID=' '

Step 3:
Replace the Id obtained from Step 1 into the below query and delete the entry from subplans table as shown below,
DELETE FROM SYSMAINTPLAN_SUBPLANS WHERE PLAN_ID = ' '

Step 4:
Finally delete the maintenance plan using the below query where ID is obtained from Step1
DELETE FROM SYSMAINTPLAN_PLANS WHERE ID = ' '

Tuesday, 28 April 2015

Active Directory User Details in JSON Format

In the past I have posted series of Active Directory reading and writing operations. Below is a sample query that you can use to get an idea on how to read from an Active Directory group and output into JSON format. I used Group Principal to read from Active Directory Group. I have posted my extended class in one of my previous post which I reuse here as Extended Principal User to read Tel, Fax etc… Otherwise I use principal user to read from active directory. I have created a data table with the column names which I want to print and added Active Directory Group members into this data table.


To create JSON output I used JavaScriptSerializer. I created a new List type Dictionary and added Active Directory members details along with the header. To write into txt file I have used “WriteAllText” with Replace method to replace Unicode character “\u0026”  to “&”. Because any character may be encoded using the unicode escape in JSON and any valid JSON parsing library must be able to interpret those escapes. There are many workarounds available including regex. I have used Replace method in this example.

using System.Runtime.Serialization.Json;
using System.Web.Script.Serialization;
using Newtonsoft.Json.Linq;

       public void Read_ActiveDirectory(string filelocation,string ADgroupname)
        {
            string filepath = filelocation;
            string grpname = ADgroupname;
            ctx = new PrincipalContext(ContextType.Domain);
            group = GroupPrincipal.FindByIdentity(ctx, IdentityType.Name, grpname);
            exprUser = new ExtendedPrincipalUser(ctx);
            prinUser = new UserPrincipal(ctx);
            srch = new PrincipalSearcher(prinUser);

            DataSet dsADCloud = new DataSet("AD");
            DataTable dtUsers = new DataTable("Users");
            dtUsers.Columns.Add("DisplayName");
            dtUsers.Columns.Add("Surname");
            dtUsers.Columns.Add("First_Name");
            dtUsers.Columns.Add("JobTitle");
            dtUsers.Columns.Add("Position");
            dtUsers.Columns.Add("Phone");
            dtUsers.Columns.Add("Mobile");
            dtUsers.Columns.Add("EmailAddress");
            dtUsers.Columns.Add("Department");
            dtUsers.Constraints.Add("UniqueUserId", dtUsers.Columns["UserName"], true);

            if (group != null)
            {
                foreach (Principal p in group.GetMembers(true))
                {
                    prinUser = UserPrincipal.FindByIdentity(ctx, IdentityType.SamAccountName, p.SamAccountName);
                    exprUser = ExtendedPrincipalUser.FindByIdentity(ctx, IdentityType.SamAccountName, p.SamAccountName);
                    DataRow drUser = dtUsers.NewRow();
                    if (prinUser.Enabled != false)
                    {
                        try { drUser["DisplayName"] = prinUser.DisplayName.ToString().Trim();  }
                        catch { drUser["DisplayName"] = null; }
                        try { drUser["Surname"] = prinUser.Surname.ToString().Trim();  }
                        catch { drUser["Surname"] = null; }
                        try { drUser["First_Name"] = prinUser.GivenName.ToString().Trim(); }
                        catch { drUser["First_Name"] = null; }
                        try { drUser["JobTitle"] = prinUser.Description.ToString().Trim(); }
                        catch { drUser["JobTitle"] = null; }
                        try { drUser["Position"] = exprUser.jobTitle.ToString().Trim(); }
                        catch { drUser["Position"] = null; }
                        try {drUser["Phone"] = exprUser.VoiceTelephoneNumber.Trim();}
                        catch { drUser["Phone"] = null; }
                        try { drUser["Mobile"] =  exprUser.mobile.ToString().Trim() ; }
                        catch { drUser["Mobile"] = null; }
                        try { drUser["EmailAddress"] = prinUser.EmailAddress.ToString().Trim(); }
                        catch { drUser["EmailAddress"] = null; }
                        try { drUser["Department"] = exprUser.department.ToString().Trim(); }
                        catch { drUser["Department"] = null; }

                        dtUsers.Rows.Add(drUser);
                    }
                }
            }
            dsADCloud.Tables.Add(dtUsers);

            string strExcelPath = System.Configuration.ConfigurationManager.ConnectionStrings[filepath].ConnectionString;
            System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();

            List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>();    
                    Dictionary<string, object> row;


                    foreach (DataRow dr in dtUsers.Rows)
                    {
                        row = new Dictionary<string, object>();
                        foreach (DataColumn col in dtUsers.Columns)
                        {
                            row.Add(col.ColumnName, dr[col]);
                        }
                        rows.Add(row);
                    }

                    System.IO.File.WriteAllText(strExcelPath, serializer.Serialize(rows).ToString().Replace(@"\u0026", @"&"));
        }



Sample Output:

[{
"DisplayName":"ABC DEF","Surname":"DEF","First_Name":"ABC","JobTitle":"Manager","Position":"Manager","Phone":"123456789","Mobile":"147258369","EmailAddress":"ABCDEF@sampleemail.com","Department":"TEST DEPARTMENT 1"}, {"DisplayName":"GHI JKL","Surname":"JKL","First_Name":"GHI","JobTitle":"Coordinator","Position":"Coordinator","Phone":"012345678","Mobile":null,"EmailAddress":"GHI.JKL@sampleemail.com","Department":"TEST DEPARTMENT 2"}]

Tuesday, 14 April 2015

C# Extended class to read properties in Active Directory

Well I thought I have written this post, but it was always in my mind to do and realized I haven't done it yet. Time has come to share my extended class to retrieve properties from Active Directory not listed under User Principal. I have used Directory Services which is compatible with earlier versions of .Net  3.5. The reason is UserPrincipal class has limitations. Eg: If you want Fax number then easy way to do this is using directory services.

Below is my extended class snippet.

 
using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.DirectoryServices;

using System.DirectoryServices.AccountManagement;

namespace ExtPrincipalUser


{
[DirectoryRdnPrefix("CN")]

[DirectoryObjectClass("Person")]

public class ExtendedPrincipalUser : UserPrincipal

{
// Inplement the constructor using the base class constructor.

public ExtendedPrincipalUser(PrincipalContext ctx): base(ctx)

{

}
// Implement the constructor with initialization parameters.

public ExtendedPrincipalUser(PrincipalContext ctx, string samAccountName, string password, bool enabled)

: base(ctx, samAccountName, password, enabled)
{

}
[DirectoryProperty("facsimileTelephoneNumber")]

public string facsimileTelephoneNumber
{   get
   {
       if (ExtensionGet("facsimileTelephoneNumber").Length != 1)
       return null;
       return (string)ExtensionGet("facsimileTelephoneNumber")[0];
  }
set

  {
     this.ExtensionSet("facsimileTelephoneNumber", value);
  }
}

 
[DirectoryProperty("title")]

public string jobTitle


{
get


{
if (ExtensionGet("title").Length != 1)

return null;

return (string)ExtensionGet("title")[0];


}
set


{
this.ExtensionSet("title", value);


}

}
[DirectoryProperty("manager")]

public string manager


{
get


{
if (ExtensionGet("manager").Length != 1)

return string.Empty;

return (string)ExtensionGet("manager")[0];


}
set


{
this.ExtensionSet("manager", value);


}

}
[DirectoryProperty("facsimileTelephoneNumber")]

public string fax


{
get


{
if (ExtensionGet("facsimileTelephoneNumber").Length != 1)

return null;

return (string)ExtensionGet("facsimileTelephoneNumber")[0];


}
set


{
this.ExtensionSet("facsimileTelephoneNumber", value);


}

}

 
[DirectoryProperty("department")]

public string department


{
get


{
if (ExtensionGet("department").Length != 1)

return null;

return (string)ExtensionGet("department")[0];


}
set


{
this.ExtensionSet("department", value);


}

}
[DirectoryProperty("division")]

public string division


{
get


{
if (ExtensionGet("division").Length != 1)

return null;

return (string)ExtensionGet("division")[0];


}
set


{
this.ExtensionSet("division", value);


}

}
[DirectoryProperty("mobile")]

public string mobile


{
get


{
if (ExtensionGet("mobile").Length != 1)

return null;

return (string)ExtensionGet("mobile")[0];


}
set


{
this.ExtensionSet("mobile", value);


}

}
[DirectoryProperty("displayname")]

public string displayName


{
get


{
if (ExtensionGet("displayname").Length != 1)

return null;

return (string)ExtensionGet("displayname")[0];


}
set


{
this.ExtensionSet("displayname", value);


}

}
[DirectoryProperty("mail")]

public string mail


{
get


{
if (ExtensionGet("mail").Length != 1)

return null;

return (string)ExtensionGet("mail")[0];


}
set


{
this.ExtensionSet("mail", value);


}

}
// Implement the overloaded search method FindByIdentity.

public static new ExtendedPrincipalUser FindByIdentity(PrincipalContext context, string identityValue)


{
return (ExtendedPrincipalUser)FindByIdentityWithType(context, typeof(ExtendedPrincipalUser), identityValue);


}
// Implement the overloaded search method FindByIdentity.

public static new ExtendedPrincipalUser FindByIdentity(PrincipalContext context, IdentityType identityType, string identityValue)


{
return (ExtendedPrincipalUser)FindByIdentityWithType(context, typeof(ExtendedPrincipalUser), identityType, identityValue);


}
internal static ExtendedPrincipalUser FindByIdentity(PrincipalContext ctxMNGR)


{
throw new NotImplementedException();


}

}

}