Search

Scripts


$user = [Environment]::username



$a = new-object -comobject wscript.shell

$b = $a.popup("Hi $user . Welcome to Server Status check System",1,'Welcome')




function Invoke-SQLcmd

{

param(

$ServerInstance,

$Database,

$User,

$pw,

$Query

)



$Conn=New-Object System.Data.SQLClient.SQLConnection "Server=$ServerInstance;Database=$Database;Integrated Security=true";

$Conn.Open();

$DataCmd = New-Object System.Data.SqlClient.SqlCommand;

$DataCmd.CommandText = $Query;

$DataCmd.Connection = $Conn;

$DAadapter = New-Object System.Data.SqlClient.SqlDataAdapter;

$DAadapter.SelectCommand = $DataCmd;

$DTable = New-Object System.Data.DataTable;

$DAadapter.Fill($DTable)|Out-Null;

$Conn.Close();

$Conn.Dispose();

$DTable;

}






function sendmail()


{

param($EmailFrom,$EmailTo,$Subject,$SMTPServer,$port,$Username,$pass,$attachments)


$loc=$LocTB.Text

$mailconfigloc=$loc+'\mailconfig.xml'

[xml]$myXML = Get-Content $mailconfigloc

$EmailFrom=$myXML.mailconfig.from

$EmailTo=$myXML.mailconfig.to

$SMTPServer=$myXML.mailconfig.smtp

$port=$myXML.mailconfig.port

$Subject=$myXML.mailconfig.sub

$Username=$myXML.mailconfig.uname

$pass=$myXML.mailconfig.pwd


Write-Host '$pass'$pass

$Password = "$pass" |ConvertTo-SecureString

$Credentials = New-Object System.Management.Automation.PSCredential ($Username,$Password)


Send-MailMessage -From $EmailFrom -to $EmailTo -Body 'Mail from ServerCheckTool' -Subject $Subject -UseSsl -SmtpServer $SMTPServer -Port $port -Credential $Credentials -Attachments $attachments

}



function mailform()

