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

Register powershell snapin manually

I wanted to use the VMWare.vimautomation.core psssnapin in my scom discovery script.
But the snapin would not show up.
Earlier i had installed Powercli 6.0 and it does not have this snapin
So i uninstalled it and installed Powercli 5.5. But when the vcenter powershell was started it still showed 6.0 and the snapins would not show up.


 








 These are the steps I followed to manually register the snapin

As an example I will use the VMWare.VimAutomation.Core powershell snapin for .net 2.0.
Add the following registry key to your server.

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\PowerShellSnapIns\VMware.VimAutomation.Core]
"PowerShellVersion"="2.0"
"Vendor"="VMware Inc."
"Description"="This Windows PowerShell snap-in contains Windows PowerShell cmdlets for managing vSphere."
"Version"="5.5.0.0"
"ApplicationBase"="C:\\Program Files (x86)\\VMware\\Infrastructure\\vSphere PowerCLI"
"AssemblyName"="VMware.VimAutomation.ViCore.Cmdlets, Version=5.5.0.0, Culture=neutral, PublicKeyToken=null"
"ModuleName"="C:\\Program Files (x86)\\VMware\\Infrastructure\\vSphere PowerCLI\\VMware.VimAutomation.ViCore.Cmdlets.dll"
"CustomPSSnapInType"="VMware.VimAutomation.ViCore.Cmdlets.Vim4PSSnapIn"

Copy the assembly files in modulename for powercli 5.0 to location

C:\\Program Files (x86)\\VMware\\Infrastructure\\vSphere PowerCLI

Open powershell and run command Add-PSSnapin Vmware.vimautomation.Core