Showing posts with label powershell. Show all posts
Showing posts with label powershell. Show all posts

Tuesday, 21 June 2022

powershell script to test ping http ports and and RDP

 Function TestPingAndRDP ($IP)

{

Write-Host ***********************************************************

Write-Host Checking ping

Write-Host ***********************************************************


ping $IP


Write-Host $IP, "PING", ([System.Net.NetworkInformation.Ping]::new().Send($IP)).Status


Write-Host ***********************************************************

Write-Host Checking RDP

Write-Host ***********************************************************


Test-NetConnection -ComputerName $IP -CommonTCPport rdp

Write-Host ***********************************************************


}


Function PingOnly ($IPING)

{

Write-Host ***********************************************************

Write-Host Checking ping ONLY

Write-Host ***********************************************************


ping $IPING


Write-Host $IPING, "PING", ([System.Net.NetworkInformation.Ping]::new().Send($IPING)).Status

Write-Host ***********************************************************

}


Function TestWeb ($IWEB)

{

Write-Host ***********************************************************

Write-Host Checking web port 80

Write-Host ***********************************************************

Test-NetConnection -ComputerName $IWEB -CommonTCPport http


Write-Host ***********************************************************

Write-Host Checking web port 443

Write-Host ***********************************************************

Test-NetConnection -ComputerName $IWEB -Port 443

Write-Host ***********************************************************

}


Function TestHTTPCode ($url)

{

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

$date = Get-Date

Write-Host $url, "HTTP STATUS CODE" (Invoke-WebRequest -uri $url).StatusCode, $date

}


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

# Remove comment by removing the #, then the code will be run

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


#TestPingAndRDP 192.168.100.10

#PingOnly 192.168.100.50

#PingOnly 8.8.8.8

#PingOnly www.google.com

#TestWeb www.google.com

#TestHTTPCode www.google.com


Wednesday, 20 April 2022

powershell script to ping multiple ip addresses

Taken fromhttps://social.technet.microsoft.com/wiki/contents/articles/52396.powershell-ping-list-of-ip-addresses.aspx 

Note that we're using System.Net.NetworkInformation.Ping in these examples. However, you could also use the Test-Connection Jump PowerShell cmdlet or some other command maybe

script----------------------------------

Start-Transcript -Path .\log.txt

(Get-Content .\IPAddresses.txt) | ForEach {Write-Host $_, "-", ([System.Net.NetworkInformation.Ping]::new().Send($_)).Status}

Stop-Transcript

script----------------------------------


input file--------------------------------

1.1.1.1

8.8.8.8

8.8.4.4

input file--------------------------------

Thursday, 13 September 2012

powershell script to check the cluster volume storage usage

Another powershell script to check the disk usage on the CSV's

Import-Module FailoverClusters

#Function to send an email
function sendMail($body)
{
    $smtpServer = "192.168.1.25"
    $emailFrom = "ClusterStorageScript@email.ie" 
    $emailTo = ("alerts@email.com")
    $subject = "Cluster disk space usage"
    Send-MailMessage -From $emailFrom -To $emailTo -Subject $subject  -Body $body -SmtpServer $smtpServer
}


#Set up required arrays
$objs = @()
$alerts = @()

#code snippet to get cluster information
$csvs = Get-ClusterSharedVolume
foreach ( $csv in $csvs )
{
   $csvinfos = $csv | select -Property Name -ExpandProperty SharedVolumeInfo
   foreach ( $csvinfo in $csvinfos )
   {
      $obj = New-Object PSObject -Property @{
         Name        = $csv.Name
         Path        = $csvinfo.FriendlyVolumeName
         Size        = $csvinfo.Partition.Size
         FreeSpace   = $csvinfo.Partition.FreeSpace
         UsedSpace   = $csvinfo.Partition.UsedSpace
         PercentFree = $csvinfo.Partition.PercentFree
      }
      $objs += $obj
   }
#If there is less than 25% free add an alert to our alert array
#set sendalert to 1
   if ($csvinfo.Partition.PercentFree -le 25) 
   {
        $75alert =  "Warning 75% or more disk usage on " + $csv.Name + "`r`n"
        $alerts += $75alert
        $sendalert = 1
   }
#Similar as above adds alert for less than 20% free
   if ($csvinfo.Partition.PercentFree -le 20) 
   {
       $80alert =  "CRITICAL!!! 80% or more disk usage on " + $csv.Name + "`r`n"
       $alerts += $80alert
       $sendalert = 1
   }
     
}
#If send alert is set to 1 above then do the following
if ($sendalert -eq 1)
{
#Put all the alerts into a string $smsg
$a = 0
$smsg = ""
while ($a -le $alerts.count)
{
$smsg += $alerts[$a] + "`r`n"
$a++
}
#Put the full output of the cluster information into a string $msg
$objs2 = $objs | ft Name,@{ Label = "Size(GB)" ; Expression = { "{0,8:N2}" -f ($_.Size/1024/1024/1024) } },@{ Label = "FreeSpace(GB)" ; Expression = { "{0,13:N2}" -f ($_.FreeSpace/1024/1024/1024) } },@{ Label = "UsedSpace(GB)" ; Expression = { "{0,13:N2}" -f ($_.UsedSpace/1024/1024/1024) } },@{ Label = "PercentFree" ; Expression = { "{0,11:N2}" -f ($_.PercentFree) } }
$msg = out-string -inputobject $objs2 -width 85
#append new lings to $smsgm
$smsg += "`r`n`r`n"
#crate a new string comprised of the alerts and the full information
$emailmsg = $smsg + $msg
write-host $emailmsg
#email all of the information using the function at the top of this script
sendMail($emailmsg)

}

