Sunday, 30 July 2017

GET-Memory Useage function

Created a function to check the memory usage  on multiple servers



 Function Get-MemoryUsage ($ComputerName=$ENV:ComputerName) {
[cmdletbinding()]
$os = Get-Ciminstance Win32_OperatingSystem -ComputerName $ComputerName
$totalvisibleMemmorysize=$os.TotalvisibleMemmorysize
$pctFree = [math]::Round(($os.FreePhysicalMemory/$os.TotalVisibleMemorySize)*100,2)
$ser=$os.CSName

if ($pctFree -ge 45) {
$Status = "OK"
}
elseif ($pctFree -ge 15 ) {
$Status = "Warning"
}
else {
$Status = "Critical"
}

$os | Select @{Name = "Servername";Expression = {$ser}},
 @{Name = "TotalGB";Expression = {[int]($_.TotalVisibleMemorySize/1mb)}},
@{Name = "FreeGB";Expression = {[math]::Round($_.FreePhysicalMemory/1mb,2)}},
@{Name = "PctFree%"; Expression = {$pctFree}},@{Name = "Status";Expression = {$Status}} | Format-Table -AutoSize
}



Sunday, 2 July 2017

 Getting System Up time in Customized display

# Getting System Up time in Customized display
#Using this Script you can get the uptime of  multiple servers
#save the servers list in notepad with  out spaces.it will use to save the time if you want query the uptime of bulk servers.Please contact me for for any sugesstions and feedback





function get-uptime {
param(
$computername =$env:computername
)

$os = Get-WmiObject win32_operatingsystem -ComputerName $computername -ea silentlycontinue
if($os){
$lastbootuptime =$os.ConvertTodateTime($os.LastBootUpTime)

$LocalDateTime =$os.ConvertTodateTime($os.LocalDateTime)

$up =$LocalDateTime - $lastbootuptime

$uptime ="$($up.Days) days, $($up.Hours)h, $($up.Minutes)mins"

$results =new-object psobject

$results |Add-Member noteproperty LastBootUptime $LastBootuptime
$results |Add-Member noteproperty ComputerName $computername
$results |Add-Member noteproperty uptime $uptime


#Display the results

$results | Select-Object computername,LastBootuptime,Uptime

}


else

{

$results =New-Object psobject

$results =new-object psobject
$results |Add-Member noteproperty LastBootUptime "Na"
$results |Add-Member noteproperty ComputerName $computername
$results |Add-Member noteproperty uptime "Na"

#display the results

$results | Select-Object computername,LastBootUptime,Uptime




}



}

$infouptime =@()


foreach($allserver in $allservers){

$infouptime += get-uptime $allserver
}

$infouptime




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

#Getting Computers source


$servers =Get-Content C:\servers.txt

foreach ($server in $servers)

{

get-uptime -computername $server

}



Friday, 7 April 2017

simple try catch to find servers ping status

$servers =Get-Content -Path C:\servers.txt.txt

foreach ($server in $servers) {


try

{

Test-Connection -ComputerName $server -Count 2 -ErrorAction Stop | ft -AutoSize

}


catch

{
Write-Host $_.Exception.Message -ForegroundColor Green

}
}


How to find specific software version,name on remote servers

$servers =Get-Content -Path C:\servers.txt.txt

foreach ($server in $servers)

{
Get-WmiObject -Class win32_product  -ComputerName $server -Filter {Name like "%adobe%"} | Select-Object __SERVER,Name,Version | ft -AutoSize

}



Tuesday, 15 March 2016

How to automate and start w3svc service if it is in stopped stated and send mail

$a = "<style>"
$a = $a + "BODY{background-color:Green;}"
$a = $a + "TABLE{border-width: 2px;border-style: solid;border-color: black;border-collapse: collapse;}"
$a = $a + "TH{border-width: 2px;padding: 0px;border-style: solid;border-color: black;}"
$a = $a + "TD{border-width: 2;padding: 0px;border-style: solid;border-color: black;}"
$a = $a + "</style>"
$servers =Get-Content -Path C:\serverslist.txt

$servicestateok =Get-WmiObject -Class win32_service -ComputerName $servers | Where-Object {$_.name -like "w3svc"} | ?  {$_.state -match "Running"} | Select-Object __SERVER,DisplayName,StaTE | FT -AutoSize

