Showing posts with label script. Show all posts
Showing posts with label script. Show all posts

Friday, October 27, 2017

Powershell script to ping multiple servers at the same time.

Powershell script to ping multiple servers at the same time.
Warning: This script is resource intensive as it starts a powershell instance for each of the jobs. If you have multiple servers then it will take all up all the memory until it finishes all the jobs.

$scriptDir = Split-Path -Parent -Path $MyInvocation.MyCommand.Definition
$serverlist = GC "$scriptdir\list.txt"
$outputpath =  "$scriptdir\pingcheck.txt"



foreach($server in $serverlist)
{

  Start-job    -scriptblock {
                                                                         param([string]$computer,$outputpath)
                                                                         $out = Test-NetConnection -ComputerName $computer
                                                                         if($out.pingsucceeded -eq "True")
                                                                         {
                                                                         $output = "$computer" + "`t" + $out.remoteaddress + "`t" + " is pinging"
                                                                       
                                                                         }
                                                                         Else
                                                                         {
                                                                         $output = "$computer" + "`t" + $out.remoteaddress + "`t" + " is not pinging"
                                                                         Out-File -InputObject $output -FilePath $outputpath -Append
                                                                         }
                                                                         Sleep -Seconds 10
 return $output

                                                                   } -ArgumentList @($server,$outputpath)

 }

$A = Get-Job | Wait-Job | Receive-Job
Write-host "Writing A" -ForegroundColor Yellow
$A
Out-File -InputObject $A -FilePath $outputpath -Append 
Get-job | Remove-Job

Thursday, August 3, 2017

Regular expression checker powershell

Simple powershell script to check your regular expression strings.
The reference for creating a regular expression is here

http://www.regular-expressions.info/quickstart.html


PS C:\WINDOWS\system32> $String = 'This is my string'
$expression = [regex]"my"
$expression.Match($String)

Groups   : {my}
Success  : True
Captures : {my}
Index    : 8
Length   : 2
Value    : my


PS C:\WINDOWS\system32> $String = 'this is my string'
$expression = [regex]"[A-Za-z]"
$expression.Match($String)


Groups   : {t}
Success  : True
Captures : {t}
Index    : 0
Length   : 1
Value    : t



PS C:\WINDOWS\system32> $String = 'this is my string'
$expression = [regex]"[A-Z]"
$expression.Match($String)



Groups   : {}
Success  : False
Captures : {}
Index    : 0
Length   : 0
Value    : 

Tuesday, February 14, 2017

Scom Maintenance mode scheduler

#SCOM Maintenance mode scheduler. The assemblies can be found in this folder on your management servers  C:\Program Files\Microsoft System Center 2012 R2\Operations Manager\Server\SDK Binaries
$scriptdir = Get-location
$ScriptDir = split-path -parent $MyInvocation.MyCommand.Path
$void = [System.Reflection.Assembly]::LoadFile(“$scriptdir\Microsoft.EnterpriseManagement.OperationsManager.dll”)
$void3 = [System.Reflection.Assembly]::LoadFile(“$scriptdir\Microsoft.EnterpriseManagement.Runtime.dll”)
$void1 = [System.Reflection.Assembly]::LoadFile(“$scriptdir\Microsoft.EnterpriseManagement.Core.dll”)
$server = “testsql”
$RootMS = “testdc01”
$Minutes =  60
$Comment = “Scheduled Maintenance Request”
$MGConnSetting = New-Object Microsoft.EnterpriseManagement.ManagementGroupConnectionSettings($RootMS)
$MG = New-Object Microsoft.EnterpriseManagement.ManagementGroup($MGConnSetting)
$Admin = $MG.GetAdministration()
$Agents = $admin.GetAllAgentManagedComputers()
$agent = $Agents | ?{$_.Computername -match $server}
$monitoring =$MG.Monitoring
$AllClasses = $MG.GetMonitoringClasses()
$MyClass = $AllClasses | ?{$_.name -eq “Microsoft.Windows.Computer”}
$Objects = $Mg.GetMonitoringObjects($Myclass)
$Object = $Objects | ?{$_.name -match $server}
$Object.schedulemaintenancemode
$startTime = [Datetime]’12/21/2016 04:26:00′
$StartTimeUTC = $startTime.ToUniversalTime()
$EndTime = $startTimeUTC.AddMinutes(’10’)
$Object.ScheduleMaintenanceMode($startTimeUTC,$EndTime,”PlannedOther”,$Comment)