powershell script to check for hyper-v snapshots and email if they are found or not

Simple powershell script to check for hyper-v snapshots and email if they are found or not

function sendMail1($body)
{
    $smtpServer = "192.168.1.25"
    $emailFrom = "snapshots@email.ie" 
    $emailTo = ("alerts@email.com")
    $subject1 = "No .avhd files (snapshots) found"
    Send-MailMessage -From $emailFrom -To $emailTo -Subject $subject1  -Body $body -SmtpServer 

$smtpServer
}

function sendMail2($body)
{
    $smtpServer = "192.168.1.25"
    $emailFrom = "snapshots@email.ie" 
    $emailTo = ("alerts@email.com")
    $subject = "ALERT!! snapshots found - Urgent action required"
    Send-MailMessage -From $emailFrom -To $emailTo -Subject $subject -Body $body -SmtpServer 

$smtpServer
}
$output = Get-ChildItem C:\ClusterStorage\ * -include *.avhd -recurse

If ($output -eq $NULL)
{
#Do nothing
#$msg = "No .avhd files (snapshots) found"
#sendMail1($msg)
}
Else
{
#send the email
$msg = out-string -inputobject $output
sendMail2($msg)
}

Monday, 30 July 2012

exporting messages from exchange 2007 to a pst file

Make sure you have an exchange admin account, grant it rights on the mailbox you want to export
Add-MailboxPermission -Identity MailBoxToExport -User MyAdminAccount -AccessRights FullAccess

Make sure you have enough disk space to extract the data to
Export-Mailbox -Identity MailBoxToExport -IncludeFolders '\Folder to Export'  -PSTFolderPath C:\Users\jack.oregan\extracted-pst\extracted.pst

Monday, 23 July 2012

get information on a mailbox in exchange 2007

start the exchange powershell console

Get-Mailbox mailboxname | Select-Object name,primarysmtpaddress, DisplayName,Database,@{n="Size(MB)";e = {$MBXstat = Get-MailboxStatistics $_.name; $MBXstat.totalItemsize.value.toMB()}},@{n="Items"; e = {$MBXstat = Get-MailboxStatistics $_.name ; $MBXstat.itemcount}}

Monday, 9 July 2012

how to extract emails from the message queue in exchange 2007

Start the exchange management console
Mark the message as suspended in the queue
Collect the message properties so you have the queue and message ID
Start the exchange management shell

Export-Message -Identity EXCHSRV\5000\10000 -Path "C:\export\exported-email.eml"

5000 is the queue ID
10000 is the message ID

Export the entire queue (I have not tested this)
http://technet.microsoft.com/en-us/library/bb125215%28v=exchg.80%29

Monday, 2 July 2012

Wednesday, 6 June 2012

powershell cheat sheet

List the members of the Domain Admins group in AD
Get-ADGroupMember "Domain Admins" | select "SamAccountName" | Sort-Object SamAccountName

Friday, 18 May 2012

importing data from a csv file with powershell


This is the contents of my data.csv file:

Col1,Col2,Col3
jack,jack@test.com,1234567
john,john@test.com,7654321

Here is the powershell code of my ImportFromCSV.ps1:

$dataSource=import-csv "data.csv"
foreach($dataRecord in $datasource)
{
    $name=$dataRecord.Col1
    $email=$dataRecord.Col2
    $phone=$dataRecord.Col3
    write-host $name $email $phone
}

Running the code gives the following output:

jack jack@test.com 1234567
john john@test.com 7654321

For this to work data.csv and ImportFromCSV.ps1 should be in the same directory