Category Archives: Code Snippets

Code Snippets SQL

MySQL DATE_FORMAT

DATE_FORMAT(date,format)

%a    Abbreviated weekday name (Sun-Sat)
%b    Abbreviated month name (Jan-Dec)
%c    Month, numeric (0-12)
%D    Day of month with English suffix (0th, 1st, 2nd, 3rd, ?)
%d    Day of month, numeric (00-31)
%e    Day of month, numeric (0-31)
%f    Microseconds (000000-999999)
%H    Hour (00-23)
%h    Hour (01-12)
%I    Hour (01-12)
%i    Minutes, numeric (00-59)
%j    Day of year (001-366)
%k    Hour (0-23)
%l    Hour (1-12)
%M    Month name (January-December)
%m    Month, numeric (00-12)
%p    AM or PM
%r    Time, 12-hour (hh:mm:ss followed by AM or PM)
%S    Seconds (00-59)
%s    Seconds (00-59)
%T    Time, 24-hour (hh:mm:ss)
%U    Week (00-53) where Sunday is the first day of week
%u    Week (00-53) where Monday is the first day of week
%V    Week (01-53) where Sunday is the first day of week, used with %X
%v    Week (01-53) where Monday is the first day of week, used with %x
%W    Weekday name (Sunday-Saturday)
%w    Day of the week (0=Sunday, 6=Saturday)
%X    Year for the week where Sunday is the first day of week, four digits, used with %V
%x    Year for the week where Monday is the first day of week, four digits, used with %v
%Y    Year, numeric, four digits
%y    Year, numeric, two digits

The following script uses the DATE_FORMAT() function to display different formats. We will use the NOW() function to get the current date/time:

DATE_FORMAT(NOW(),’%b %d %Y %h:%i %p’)
DATE_FORMAT(NOW(),’%m-%d-%Y’)
DATE_FORMAT(NOW(),’%d %b %y’)
DATE_FORMAT(NOW(),’%d %b %Y %T:%f’)

The result would look something like this:

Nov 04 2014 11:45 PM
11-04-2014
04 Nov 14
04 Nov 2014 11:45:34:243

Code Snippets Microsoft Networking Tutorials

How to find a Global Catalog server?

With DNS Requests (NSLOOKUP)

In an Active Directory environment, all Global Catalogs are anchored in DNS . There is a separate subdomain ‘GC._msdcs ….’ in the namespace of the AD root domain (please remember: the global catalog does not refer to individual domains, but to the entire forest). So if your root domain in the forest is e.g. example.root, then you get a list of all GCs with this command:

C:\> nslookup gc._msdcs.example.root

Server:  dns01.example.root
Address:  10.127.60.3

Name:  gc._msdcs.example.root
Adresses:  10.127.60.100
10.127.60.102
10.127.60.103
10.127.77.1
10.127.77.130
10.127.93.2
10.127.93.12
192.168.35.1

The container _msdcs contains the infrastructural DNS records of the Active Directory. This is also where all the SRVservice records for the domain controllers are stored.

With DSQUERY

You can also use the standard command line tool DSQUERY for searching GCs. The search can be limited to certain domains or AD sites. However, you must be authenticated in the regarding forest and DSQUERY must be available on your machine (this is usually the case on Widows servers). As a result, the server objects in the Configuration partition is displayed:

C:\> dsquery server -isgc

“CN=DC001,CN=Servers,CN=Site-Sidney,CN=Sites,CN=Configuration,DC=example,DC=root”
“CN=DC014,CN=Servers,CN=Site-Auckland,CN=Sites,CN=Configuration,DC=example,DC=root”

C:\> dsquery server -isgc -domain "dev.example.com"

C:\> dsquery server -isgc -site "Site-Auckland"

Per Script with an LDAP filter
In the last section we have seen that the global catalog servers are present in the configuration partition of the directory as specific objects. Her we can look for them with our own script. These servers have set the first bit in their ‘options’ attribute. All we need is the appropriate LDAP filter:

ldapFilter = "(&(objectClass=nTDSDSA)(options:1.2.840.113556.1.4.803:=1))"

Set rootDSE = GetObject("LDAP://rootDSE")
configDN =   rootDSE.Get("configurationNamingContext")

Set ado = CreateObject("ADODB.Connection")
ado.Provider = "ADSDSOObject"
ado.Open "ADSearch"
Set objectList =   ado.Execute("<LDAP://" &configDN& ">;" & ldapFilter & ";distinguishedName;subtree")

While Not objectList.EOF
nTSDSA = objectList.Fields("distinguishedName")
serverDN = Mid(nTSDSA, 18)                                         'CN=NTDS Settings abschneiden => Server Objekt
Set serverObj = GetObject("LDAP://" & serverDN )

WScript.Echo serverObj.dNSHostName

objectList.MoveNext
Wend

 

 

Code Snippets IIS wordpress

WordPress ISS Permalinks

Enabling Pretty Permalinks in WordPress

