Friday, 7 April 2017
Tuesday, 5 July 2016
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"
}
}
$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
}
}
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

$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"
}}
$offline =@()
foreach ($computer in $computers)
{
if (Test-Connection $computer -Count 3 -ea 0) {
Stop-Computer -ComputerName $computers
}
else {
$offline+= "$computer not reachable"
}}
Wednesday, 29 July 2015
How to get Total installed memory on remote systems and export to csv
$computers= Get-Content -Path C:\servers.txt
$info=@()
foreach($computer in $computers){
$colItems = get-wmiobject -class "Win32_ComputerSystem" -namespace "root\CIMV2" -computername $computer
foreach ($objItem in $colItems){
$displayGB = [math]::round($objItem.TotalPhysicalMemory/1024/1024/1024, 0)
$res = "" | select Computer,Memory
$res.computer = $computer
$res.Memory = $displayGB
$info +=$res
}
}
$info | Export-Csv c:\12.csv -NoTypeInformation
Tuesday, 9 June 2015
Getting Disk space from Local system
If you want to get the list of disks and their use age use following script
get-wmiobject win32_logicaldisk -Filter "Drivetype=3" -ComputerName "localhost" | select systemName,deviceid,volumename,@{Name ="size(GB)";Expression={"{0:N1}" -f($_.size/1gb)}},
@{Name="Freespace(Gb)";Expression={"{0:N1}" -f($_.freespace/1gb)}} | Format-Table -AutoSize
Output will be
get-wmiobject win32_logicaldisk -Filter "Drivetype=3" -ComputerName "localhost" | select systemName,deviceid,volumename,@{Name ="size(GB)";Expression={"{0:N1}" -f($_.size/1gb)}},
@{Name="Freespace(Gb)";Expression={"{0:N1}" -f($_.freespace/1gb)}} | Format-Table -AutoSize
Output will be
Sunday, 20 April 2014
Save and clear Eventlogs
Hello friends
This time i am posting "How to save and clear Eventlogs"
if most of your production servers are running on VM (virtualmachines) the space on OS drive (C) may reach above threshhold in that case you need to
clear some space.we can clear temp files and compress some logs.apart doing
all these we can create a powershellscript to save and clear eventlogs.
$applog=Get-WmiObject win32_nteventlogfile -Filter "logfilename = 'application'"
$date=Get-Date -Format ("dd-MM-yyy-mm-ss")
$applog.backupeventlog("d:\Applicationlog_$date.evtx")
$applog=Get-WmiObject win32_nteventlogfile -Filter "logfilename ='system'"
$applog.backupeventlog("d:\systemlog_$date.evtx")
Clear-EventLog -Log application,system
This time i am posting "How to save and clear Eventlogs"
if most of your production servers are running on VM (virtualmachines) the space on OS drive (C) may reach above threshhold in that case you need to
clear some space.we can clear temp files and compress some logs.apart doing
all these we can create a powershellscript to save and clear eventlogs.
$applog=Get-WmiObject win32_nteventlogfile -Filter "logfilename = 'application'"
$date=Get-Date -Format ("dd-MM-yyy-mm-ss")
$applog.backupeventlog("d:\Applicationlog_$date.evtx")
$applog=Get-WmiObject win32_nteventlogfile -Filter "logfilename ='system'"
$applog.backupeventlog("d:\systemlog_$date.evtx")
Clear-EventLog -Log application,system
Subscribe to:
Posts (Atom)
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...
-
##########################################################################################################################################...
-
$date = get-date -f "dd/MM/yyy hh:mm:ss" #this is server list $servers = Get-Content -Path 'C:\servers.txt' $count =...