$servicestatentok =Get-WmiObject -Class win32_service -ComputerName $servers | Where-Object {$_.name -like "w3svc"} | ?  {$_.state -match "stopped"} | Select-Object __SERVER,DisplayName,StaTE | FT -AutoSize


foreach ($server in $servers)

{}

if ($servicestateok)

{



 Write-Host "service is running no actvion is required"



}


else

{



{

Write-Host "service is not ok hence starting it"

Write-Host "currently working on $servers"



Start-Service -inputobject $(get-service -ComputerName $servers -Name w3svc)

Start-Sleep 15

$SER=Get-WmiObject -Class win32_service -ComputerName $servers | Where-Object {$_.name -like "w3svc"} | ?  {$_.state -match "Running"} | Select-Object __SERVER,DisplayName,State  | ConvertTo-Html -head $a


Send-MailMessage -to "administrator@superdc.com" -From "DG_ADMINS@superdc.com" -Subject "Service status"   -SmtpServer "dc.superdc.com" -BodyAsHtml "$SER"



}
}



Monday, 7 March 2016

How to run schedule task if a particular processes is running and monitor it until it closes and send mail

$SERVER1= "SERVER1"
$SERVER2="SERVER2"

write-host "Currently Working on Server $server1"

$procexist  = Get-WmiObject -Class win32_process -ComputerName $SERVER1 | where{$_.Processname -like "notepad.exe"} | Select-Object __Server,ProcessName,VM,WS

if($procexist){


schtasks /run /s "$server1" /TN servicecheck


for($i=0;$i -le 3$i++){

write-host "in Iteration Loop for the $i time on $SERVER1"
sleep 4

$procexist  = Get-WmiObject -Class win32_process -ComputerName $SERVER1| where{$_.Processname -like "notepad.exe"} | Select-Object __Server,ProcessName,VM,WS
if($procexist)

{

write-host "Still Process exist on  $SERVER1"


}


else{break}
}



}

else{

}


$procexist  = Get-WmiObject -Class win32_process -ComputerName $SERVER1| where{$_.Processname -like "notepad.exe"} | Select-Object __Server,ProcessName,VM,WS

if($procexist){
write-host "Still process exist  on Server $SERVER2"
write-host "Sending Mail and Exiting"
Send-MailMessage -to "administrator@superdc.com" -From "DG_ADMINS@superdc.com" -Subject "action required" -Body "Notepad is still running your attention is required on $server1" -SmtpServer "dc.superdc.com"
exit
}
else{

write-host "NOTEPAD IS NOT RUNNING"
write-host "Restarting Server $SERVER1"
#Restart-Computer -ComputerName $server -Force
}



write-host "Currently Working on Server $server2"

$procexist  = Get-WmiObject -Class win32_process -ComputerName $SERVER2 | where{$_.Processname -like "notepad.exe"} | Select-Object __Server,ProcessName,VM,WS

if($procexist){


schtasks /run /s "$server2" /TN servicecheck

Write-Host "notepad is running on $SERVER2"

Start-Sleep 10


for($i=0;$i -le 3$i++){

write-host "in Iteration Loop for the $i time on $SERVER2"
sleep 4

$procexist  = Get-WmiObject -Class win32_process -ComputerName $SERVER2| where{$_.Processname -like "notepad.exe"} | Select-Object __Server,ProcessName,VM,WS
if($procexist)

{

write-host "Still Process exist on  $SERVER2"


}


else{break}
}



}

else{

}


$procexist  = Get-WmiObject -Class win32_process -ComputerName $SERVER2| where{$_.Processname -like "notepad.exe"} | Select-Object __Server,ProcessName,VM,WS

if($procexist){
write-host "Still process exist  on Server $SERVER2"
write-host "Sending Mail and Exiting"
Send-MailMessage -to "administrator@superdc.com" -From "DG_ADMINS@superdc.com" -Subject "action required" -Body "Notepad is still running your attention is required on $server2" -SmtpServer "dc.superdc.com"
exit
}
else{

write-host "NOTEPAD IS NOT RUNNING"
write-host "Restarting Server $SERVER2"
#Restart-Computer -ComputerName $server -Force
}