This walkthrough describes how to enable “Pretty Permalinks” for blog posts in the WordPress blog engine that is installed on IIS 7 and above. Typically, without URL rewriting functionality on a Web server, WordPress users must use “Almost Pretty” URLs, for example, http://contoso.com/index.php/yyyy/mm/dd/post-name/. By using the URL Rewrite module, you can use “Pretty Permalinks,” for example, http://example.com/year/month/day/post-name/, for WordPress blogs that are hosted on IIS.
Prerequisites

This walkthrough requires the following prerequisites:

IIS 7 or above with FastCGI and PHP installed. If you need to install PHP, follow the instructions in this article.
WordPress installed. If you need to install WordPress, follow the instructions in this article or use the instructions from the official WordPress site.
URL Rewrite installed.

Note that for the purposes of this walkthrough it is assumed that WordPress is installed in a Web site root directory. If WordPress is installed in a subdirectory, then the rewrite rules that are used in this walkthrough should be included in the Web.config file that is located within the same subdirectory where the WordPress files are.
Enabling Pretty Permalinks in WordPress

Use the following instructions to create pretty permalinks for your blog posts.

To enable pretty permalinks in Word Press:

Log on to WordPress with Administrator user rights.
In WordPress, click the Options tab.
On the Options page, click the Permalinks subtab.
This will take you to the page where you can customize how WordPress generates permalinks for blog posts.
On the Permalinks page, select Custom, specify below and enter “/%year%/%monthnum%/%day%/%postname%/” in the Custom structure text box.
Click Update Permalink Structure.

All the blog post links will have URLs that follow the format that you have specified, but if you click any one of those links the Web server will return a 404 – File Not Found error. This is because WordPress relies on a URL rewriting capability within the server to rewrite requests that have “pretty permalinks” to an Index.php file. In the next section, you will create a rule that will provide this capability.
Creating a Rewrite Rule

Open the Web.config file that is located in the same directory where the WordPress files are installed, and paste the following XML section into the system.webServer element:

<rewrite>
<rules>
<rule name="Main Rule" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="index.php" />
</rule>
</rules>
</rewrite>

This rule will try to match any requested URL. If the URL does not correspond to a file or a folder on the file system, it will rewrite the URL to the Index.php file. At that point, WordPress will determine which content to serve based on the REQUEST_URI server variable that contains the original URL before it was modified by this rule.
Testing the Rewrite Rule

After you save the rewrite rule to the Web.config file, open a Web browser and click any one of the permalinks in your WordPress blog. You should see the correct content returned by the Web server for each permalink.

Summary

In this walkthrough you learned how to use the URL Rewrite module to enable “pretty permalinks” in the WordPress blog engine. WordPress is just one example of the many popular PHP applications that can take advantage of the URL Rewrite module in IIS, a feature that enables user-friendly and search engine-friendly URLs.

 

 

By Ruslan Yakushev

http://www.iis.net/learn/extensions/url-rewrite-module/enabling-pretty-permalinks-in-wordpress

Code Snippets MySQL SQL

Mysql Proper Case

    DROP FUNCTION IF EXISTS proper;
    SET GLOBAL  log_bin_trust_function_creators=TRUE;
    DELIMITER |
    CREATE FUNCTION proper( str VARCHAR(128) )
    RETURNS VARCHAR(128)
    BEGIN
      DECLARE c CHAR(1);
      DECLARE s VARCHAR(128);
      DECLARE i INT DEFAULT 1;
      DECLARE bool INT DEFAULT 1;
      DECLARE punct CHAR(17) DEFAULT ' ()[]{},.-_!@;:?/';
      SET s = LCASE( str );
      WHILE i < LENGTH( str ) DO 
        BEGIN
          SET c = SUBSTRING( s, i, 1 );
          IF LOCATE( c, punct ) > 0 THEN
            SET bool = 1;
          ELSEIF bool=1 THEN 
            BEGIN
              IF c >= 'a' AND c <= 'z' THEN 
                BEGIN
                  SET s = CONCAT(LEFT(s,i-1),UCASE(c),SUBSTRING(s,i+1));
                  SET bool = 0;
                END;
              ELSEIF c >= '0' AND c <= '9' THEN
                SET bool = 0;
              END IF;
            END;
          END IF;
          SET i = i+1;
        END;
      END WHILE;
      RETURN s;
    END;
    |
    DELIMITER ;
Code Snippets

Migradoc vertical text


        Dim myTextFrame As TextFrame = Section1.AddTextFrame
        myTextFrame.Orientation = TextOrientation.Upward
        myTextFrame.Width = 10
        myTextFramew.Top = 0
        myTextFrame.Left = -14
        myTextFrame.Height = 600
   
        Dim myP As Paragraph = myTextFrame.AddParagraph
        myP.Format.Alignment = ParagraphAlignment.Center
        myP.Format.Font.Size = 8
        myP.AddText("text vertical")

Code Snippets

CKEditor

CKEditor is a ready-for-use HTML text editor designed to simplify web content creation. It’s a WYSIWYG editor that brings common word processor features directly to your web pages. Enhance your website experience with our community maintained editor

http://ckeditor.com

Code Snippets .NET ASP.Net regex

