Check Ping Status of Server.
Read server names from file and output to file.
$servername = Get-content E:\Utilities\Ping\Servers.txt
foreach ($Server in $ServerName) {
if (test-Connection -ComputerName $Server -Quiet )
{
write-Host "$Server is Pingable " -ForegroundColor Green
Add-Content E:\Utilities\Ping\Success.txt $Server
} else
{ Write-Host "$Server is Down" -ForegroundColor Red
Add-Content E:\Utilities\Ping\fail.txt $Server
}
}
Restart Windows Service in Multiple Servers
#Variable Definition
Write-host "Defining Variables"
$service= "Health Service"
$getservice= "HealthService"
Write-host "Variables Defined"
Function RestartAllAgentsService
{
$servicename = "HealthService"
$info = get-content "C:\Utilities\SCOM\RestartAgents.txt"
foreach($info in $info)
{
$info
Sleep 3
Write-host "Starting For Loop..."
Write-host "Checking Service status in $info"
$check = Get-service -name "HealthService" -Computername $info
$status= $check.status
Write-host "Health Service in $info is $status" -foreground cyan
Stop-service -inputobject $(get-service -name "HealthService" -ComputerName $info)
Write-host "Stopping service $info..." -foreground Yellow
Sleep 3
Write-host "Service Stopped $info" -foreground red
Sleep 3
Write-host "Starting service $info..." -foreground Yellow
Start-service -inputobject $(get-service -Name "HealthService" -ComputerName $info)
Write-host "Service Started $info" -foreground Green
}
}
RestartAllAgentsService
Check if Servers can connect to Internet
Here is a PowerShell which will let you know if your servers can connect to internet. You need to put all the servers in the server.txt file. Please make appropriate changes in the locations of files.
The code will also segregate the servers into different files.
You need to execute the main code.
PowerShell main code:
$server = Get-content "E:\Microsoft\SCOM\Utilities\InternetConnectivityv2\server.txt"
$error.clear()
foreach ($server in $server)
{
$error.clear()
$check = invoke-command -computername $server -filepath "E:\Microsoft\SCOM\Utilities\InternetConnectivityv2\Checkinternetconnection.ps1"
if($check)
{
write-host $server
add-content E:\Microsoft\SCOM\Utilities\InternetConnectivityv2\success.txt $server
}
Elseif($error -like "*Unable to connect to the remote server*")
{
Write-host $server
add-content E:\Microsoft\SCOM\Utilities\InternetConnectivityv2\Fail.txt $server
}
#elseif($error -like "*enied*")
#
# {
# add-content E:\Microsoft\SCOM\Utilities\InternetConnectivityv2\AccessDenied.txt $server
# }
#
else
{
add-content E:\Microsoft\SCOM\Utilities\InternetConnectivityv2\AccessDenied.txt $server,$error
}
}
Checkinternetconnection.ps1:
(New-Object Net.WebClient).DownloadString(‘http://www.bing.com’)
Check if Hosts Files has Manual Entries
Below is the script that will search for any manual entries done in the hosts files. Please make necessary changes in the file location paths.
$server = Get-content "E:\Microsoft\SCOM\Utilities\CheckHostFileEntriesv1.1\server.txt"
$path="Windows\System32\drivers\etc\hosts"
foreach ($server in $server)
{
$check = Test-Path "\\$server\C$\$path"
if($check)
{
$div = "-----------------------------------------------------------------------------------------------------------------------------------------------"
$delimiter = ","
$lines = Get-content "\\$server\C$\$path"
Write-host "File Found $server"
$manual = "No Manual Changes Present"
$lines | out-file "E:\Microsoft\SCOM\Utilities\CheckHostFileEntriesv1.1\temp.txt"
$output = Get-content "E:\Microsoft\SCOM\Utilities\CheckHostFileEntriesv1.1\temp.txt"
$finaloutput = Get-content "E:\Microsoft\SCOM\Utilities\CheckHostFileEntriesv1.1\temp.txt"| Where-Object { $_ }
$finaloutput | out-file "E:\Microsoft\SCOM\Utilities\CheckHostFileEntriesv1.1\temp.txt"
$finaloutput = Get-content "E:\Microsoft\SCOM\Utilities\CheckHostFileEntriesv1.1\temp.txt"| ? {$_ -notmatch '^\#'}
$output
if($finaloutput[1..($finaloutput.count - 1)])
{
foreach($finaloutput in $finaloutput)
{
$d=","
$Finaloutput = $server+$d+$finaloutput
Add-content "E:\Microsoft\SCOM\Utilities\CheckHostFileEntriesv1.1\output.csv" $finaloutput
}
}
else
{
Add-content "E:\Microsoft\SCOM\Utilities\CheckHostFileEntriesv1.1\Output.txt" $div
$d=","
$Finaloutputnull = $server+$d+$manual
Add-content "E:\Microsoft\SCOM\Utilities\CheckHostFileEntriesv1.1\output.csv" $finaloutputnull
}
}
else
{
write-host "Access Denied $server"
Add-content "E:\Microsoft\SCOM\Utilities\CheckHostFileEntriesv1.1\AccessDenied.txt" $server
}
}
Get PowerShell version, Windows version and Internet Connectivity of multiple servers
$filepath = "C:\Utilities\AllCheckInternetPSVersionWindowsVersion\server.txt"
$delimiter =","
$NA = "N/A"
add-content C:\Utilities\AllCheckInternetPSVersionWindowsVersion\output.csv "ServerName,PingAble, CheckInternet, PowerShellVersion, OSVersion"
if (Test-Path $filepath)
{
Write-host "File Found" -foregroundcolor "Green"
}
else
{
write-host "File not Found. Exiting from code" -foregroundcolor "Magenta"
exit;
}
$server = Get-content $filepath
foreach ($Server in $server)
{
If (test-Connection -ComputerName $Server -Count 2 -Quiet )
{
$pingtest = "Success"
}
else
{
$pingtest = "Fail"
}
If($pingtest -eq "Success")
{
$error.clear()
$check = invoke-command -computername $server -filepath "C:\Utilities\AllCheckInternetPSVersionWindowsVersion\Checkinternetconnection.ps1" -ErrorAction SilentlyContinue
if($check)
{
$internet = "Success"
}
Elseif($error -like "*Unable to connect to the remote server*")
{
$internet = "Fail"
}
else
{
$internet = "AccessDenied"
}
$output = $server + $delimiter + $pingtest + $delimiter + $internet
}
else
{
$output = $server + $delimiter + $pingtest + $delimiter + $NA
}
# add-content "C:\Utilities\AllCheckInternetPSVersionWindowsVersion\Output.txt" $output
$ver= Invoke-Command -Computername $server -Scriptblock {$PSVersionTable.psversion} |Select major|ft -hide -auto
#$ver = $ver.trimend();
$ver = $ver| Out-String -stream | Select-Object -Skip 1
$ver
if($error)
{
$div = "-----------------------------------------------------------------------------------------------------------------------------------------------"
$ac = "Access Denied"
$finaloutput = $output+$delimiter+$ac
}
else
{
$div = "-----------------------------------------------------------------------------------------------------------------------------------------------"
$finaloutput = $output+$delimiter+$ver
}
$osver = Invoke-Command -ComputerName $server -ScriptBlock{(Get-WmiObject -class Win32_OperatingSystem).Caption}
$outtofile = $finaloutput + $delimiter + $osver
add-content C:\Utilities\AllCheckInternetPSVersionWindowsVersion\Output.csv $outtofile
}
Checkinternetconnection.ps1
(New-Object Net.WebClient).DownloadString(‘http://www.bing.com’)
Read server names from file and output to file.
$servername = Get-content E:\Utilities\Ping\Servers.txt
foreach ($Server in $ServerName) {
if (test-Connection -ComputerName $Server -Quiet )
{
write-Host "$Server is Pingable " -ForegroundColor Green
Add-Content E:\Utilities\Ping\Success.txt $Server
} else
{ Write-Host "$Server is Down" -ForegroundColor Red
Add-Content E:\Utilities\Ping\fail.txt $Server
}
}
Restart Windows Service in Multiple Servers
#Variable Definition
Write-host "Defining Variables"
$service= "Health Service"
$getservice= "HealthService"
Write-host "Variables Defined"
Function RestartAllAgentsService
{
$servicename = "HealthService"
$info = get-content "C:\Utilities\SCOM\RestartAgents.txt"
foreach($info in $info)
{
$info
Sleep 3
Write-host "Starting For Loop..."
Write-host "Checking Service status in $info"
$check = Get-service -name "HealthService" -Computername $info
$status= $check.status
Write-host "Health Service in $info is $status" -foreground cyan
Stop-service -inputobject $(get-service -name "HealthService" -ComputerName $info)
Write-host "Stopping service $info..." -foreground Yellow
Sleep 3
Write-host "Service Stopped $info" -foreground red
Sleep 3
Write-host "Starting service $info..." -foreground Yellow
Start-service -inputobject $(get-service -Name "HealthService" -ComputerName $info)
Write-host "Service Started $info" -foreground Green
}
}
RestartAllAgentsService
Check if Servers can connect to Internet
Here is a PowerShell which will let you know if your servers can connect to internet. You need to put all the servers in the server.txt file. Please make appropriate changes in the locations of files.
The code will also segregate the servers into different files.
You need to execute the main code.
PowerShell main code:
$server = Get-content "E:\Microsoft\SCOM\Utilities\InternetConnectivityv2\server.txt"
$error.clear()
foreach ($server in $server)
{
$error.clear()
$check = invoke-command -computername $server -filepath "E:\Microsoft\SCOM\Utilities\InternetConnectivityv2\Checkinternetconnection.ps1"
if($check)
{
write-host $server
add-content E:\Microsoft\SCOM\Utilities\InternetConnectivityv2\success.txt $server
}
Elseif($error -like "*Unable to connect to the remote server*")
{
Write-host $server
add-content E:\Microsoft\SCOM\Utilities\InternetConnectivityv2\Fail.txt $server
}
#elseif($error -like "*enied*")
#
# {
# add-content E:\Microsoft\SCOM\Utilities\InternetConnectivityv2\AccessDenied.txt $server
# }
#
else
{
add-content E:\Microsoft\SCOM\Utilities\InternetConnectivityv2\AccessDenied.txt $server,$error
}
}
Checkinternetconnection.ps1:
(New-Object Net.WebClient).DownloadString(‘http://www.bing.com’)
Check if Hosts Files has Manual Entries
Below is the script that will search for any manual entries done in the hosts files. Please make necessary changes in the file location paths.
$server = Get-content "E:\Microsoft\SCOM\Utilities\CheckHostFileEntriesv1.1\server.txt"
$path="Windows\System32\drivers\etc\hosts"
foreach ($server in $server)
{
$check = Test-Path "\\$server\C$\$path"
if($check)
{
$div = "-----------------------------------------------------------------------------------------------------------------------------------------------"
$delimiter = ","
$lines = Get-content "\\$server\C$\$path"
Write-host "File Found $server"
$manual = "No Manual Changes Present"
$lines | out-file "E:\Microsoft\SCOM\Utilities\CheckHostFileEntriesv1.1\temp.txt"
$output = Get-content "E:\Microsoft\SCOM\Utilities\CheckHostFileEntriesv1.1\temp.txt"
$finaloutput = Get-content "E:\Microsoft\SCOM\Utilities\CheckHostFileEntriesv1.1\temp.txt"| Where-Object { $_ }
$finaloutput | out-file "E:\Microsoft\SCOM\Utilities\CheckHostFileEntriesv1.1\temp.txt"
$finaloutput = Get-content "E:\Microsoft\SCOM\Utilities\CheckHostFileEntriesv1.1\temp.txt"| ? {$_ -notmatch '^\#'}
$output
if($finaloutput[1..($finaloutput.count - 1)])
{
foreach($finaloutput in $finaloutput)
{
$d=","
$Finaloutput = $server+$d+$finaloutput
Add-content "E:\Microsoft\SCOM\Utilities\CheckHostFileEntriesv1.1\output.csv" $finaloutput
}
}
else
{
Add-content "E:\Microsoft\SCOM\Utilities\CheckHostFileEntriesv1.1\Output.txt" $div
$d=","
$Finaloutputnull = $server+$d+$manual
Add-content "E:\Microsoft\SCOM\Utilities\CheckHostFileEntriesv1.1\output.csv" $finaloutputnull
}
}
else
{
write-host "Access Denied $server"
Add-content "E:\Microsoft\SCOM\Utilities\CheckHostFileEntriesv1.1\AccessDenied.txt" $server
}
}
Get PowerShell version, Windows version and Internet Connectivity of multiple servers
$filepath = "C:\Utilities\AllCheckInternetPSVersionWindowsVersion\server.txt"
$delimiter =","
$NA = "N/A"
add-content C:\Utilities\AllCheckInternetPSVersionWindowsVersion\output.csv "ServerName,PingAble, CheckInternet, PowerShellVersion, OSVersion"
if (Test-Path $filepath)
{
Write-host "File Found" -foregroundcolor "Green"
}
else
{
write-host "File not Found. Exiting from code" -foregroundcolor "Magenta"
exit;
}
$server = Get-content $filepath
foreach ($Server in $server)
{
If (test-Connection -ComputerName $Server -Count 2 -Quiet )
{
$pingtest = "Success"
}
else
{
$pingtest = "Fail"
}
If($pingtest -eq "Success")
{
$error.clear()
$check = invoke-command -computername $server -filepath "C:\Utilities\AllCheckInternetPSVersionWindowsVersion\Checkinternetconnection.ps1" -ErrorAction SilentlyContinue
if($check)
{
$internet = "Success"
}
Elseif($error -like "*Unable to connect to the remote server*")
{
$internet = "Fail"
}
else
{
$internet = "AccessDenied"
}
$output = $server + $delimiter + $pingtest + $delimiter + $internet
}
else
{
$output = $server + $delimiter + $pingtest + $delimiter + $NA
}
# add-content "C:\Utilities\AllCheckInternetPSVersionWindowsVersion\Output.txt" $output
$ver= Invoke-Command -Computername $server -Scriptblock {$PSVersionTable.psversion} |Select major|ft -hide -auto
#$ver = $ver.trimend();
$ver = $ver| Out-String -stream | Select-Object -Skip 1
$ver
if($error)
{
$div = "-----------------------------------------------------------------------------------------------------------------------------------------------"
$ac = "Access Denied"
$finaloutput = $output+$delimiter+$ac
}
else
{
$div = "-----------------------------------------------------------------------------------------------------------------------------------------------"
$finaloutput = $output+$delimiter+$ver
}
$osver = Invoke-Command -ComputerName $server -ScriptBlock{(Get-WmiObject -class Win32_OperatingSystem).Caption}
$outtofile = $finaloutput + $delimiter + $osver
add-content C:\Utilities\AllCheckInternetPSVersionWindowsVersion\Output.csv $outtofile
}
Checkinternetconnection.ps1
(New-Object Net.WebClient).DownloadString(‘http://www.bing.com’)
No comments:
Post a Comment