Feeds:
Posts
Comments

Archive for the ‘Powershell’ Category

Initial powershell Configuration

1.Open Powershell in windows Server With Local Administrator login

1pw

2pw

  2. enter Get-ExecutionPolicy on the prompt
   3. output restricted then change to Remote Signed Using Set-ExecutionPolicy RemoteSigned

3pw

4.paste the below code in notepad and save as <file name> with ps1 ext

###################################################################

function print-file($file) {

begin {

function internal-printfile($thefile) {

if ($thefile -is [string]) {$filename = $thefile }

else {

if ($thefile.FullName -is [string] ) { $filename = $THEfile.FullName }

}

$start = new-object System.Diagnostics.ProcessStartInfo $filename

$start.Verb = “print”

[System.Diagnostics.Process]::Start($start)

}

if ($file -ne $null) {

$filespecified = $true;

internal-printfile $file

}

}

process{if (!$filespecified) { write-Host process ; internal-printfile $_ } }

}

#dir *.pdf -r | print-file

$DestFileName=”E:\test1.pdf”     #PDF Path

print-file -file $DestFileName

###############################################################

5.Run the File from saved location in powershell

4pw

 

 

Note :- Which file type  want to print the machine have a viewer for a particular file type.

and default printer also configured on that machine.

 

Advertisements

Read Full Post »