ASP.Net Character Length Regular Expression Validators

ASP.Net RegularExpression Validators

Maximum character length Validation (Maximum 6 characters allowed)

<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:RegularExpressionValidator runat="server" ID="RegularExpressionValidator1"
Display = "Dynamic"
ControlToValidate = "TextBox1"
ValidationExpression = "^[\s\S]{0,6}$"
ErrorMessage="Maximum 6 characters allowed.">
</asp:RegularExpressionValidator>

Minimum character length Validation (Minimum 6 characters required)

<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:RegularExpressionValidator runat="server" ID="RegularExpressionValidator2"
Display = "Dynamic"
ControlToValidate = "TextBox2"
ValidationExpression = "^[\s\S]{6,}$"
ErrorMessage="Minimum 6 characters required.">
</asp:RegularExpressionValidator>

Minimum and Maximum character length Validation (Minimum 2 and Maximum 6 characters required)

<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
<asp:RegularExpressionValidator runat="server" ID="RegularExpressionValidator3"
Display = "Dynamic"
ControlToValidate = "TextBox3"
ValidationExpression = "^[\s\S]{2,6}$"
ErrorMessage="Minimum 2 and Maximum 6 characters required.">
</asp:RegularExpressionValidator>

Alphanumeric with special Characters
Minimum length 7 and Maximum length 10. Characters allowed – a – z A – Z 0-9 ’@ & # .

<asp:RegularExpressionValidator ID="RegExp1" runat="server"
ErrorMessage="Password length must be between 7 to 10 characters"
ControlToValidate=" txtPassword "
ValidationExpression="^[a-zA-Z0-9'@&#.\s]{7,10}$" />

Alphanumeric Only
Minimum length 7 and Maximum length 10. Characters allowed – a – z A – Z 0-9

<asp:RegularExpressionValidator ID="RegExp1" runat="server"
ErrorMessage="Password length must be between 7 to 10 characters"
ControlToValidate=" txtPassword "
ValidationExpression="^[a-zA-Z0-9\s]{7,10}$" />

Alphabets Only
Minimum length 7 and Maximum length 10. Characters allowed – a – z A – Z

<asp:RegularExpressionValidator ID="RegExp1" runat="server"
ErrorMessage="Password length must be between 7 to 10 characters"
ControlToValidate="txtPassword"
ValidationExpression="^[a-zA-Z]{7,10}$" />

Numeric Only
Minimum length 7 and Maximum length 10. Characters allowed – 0 – 9

<asp:RegularExpressionValidator ID="RegExp1" runat="server"
ErrorMessage="Password length must be between 7 to 10 characters"
ControlToValidate="txtPassword"
ValidationExpression="^[0-9]{7,10}$" />

source:
http://www.aspsnippets.com/Articles/ASP.Net-Regular-Expression-Validator-to-validate-Minimum-and-Maximum-Text-Length.aspx

Code Snippets SQL

Detect invalid(sintax) emails

Detect invalid(sintax) emails in db

SELECT * FROM people WHERE email NOT LIKE '%_@__%.__%'
Code Snippets SQL

Query to list number of records in each table in a database

sql server 2005



SELECT 
    t.NAME AS TableName,
    s.Name AS SchemaName,
    p.rows AS RowCounts,
    SUM(a.total_pages) * 8 AS TotalSpaceKB, 
    SUM(a.used_pages) * 8 AS UsedSpaceKB, 
    (SUM(a.total_pages) - SUM(a.used_pages)) * 8 AS UnusedSpaceKB
FROM 
    sys.tables t
INNER JOIN      
    sys.indexes i ON t.OBJECT_ID = i.object_id
INNER JOIN 
    sys.partitions p ON i.object_id = p.OBJECT_ID AND i.index_id = p.index_id
INNER JOIN 
    sys.allocation_units a ON p.partition_id = a.container_id
LEFT OUTER JOIN 
    sys.schemas s ON t.schema_id = s.schema_id
WHERE 
    t.NAME NOT LIKE 'dt%' 
    AND t.is_ms_shipped = 0
    AND i.OBJECT_ID > 255 
GROUP BY 
    t.Name, s.Name, p.Rows
ORDER BY 
    t.Name





Code Snippets Exchange Microsoft PowerShell

Export to CSV all your Exchange 2007/2010 Email Addresses

If you want to export all your users email addresses to a CSV so you can see who has which alias you can do this with this Power Shell Command


Get-Mailbox -ResultSize Unlimited
|Select-Object DisplayName,ServerName,PrimarySmtpAddress, @{Name=“EmailAddresses”;Expression={$_.EmailAddresses
|Where-Object {$_.PrefixString -ceq “smtp”}
| ForEach-Object {$_.SmtpAddress}}}
| Export-CSV c:\smtp.csv -NoTypeInformation

I have wrapped it here so just paste it in to Power Shell to run and this will show the Primary Email address and then all the alias addresses.

PowerShell Get-Mailbox display SMTP addresses


http://cscmblog.blogspot.pt/2012/02/export-to-csv-all-your-exchange.html