{

param($location)


function sendmail()


{

param($EmailFrom,$EmailTo,$Subject,$SMTPServer,$port,$Username,$pass)



$Password = "$pass" |ConvertTo-SecureString

$Credentials = New-Object System.Management.Automation.PSCredential ($Username,$Password)

Send-MailMessage -From $EmailFrom -to $EmailTo -Body 'Mail from ServerCheckTool' -Subject $Subject -UseSsl -SmtpServer $SMTPServer -Port $port -Credential $Credentials


}


# Create a GUI Form

$Mailform = New-Object System.Windows.Forms.Form

$Mailform.Text = “Mail configuration Page”

$Mailform.Size = New-Object System.Drawing.Size(500,700)

$Mailform.StartPosition = “CenterScreen”



# Add a text box to the Location

$FromTB = New-Object System.Windows.Forms.TextBox

$FromTB.Location = New-Object System.Drawing.Size(150,50)

$FromTB.Size = New-Object System.Drawing.Size(200,20)

$Mailform.Controls.Add($FromTB)



# Add a text box to the Location

$FromLabel = New-Object System.Windows.Forms.Label

$FromLabel.Location = New-Object System.Drawing.Size(50,50)

$FromLabel.Size = New-Object System.Drawing.Size(200,20)

$FromLabel.Text='From email'

$Mailform.Controls.Add($FromLabel)


# Add a text box to the Location

$ToTB = New-Object System.Windows.Forms.TextBox

$ToTB.Location = New-Object System.Drawing.Size(150,100)

$ToTB.Size = New-Object System.Drawing.Size(200,20)

$Mailform.Controls.Add($ToTB)



# Add a text box to the Location

$ToLabel = New-Object System.Windows.Forms.Label

$ToLabel.Location = New-Object System.Drawing.Size(50,100)

$ToLabel.Size = New-Object System.Drawing.Size(200,20)

$ToLabel.Text='To email'

$Mailform.Controls.Add($ToLabel)


# Add a text box SMTP the Location

$SMTPTB = New-Object System.Windows.Forms.TextBox

$SMTPTB.Location = New-Object System.Drawing.Size(150,150)

$SMTPTB.Size = New-Object System.Drawing.Size(200,20)

$Mailform.Controls.Add($SMTPTB)



# Add a text box SMTP the Location

$SMTPLabel = New-Object System.Windows.Forms.Label

$SMTPLabel.Location = New-Object System.Drawing.Size(50,150)

$SMTPLabel.Size = New-Object System.Drawing.Size(200,20)

$SMTPLabel.Text='SMTP '

$Mailform.Controls.Add($SMTPLabel)


# Add a text box Subject the Location

$SubjectTB = New-Object System.Windows.Forms.TextBox

$SubjectTB.Location = New-Object System.Drawing.Size(150,200)

$SubjectTB.Size = New-Object System.Drawing.Size(200,20)

$Mailform.Controls.Add($SubjectTB)



# Add a text box Subject the Location

$SubjectLabel = New-Object System.Windows.Forms.Label

$SubjectLabel.Location = New-Object System.Drawing.Size(50,200)

$SubjectLabel.Size = New-Object System.Drawing.Size(200,20)

$SubjectLabel.Text='Subject '

$Mailform.Controls.Add($SubjectLabel)


# Add a text box port the Location

$portTB = New-Object System.Windows.Forms.TextBox

$portTB.Location = New-Object System.Drawing.Size(150,250)

$portTB.Size = New-Object System.Drawing.Size(200,20)

$Mailform.Controls.Add($portTB)



# Add a text box port the Location

$portLabel = New-Object System.Windows.Forms.Label

$portLabel.Location = New-Object System.Drawing.Size(50,250)

$portLabel.Size = New-Object System.Drawing.Size(200,20)

$portLabel.Text='Port No: '

$Mailform.Controls.Add($portLabel)


# Add a text box port the Location

$usernameTB = New-Object System.Windows.Forms.TextBox

$usernameTB.Location = New-Object System.Drawing.Size(150,300)

$usernameTB.Size = New-Object System.Drawing.Size(200,20)

$Mailform.Controls.Add($usernameTB)



# Add a text box port the Location

$usernameLabel = New-Object System.Windows.Forms.Label

$usernameLabel.Location = New-Object System.Drawing.Size(40,300)

$usernameLabel.Size = New-Object System.Drawing.Size(200,20)

$usernameLabel.Text='Email Username '

$Mailform.Controls.Add($usernameLabel)


# Add a text box port the Location

$passwordTB = New-Object System.Windows.Forms.TextBox

$passwordTB.Location = New-Object System.Drawing.Size(150,350)

$passwordTB.Size = New-Object System.Drawing.Size(200,20)

#$Mailform.Controls.Add($passwordTB)



# Add a text box port the Location

$passwordLabel = New-Object System.Windows.Forms.Label

$passwordLabel.Location = New-Object System.Drawing.Size(40,350)

$passwordLabel.Size = New-Object System.Drawing.Size(200,20)

$passwordLabel.Text='Email password '

#$Mailform.Controls.Add($passwordLabel)



# Add a text box port the Location

$Save = New-Object System.Windows.Forms.Button

$Save.Location = New-Object System.Drawing.Size(150,450)

$Save.Size = New-Object System.Drawing.Size(200,30)

$Save.Text='Save Config'

$Mailform.Controls.Add($Save)


# Add a text box port the Location

$testbutton = New-Object System.Windows.Forms.Button

$testbutton.Location = New-Object System.Drawing.Size(150,500)

$testbutton.Size = New-Object System.Drawing.Size(200,30)

$testbutton.Text='Send Test Email'

$Mailform.Controls.Add($testbutton)





# Add a text box port the Location

$SaveLabel = New-Object System.Windows.Forms.Label

$SaveLabel.Location = New-Object System.Drawing.Size(50,350)

$SaveLabel.Size = New-Object System.Drawing.Size(300,20)

$SaveLabel.ForeColor='Green'


$mailconfigloc="$location\mailconfig.xml"


[xml]$myXML = Get-Content $mailconfigloc

$FromTB.Text=$myXML.mailconfig.from

$ToTB.Text=$myXML.mailconfig.to

$SMTPTB.Text=$myXML.mailconfig.smtp

$SubjectTB.Text=$myXML.mailconfig.sub

$portTB.Text=$myXML.mailconfig.port

$usernameTB.Text=$myXML.mailconfig.uname

#$passwordTB.Text=$myXML.mailconfig.pwd


$Mailform.Controls.Add($SaveLabel)


$Save.Add_click({


$from=$FromTB.Text

$to=$ToTB.Text

$smtp=$SMTPTB.Text

$sub=$SubjectTB.Text

$port=$portTB.Text

$uname=$usernameTB.Text

#$pass=$passwordTB.Text


$getcred=Get-Credential -UserName $uname -Message 'Enter your email password'

$encryptedpassword= $getcred.password

$pass = $encryptedpassword | ConvertFrom-SecureString


$mailxml=@"

<mailconfig>

<from>$from</from>

<to>$to</to>

<smtp>$smtp</smtp>

<port>$port</port>

<sub>$sub</sub>

<uname>$uname</uname>

<pwd>$pass</pwd>

</mailconfig>

"@



if(!(Test-Path $mailconfigloc))

{

New-Item $mailconfigloc

$mailxml | Out-File $mailconfigloc

}

else


{

[xml]$myXML = Get-Content $mailconfigloc

$myXML.mailconfig.from=$from

$myXML.mailconfig.to=$to

$myXML.mailconfig.smtp=$smtp

$myXML.mailconfig.port=$port

$myXML.mailconfig.sub=$sub

$myXML.mailconfig.uname=$uname

$myXML.mailconfig.pwd="$pass"


$myXML.Save($mailconfigloc)

}


$SaveLabel.Text='Configurations saved successfully. '


})



$testbutton.Add_Click({

$from=$FromTB.Text

$to=$ToTB.Text

$smtp=$SMTPTB.Text

$sub=$SubjectTB.Text

$port=$portTB.Text

$uname=$usernameTB.Text


[xml]$myXML = Get-Content $mailconfigloc

$pass=$myXML.mailconfig.pwd



$Username = $uname

$Password = $pass |ConvertTo-SecureString

$Credentials = New-Object System.Management.Automation.PSCredential ($Username,$Password)


sendmail -emailfrom $from -emailto $to -subject $sub -Body 'Test mail' -smtpserver $smtp -port $port -Username $uname -pass $pass

$SaveLabel.Refresh()

$SaveLabel.Text='Mail queued. You will shortly receive the email. '

})



$Mailform.ShowDialog()

}