#Put All SQL Instance Name in "C:\SQLInstances.txt" File
& {
#*****************************************************************************************
#Project Name   : SQL Server Check List
#Version        : 0.1
#Created By     : Ragu Thangavel
#Start Date     : 26-May-2011
#Modified By    : Ragu Thangavel
#Last Modified  : 27-May-2011
#*****************************************************************************************

# Excel Constants
# Microsoft.Office.Interop.Excel.XlRgbColor

$XlRgbColor = @{}
$XlRgbColor += @{"rgbAliceBlue" = "16775408"}		           # Alice Blue
$XlRgbColor += @{"rgbAntiqueWhite" = "14150650"}	           # Antique White
$XlRgbColor += @{"rgbAqua" = "16776960"}		               # Aqua
$XlRgbColor += @{"rgbAquamarine" = "13959039"}		           # Aquamarine
$XlRgbColor += @{"rgbAzure" = "16777200"}		               # Azure
$XlRgbColor += @{"rgbBeige" = "14480885"}		               # Beige
$XlRgbColor += @{"rgbBisque" = "12903679"}		               # Bisque
$XlRgbColor += @{"rgbBlack" = "0"}		                       # Black
$XlRgbColor += @{"rgbBlanchedAlmond" = "13495295"}	           # Blanched Almond
$XlRgbColor += @{"rgbBlue" = "16711680"}		               # Blue
$XlRgbColor += @{"rgbBlueViolet" = "14822282"}		           # Blue Violet
$XlRgbColor += @{"rgbBrown" = "2763429"}		               # Brown
$XlRgbColor += @{"rgbBurlyWood" = "8894686"}		           # Burly Wood
$XlRgbColor += @{"rgbCadetBlue" = "10526303"}		           # Cadet Blue
$XlRgbColor += @{"rgbChartreuse" = "65407"}		               # Chartreuse
$XlRgbColor += @{"rgbCoral" = "5275647"}		               # Coral
$XlRgbColor += @{"rgbCornflowerBlue" = "15570276"}             # Cornflower Blue
$XlRgbColor += @{"rgbCornsilk" = "14481663"}		           # Cornsilk
$XlRgbColor += @{"rgbCrimson" = "3937500"}		               # Crimson
$XlRgbColor += @{"rgbDarkBlue" = "9109504"}		               # Dark Blue
$XlRgbColor += @{"rgbDarkCyan" = "9145088"}		               # Dark Cyan
$XlRgbColor += @{"rgbDarkGoldenrod" = "755384"}		           # Dark Goldenrod
$XlRgbColor += @{"rgbDarkGray" = "11119017"}		           # Dark Gray
$XlRgbColor += @{"rgbDarkGreen" = "25600"}		               # Dark Green
$XlRgbColor += @{"rgbDarkGrey" = "11119017"}		           # Dark Grey
$XlRgbColor += @{"rgbDarkKhaki" = "7059389"}		           # Dark Khaki
$XlRgbColor += @{"rgbDarkMagenta" = "9109643"}		           # Dark Magenta
$XlRgbColor += @{"rgbDarkOliveGreen" = "3107669"}	           # Dark Olive Green
$XlRgbColor += @{"rgbDarkOrange" = "36095"}		               # Dark Orange
$XlRgbColor += @{"rgbDarkOrchid" = "13382297"}		           # Dark Orchid
$XlRgbColor += @{"rgbDarkRed" = "139"}		                   # Dark Red
$XlRgbColor += @{"rgbDarkSalmon" = "8034025"}		           # Dark Salmon
$XlRgbColor += @{"rgbDarkSeaGreen" = "9419919"}		           # Dark Sea Green
$XlRgbColor += @{"rgbDarkSlateBlue" = "9125192"}	           # Dark Slate Blue
$XlRgbColor += @{"rgbDarkSlateGray" = "5197615"}	           # Dark Slate Gray
$XlRgbColor += @{"rgbDarkSlateGrey" = "5197615"}	           # Dark Slate Grey
$XlRgbColor += @{"rgbDarkTurquoise" = "13749760"}	           # Dark Turquoise
$XlRgbColor += @{"rgbDarkViolet" = "13828244"}		           # Dark Violet
$XlRgbColor += @{"rgbDeepPink" = "9639167"}		               # Deep Pink
$XlRgbColor += @{"rgbDeepSkyBlue" = "16760576"}		           # Deep Sky Blue
$XlRgbColor += @{"rgbDimGray" = "6908265"}		               # Dim Gray
$XlRgbColor += @{"rgbDimGrey" = "6908265"}		               # Dim Grey
$XlRgbColor += @{"rgbDodgerBlue" = "16748574"}		           # Dodger Blue
$XlRgbColor += @{"rgbFireBrick" = "2237106"}		           # Fire Brick
$XlRgbColor += @{"rgbFloralWhite" = "15792895"}		           # Floral White
$XlRgbColor += @{"rgbForestGreen" = "2263842"}		           # Forest Green
$XlRgbColor += @{"rgbFuchsia" = "16711935"}		               # Fuchsia
$XlRgbColor += @{"rgbGainsboro" = "14474460"}		           # Gainsboro
$XlRgbColor += @{"rgbGhostWhite" = "16775416"}		           # Ghost White
$XlRgbColor += @{"rgbGold" = "55295"}		                   # Gold
$XlRgbColor += @{"rgbGoldenrod" = "2139610"}		           # Goldenrod
$XlRgbColor += @{"rgbGray" = "8421504"}		                   # Gray
$XlRgbColor += @{"rgbGreen" = "32768"}		                   # Green
$XlRgbColor += @{"rgbGreenYellow" = "3145645"}		           # Green Yellow
$XlRgbColor += @{"rgbGrey" = "8421504"}		                   # Grey
$XlRgbColor += @{"rgbHoneydew" = "15794160"}		           # Honeydew
$XlRgbColor += @{"rgbHotPink" = "11823615"}		               # Hot Pink
$XlRgbColor += @{"rgbIndianRed" = "6053069"}		           # Indian Red
$XlRgbColor += @{"rgbIndigo" = "8519755"}		               # Indigo
$XlRgbColor += @{"rgbIvory" = "15794175"}		               # Ivory
$XlRgbColor += @{"rgbKhaki" = "9234160"}		               # Khaki
$XlRgbColor += @{"rgbLavender" = "16443110"}		           # Lavender
$XlRgbColor += @{"rgbLavenderBlush" = "16118015"}	           # Lavender Blush
$XlRgbColor += @{"rgbLawnGreen" = "64636"}		               # Lawn Green
$XlRgbColor += @{"rgbLemonChiffon" = "13499135"}	           # Lemon Chiffon
$XlRgbColor += @{"rgbLightBlue" = "15128749"}		           # Light Blue
$XlRgbColor += @{"rgbLightCoral" = "8421616"}		           # Light Coral
$XlRgbColor += @{"rgbLightCyan" = "9145088"}		           # Light Cyan
$XlRgbColor += @{"rgbLightGoldenrodYellow" = "13826810"}	   # LightGoldenrodYellow
$XlRgbColor += @{"rgbLightGray" = "13882323"}		           # Light Gray
$XlRgbColor += @{"rgbLightGreen" = "9498256"}		           # Light Green
$XlRgbColor += @{"rgbLightGrey" = "13882323"}		           # Light Grey
$XlRgbColor += @{"rgbLightPink" = "12695295"}		           # Light Pink
$XlRgbColor += @{"rgbLightSalmon" = "8036607"}		           # Light Salmon
$XlRgbColor += @{"rgbLightSeaGreen" = "11186720"}		       # Light Sea Green
$XlRgbColor += @{"rgbLightSkyBlue" = "16436871"}		       # Light Sky Blue
$XlRgbColor += @{"rgbLightSlateGray" = "10061943"}		       # Light Slate Gray
$XlRgbColor += @{"rgbLightSteelBlue" = "14599344"}		       # Light Steel Blue
$XlRgbColor += @{"rgbLightYellow" = "14745599"}		           # Light Yellow
$XlRgbColor += @{"rgbLime" = "65280"}		                   # Lime
$XlRgbColor += @{"rgbLimeGreen" = "3329330"}		           # Lime Green
$XlRgbColor += @{"rgbLinen" = "15134970"}		               # Linen
$XlRgbColor += @{"rgbMaroon" = "128"}		                   # Maroon
$XlRgbColor += @{"rgbMediumAquamarine" = "11206502"}		   # Medium Aquamarine
$XlRgbColor += @{"rgbMediumBlue" = "13434880"}		           # Medium Blue
$XlRgbColor += @{"rgbMediumOrchid" = "13850042"}		       # Medium Orchid
$XlRgbColor += @{"rgbMediumPurple" = "14381203"}		       # Medium Purple
$XlRgbColor += @{"rgbMediumSeaGreen" = "7451452"}		       # Medium Sea Green
$XlRgbColor += @{"rgbMediumSlateBlue" = "15624315"}		       # Medium Slate Blue
$XlRgbColor += @{"rgbMediumSpringGreen" = "10156544"}		   # Medium Spring Green
$XlRgbColor += @{"rgbMediumTurquoise" = "13422920"}		       # Medium Turquoise
$XlRgbColor += @{"rgbMediumVioletRed" = "8721863"}		       # Medium Violet Red
$XlRgbColor += @{"rgbMidnightBlue" = "7346457"}		           # Midnight Blue
$XlRgbColor += @{"rgbMintCream" = "16449525"}		           # Mint Cream
$XlRgbColor += @{"rgbMistyRose" = "14804223"}		           # Misty Rose
$XlRgbColor += @{"rgbMoccasin" = "11920639"}		           # Moccasin
$XlRgbColor += @{"rgbNavajoWhite" = "11394815"}		           # Navajo White
$XlRgbColor += @{"rgbNavy" = "8388608"}		                   # Navy
$XlRgbColor += @{"rgbNavyBlue" = "8388608"}		               # Navy Blue
$XlRgbColor += @{"rgbOldLace" = "15136253"}		               # Old Lace
$XlRgbColor += @{"rgbOlive" = "32896"}		                   # Olive
$XlRgbColor += @{"rgbOliveDrab" = "2330219"}		           # Olive Drab
$XlRgbColor += @{"rgbOrange" = "42495"}		                   # Orange
$XlRgbColor += @{"rgbOrangeRed" = "17919"}		               # Orange Red
$XlRgbColor += @{"rgbOrchid" = "14053594"}		               # Orchid
$XlRgbColor += @{"rgbPaleGoldenrod" = "7071982"}		       # Pale Goldenrod
$XlRgbColor += @{"rgbPaleGreen" = "10025880"}		           # Pale Green
$XlRgbColor += @{"rgbPaleTurquoise" = "15658671"}		       # Pale Turquoise
$XlRgbColor += @{"rgbPaleVioletRed" = "9662683"}		       # Pale Violet Red
$XlRgbColor += @{"rgbPapayaWhip" = "14020607"}		           # Papaya Whip
$XlRgbColor += @{"rgbPeachPuff" = "12180223"}		           # Peach Puff
$XlRgbColor += @{"rgbPeru" = "4163021"}		                   # Peru
$XlRgbColor += @{"rgbPink" = "13353215"}		               # Pink
$XlRgbColor += @{"rgbPlum" = "14524637"}		               # Plum
$XlRgbColor += @{"rgbPowderBlue" = "15130800"}		           # Powder Blue
$XlRgbColor += @{"rgbPurple" = "8388736"}		               # Purple
$XlRgbColor += @{"rgbRed" = "255"}		                       # Red
$XlRgbColor += @{"rgbRosyBrown" = "9408444"}		           # Rosy Brown
$XlRgbColor += @{"rgbRoyalBlue" = "14772545"}		           # Royal Blue
$XlRgbColor += @{"rgbSalmon" = "7504122"}		               # Salmon
$XlRgbColor += @{"rgbSandyBrown" = "6333684"}		           # Sandy Brown
$XlRgbColor += @{"rgbSeaGreen" = "5737262"}		               # Sea Green
$XlRgbColor += @{"rgbSeashell" = "15660543"}		           # Seashell
$XlRgbColor += @{"rgbSienna" = "2970272"}		               # Sienna
$XlRgbColor += @{"rgbSilver" = "12632256"}		               # Silver
$XlRgbColor += @{"rgbSkyBlue" = "15453831"}		               # Sky Blue
$XlRgbColor += @{"rgbSlateBlue" = "13458026"}		           # Slate Blue
$XlRgbColor += @{"rgbSlateGray" = "9470064"}		           # Slate Gray
$XlRgbColor += @{"rgbSnow" = "16448255"}		               # Snow
$XlRgbColor += @{"rgbSpringGreen" = "8388352"}		           # Spring Green
$XlRgbColor += @{"rgbSteelBlue" = "11829830"}		           # Steel Blue
$XlRgbColor += @{"rgbTan" = "9221330"}		                   # Tan
$XlRgbColor += @{"rgbTeal" = "8421376"}		                   # Teal
$XlRgbColor += @{"rgbThistle" = "14204888"}		               # Thistle
$XlRgbColor += @{"rgbTomato" = "4678655"}		               # Tomato
$XlRgbColor += @{"rgbTurquoise" = "13688896"}		           # Turquoise
$XlRgbColor += @{"rgbViolet" = "15631086"}		               # Violet
$XlRgbColor += @{"rgbWheat" = "11788021"}		               # Wheat
$XlRgbColor += @{"rgbWhite" = "16777215"}		               # White
$XlRgbColor += @{"rgbWhiteSmoke" = "16119285"}		           # White Smoke
$XlRgbColor += @{"rgbYellow" = "65535"}		                   # Yellow
$XlRgbColor += @{"rgbYellowGreen" = "3329434"}		           # Yellow Green

# Microsoft.Office.Interop.Excel.XlLineStyle

$XlLineStyle = @{}
$XlLineStyle += @{"xlContinuous" = "1"}                        # Continuous line.
$XlLineStyle += @{"xlDash" = '-4115'}                          # Dashed line.
$XlLineStyle += @{"xlDashDot" = ' 4'}                          # Alternating dashes and dots.
$XlLineStyle += @{"xlDashDotDot" = '5'}                        # Dash followed by two dots.
$XlLineStyle += @{"xlDot" = '-4118'}                           # Dotted line.
$XlLineStyle += @{"xlDouble" = ' -4119'}                       # Double line.
$XlLineStyle += @{"xlLineStyleNone" = ' -4142'}                # No line.
$XlLineStyle += @{"xlSlantDashDot" = ' 13'}                    # Slanted dashes.

# Microsoft.Office.Interop.Excel.XlBorderWeight

$XlBorderWeight = @{}
$XlBorderWeight += @{"xlHairline" = "1"}
$XlBorderWeight += @{"xlMedium" = "-4138"}
$XlBorderWeight += @{"xlThick" = "4"}
$XlBorderWeight += @{"xlThin" = "2"}

# Microsoft.Office.Interop.Excel.XlDupeUnique

$XlDupeUnique = @{}
$XlDupeUnique += @{"xlUnique" = "0"}
$XlDupeUnique += @{"xlDuplicate" = "1"}

# Microsoft.Office.Interop.Excel.XlAutoFillType

$XlAutoFillType = @{}
$XlAutoFillType += @{"xlFillCopy" = "1"}
$XlAutoFillType += @{"xlFillDays" = "5"}
$XlAutoFillType += @{"xlFillDefault" = "0"}
$XlAutoFillType += @{"xlFillFormats" = "3"}
$XlAutoFillType += @{"xlFillMonths" = "7"}
$XlAutoFillType += @{"xlFillSeries" = "2"}
$XlAutoFillType += @{"xlFillValues" = "4"}
$XlAutoFillType += @{"xlFillWeekdays" = "6"}
$XlAutoFillType += @{"xlFillYears" = "8"}
$XlAutoFillType += @{"xlGrowthTrend" = "10"}
$XlAutoFillType += @{"xlLinearTrend" = "9"}

    Function Get_Xcel_Cell_Name ([Long] $intColNumber)

        {

            [Long]$intDiff = $null
            [Long]$intPlaces = $intColNumber
            [string] $strColName = $null

                while ($intPlaces -gt 0)

                    {

                        $intDiff = ($intPlaces - 1) % 26
                        $strColName = [char](65 + $intDiff) + $strColName
						$intPlaces = [Long](($intPlaces - $intDiff) / 26)

                    } 

            return $strColName

        }

    Function Xcel_Cell_Back_Style($ExcelRange,$BackColor)

        {

            $ExcelRange.Interior.Color = $BackColor

        }

    Function Xcel_Cell_Font_Style($ExcelRange,$FontName,$FontSize,$FontColor)

        {

            $ExcelRange.Font.Name = $FontName
            $ExcelRange.Font.Size = $FontSize
            $ExcelRange.Font.Color = $FontColor 

        }

    Function Xcel_Cell_Border_Style($ExcelRange,$BorderLineStyle,$BorderWeight,$BorderColor)

        {

            $ExcelRange.Borders.LineStyle = $BorderLineStyle
            $ExcelRange.Borders.Weight = $BorderWeight
            $ExcelRange.Borders.Color = $BorderColor

        }

$ExcelApp = New-Object -com Excel.Application
$ExcelApp.Visible = $True
$ExcelWorkBook = $ExcelApp.Workbooks.Add()
$ExcelWorkSheet = $ExcelWorkBook.Worksheets.Item(1)

[int]$rowIndex = 1
[int]$colIndex = 0

[String]$strFromCol=$null
[String]$strToCol=$null

    Foreach ($inst in get-content "C:\SQLInstances.txt")

        { 

            $con = "Server=$inst;Database=master;Integrated Security=sspi"
            $cmd = "Select SERVERPROPERTY('MachineName')"

            $da = new-object System.Data.SqlClient.SqlDataAdapter ($cmd, $con)
            $dt = new-object System.Data.DataTable
            $da.fill($dt) | Out-Null

            $ComputerName = $dt.Rows[0][0]

            #******************************************************************************************************************
			# Get Fixed Drive Information
            #******************************************************************************************************************

            $Drives = Get-wmiObject -class "Win32_LogicalDisk" -filter "DriveType = 3" -namespace "root\CIMV2" -computername $ComputerName

			$strFromCol = Get_XCel_Cell_Name 1
			$strToCol = Get_XCel_Cell_Name 5

            $Range = $ExcelWorkSheet.Range(($strFromCol) + $rowIndex + ':' + ($strToCol) + $rowIndex)
            $Range.Value2 = 'DRIVE','TOTAL SIZE (GB)','FREE SPACE (GB)','FREE SPACE (%)','FILE SYSTEM'

			Xcel_Cell_Back_Style $Range $XlRgbColor.rgbSlateGray
			Xcel_Cell_Border_Style $Range $XlLineStyle.xlContinuous $XlBorderWeight.xlThin $XlRgbColor.rgbLightGray
			Xcel_Cell_Font_Style $Range "Verdana" 8 $XlRgbColor.rgbPaleTurquoise

            $rowIndex +=1

            Foreach ($Drive in $Drives)

                {  

                    $Range = $ExcelWorkSheet.Range(($strFromCol) + $rowIndex + ':' + ($strToCol) + $rowIndex)
                    $Range.Value2 = $Drive.DeviceID , ($Drive.Size / 1GB) , ($Drive.FreeSpace / 1GB) , (($Drive.FreeSpace / 1GB) / (($Drive.Size / 1GB))*100) , $Drive.FileSystem 

                    Xcel_Cell_Back_Style $Range $XlRgbColor.rgbWhiteSmoke
                    Xcel_Cell_Border_Style $Range $XlLineStyle.xlContinuous $XlBorderWeight.xlThin $XlRgbColor.rgbLightGray
                    Xcel_Cell_Font_Style $Range "Verdana" 8 $XlRgbColor.rgbBlack

                    $rowIndex +=1

                }

            $rowIndex +=1

            #******************************************************************************************************************
	    # Get Database Information
            #******************************************************************************************************************            

	    $cmd ="Select Name as [DATABASE NAME],compatibility_level as [COMPATIBILITY LEVEL],recovery_model_desc as [RECOVERY MODEL],'' as [SIZE] From sys.databases"

            $da = new-object System.Data.SqlClient.SqlDataAdapter ($cmd, $con)
            $dt = new-object System.Data.DataTable
            $da.fill($dt) | Out-Null

            $strFromCol = Get_XCel_Cell_Name 1
			$strToCol = Get_XCel_Cell_Name 4

            $Range = $ExcelWorkSheet.Range(($strFromCol) + $rowIndex + ':' + ($strToCol) + $rowIndex)
            $Range.Value2 = 'DATABASE NAME','COMPATIBILITY LEVEL','RECOVERY MODEL','SIZE'

            Xcel_Cell_Back_Style $Range $XlRgbColor.rgbSlateGray
			Xcel_Cell_Border_Style $Range $XlLineStyle.xlContinuous $XlBorderWeight.xlThin $XlRgbColor.rgbLightGray
			Xcel_Cell_Font_Style $Range "Verdana" 8 $XlRgbColor.rgbPaleTurquoise

            $rowIndex += 1

            For($i = 0; $i -lt $dt.Rows.Count; $i++)

                {

                    $Range = $ExcelWorkSheet.Range(($strFromCol) + $rowIndex + ':' + ($strToCol) + $rowIndex)
                    $Range.Value2 = $dt.Rows[$i][0],$dt.Rows[$i][1],$dt.Rows[$i][2],$dt.Rows[$i][3]

                    Xcel_Cell_Back_Style $Range $XlRgbColor.rgbWhiteSmoke
                    Xcel_Cell_Border_Style $Range $XlLineStyle.xlContinuous $XlBorderWeight.xlThin $XlRgbColor.rgbLightGray
                    Xcel_Cell_Font_Style $Range "Verdana" 8 $XlRgbColor.rgbBlack

                    $rowIndex +=1

                } 

            $rowIndex += 2

        }

$ExcelWorkSheet.Cells.EntireColumn.AutoFit()

}

Read Full Post »