Start-Sleep 20

Send-MailMessage -to "administrator@superdc.com" -From "DG_ADMINS@superdc.com" -Subject "OBI Reboots completed" -Body "Obi task has completed" -SmtpServer "dc.superdc.com"
exit


Sunday, 6 March 2016

OBI PROJECT

$servers = @("server1","Server2")

foreach($server in $servers){

write-host "Currently Working on Server $server"

$processexist= Get-WmiObject -Class win32_process -ComputerName $server| where{$_.Processname -like "notepad.exe"} | Select-Object __Server,ProcessName,VM,WS
if($processexist){


write-host "Process Exist and we are executing the schedule task on  $server"


schtasks /run /s "$server" /TN servicecheck


$procexist  = Get-WmiObject -Class win32_process -ComputerName $server| where{$_.Processname -like "notepad.exe"} | Select-Object __Server,ProcessName,VM,WS


write-host "Still process exist  on Server $server"

if($procexist){




for($i=1;$i -le 3;  $i++){

write-host "in Iteration Loop for the $i time on $server"
sleep 10

$procexist  = Get-WmiObject -Class win32_process -ComputerName $server| where{$_.Processname -like "notepad.exe"} | Select-Object __Server,ProcessName,VM,WS
if($procexist)

{

write-host "Still Process exist on  $server sending mail"





}

else{break}
}

}
else{

}



$procexist  = Get-WmiObject -Class win32_process -ComputerName $server| where{$_.Processname -like "notepad.exe"} | Select-Object __Server,ProcessName,VM,WS

if($procexist){
write-host "Still process exist  on Server $server"
write-host "Sending Mail and Exiting"
Send-MailMessage -to "administrator@superdc.com" -From "DG_ADMINS@superdc.com" -Subject "action required" -Body "Notepad is still running your attention is required on $server" -SmtpServer "dc.superdc.com"
exit
}
else{
write-host "Restarting Server $server"
Restart-Computer -ComputerName $server -Force
}



} else{

write-host "There is no process exist on "$server". Directly rebooting it."

Restart-Computer -ComputerName $server -Force

}


}

Wednesday, 24 February 2016

Testing purpose


$process= Get-Process -Name notepad -ComputerName server1

if($process)

{


schtasks /run /s dc.superdc.com /TN Service



}



else


{

Restart-Computer -Force -ComputerName server1

}





$process= Get-Process -Name notepad -ComputerName server2



if($process)

{


schtasks /run /s dc.superdc.com /TN Service



}



else


{

Restart-Computer -Force -ComputerName server2

}

Wednesday, 17 February 2016

Getting service status in HTML format and sending to mail

$a = "<style>"
$a = $a + "BODY{background-color:Green;}"
$a = $a + "TABLE{border-width: 2px;border-style: solid;border-color: black;border-collapse: collapse;}"
$a = $a + "TH{border-width: 2px;padding: 0px;border-style: solid;border-color: black;}"
$a = $a + "TD{border-width: 2;padding: 0px;border-style: solid;border-color: black;}"
$a = $a + "</style>"



$comp = Get-Content -Path C:\serverslist.txt

$m=foreach ($c in $comp)
{
Get-Service -ComputerName $comp | select-object machinename,Name,Status| Where-Object {$_.status -eq "running"} | ConvertTo-Html -head $a
}

Send-MailMessage -to "administrator@superdc.com"  -From "DG_ADMINS@superdc.com" -BodyAsHtml "$m" -Subject "Servicestatus" -SmtpServer "dc.superdc.com"

code in ISE

 I
output

Friday, 6 November 2015

How to shutdown Multilple computers using Powershell

$computers = Get-Content "C:\computers.txt"

$offline =@()

foreach ($computer in $computers)



{

if (Test-Connection $computer -Count 3 -ea 0) {

Stop-Computer -ComputerName $computers



}

else {

$offline+= "$computer not reachable"



}}

Powershell function to get theremote server IP details,Subnetmask,Gateway,DHCP Enabled status,DNS Servers,Wins and Macaddress

#Powershell function to get theremote server IP details,Subnetmask,Gateway,DHCP Enabled status,DNS Servers,Wins and Macaddress # use...