[void] [System.Reflection.Assembly]::LoadWithPartialName(“System.Drawing”)

[void] [System.Reflection.Assembly]::LoadWithPartialName(“System.Windows.Forms”)



$head = @'

<style>

body { background-color:#dddddd;

font-family:Tahoma;

font-size:12pt; }

td, th { border:1px solid black;

border-collapse:collapse; }

th { color:white;

background-color:black; }

table, tr, td, th { padding: 2px; margin: 0px }

table { margin-left:50px; }

</style>

'@


$timenow = Get-date


# Create functions for the functionalities

#=============================================================================


Function SQLInstancerunstatus {


param($loc)

# Defining output format for each column.

$fmtName =@{label="Service Name" ;alignment="left" ;width=30 ;Expression={$_.Name};};

$fmtMode =@{label="Start Mode" ;alignment="left" ;width=10 ;Expression={$_.StartMode};};

$fmtState =@{label="State" ;alignment="left" ;width=10 ;Expression={$_.State};};

$fmtStatus =@{label="Status" ;alignment="left" ;width=10 ;Expression={$_.Status};};

$fmtMsg =@{label="Message" ;alignment="left" ;width=50 ; `

Expression={ if (($_.StartMode -eq "Auto") -and ($_.State -ne "Running") ) {"Alarm: Stopped"} };};


#$colItems = Get-WmiObject Win32_BIOS -Namespace “root\CIMV2" -computername $strComputer

foreach($server in $strComputer)

{


$server+'details' >> $loc\inst.txt

'------------------------------' >> $loc\inst.txt

$srvc = Get-WmiObject `

-query "SELECT *

FROM win32_service

WHERE name LIKE '%SQL%'" `

-computername $server `

| Sort-Object -property name;

if($srvc -ne $null)

{

Write-Output ("Server: {0}" -f $server);

Write-Output $srvc | Format-Table $fmtName, $fmtMode, $fmtState, $fmtStatus, $fmtMsg;

$srvc | Format-Table $fmtName, $fmtMode, $fmtState, $fmtStatus, $fmtMsg >> $loc\inst.txt

}

else

{

"$server "+'Server does not exist OR not pingable OR network glitch' >> $loc\inst.txt

}