{"id":65,"date":"2017-06-18T15:29:55","date_gmt":"2017-06-18T19:29:55","guid":{"rendered":"http:\/\/ziviz.us\/WP\/?p=65"},"modified":"2017-06-18T15:29:55","modified_gmt":"2017-06-18T19:29:55","slug":"mtping","status":"publish","type":"post","link":"https:\/\/www.ziviz.net\/WP\/2017\/06\/18\/mtping\/","title":{"rendered":"MTPing"},"content":{"rendered":"<p style=\"text-align: center;\"><em>Code Release<\/em><\/p>\n<p>MTPing (or Multi-Threaded Ping) is a powershell script designed to quickly ping a large list of machines.<\/p>\n<h2>Parameters<\/h2>\n<h3>MachineList<\/h3>\n<p>This parameter should be a string that points to the location of a list of machines. The list should be formatted so that there is one machine per line.<br \/>\nExample:<\/p>\n<pre>192.168.1.1\r\n127.0.0.1\r\nwww.google.com\r\nmicrosoft.com\r\n::1<\/pre>\n<h3>LogLocation<\/h3>\n<p>LogLocation should be a string path representing where the log file should be saved. The log file will be written in a simple tabular format. The first column contains the machine-name or ip as retrieved from the machine list. The second column contains the ping result. &#8220;True&#8221; meaning connection successful.<br \/>\nExample:<\/p>\n<pre>192.168.1.1\tFalse\r\n127.0.0.1\tTrue\r\nwww.google.com\tTrue\r\nmicrosoft.com\tTrue\r\n::1\tTrue<\/pre>\n<h2>Usage<\/h2>\n<p>Inside of a powershell console enter the following:<\/p>\n<pre><code>&amp;\"\" -MachineList \"\" -LogLocation \"\"<\/code><\/pre>\n<p>Example:<\/p>\n<pre><code>&amp;\"C:\\Scripts\\MTPing.ps1\" -MachineList \"C:\\Scripts\\Machines.txt\" -LogLocation \"C:\\Logs\\PingResults.txt\"<\/code><\/pre>\n<h2>Script<\/h2>\n<pre><code>\r\nParam\r\n(\r\n    [Parameter(Mandatory=$True)]\r\n    $MachineList,\r\n    [Parameter(Mandatory=$True)]\r\n    $LogLocation\r\n)\r\n \r\n$ToProcess = @()\r\n$StreamReader = [System.IO.StreamReader] $MachineList\r\nwhile (-not $StreamReader.EndOfStream)\r\n{\r\n    $ToProcess+=$StreamReader.ReadLine();\r\n}\r\n$StreamReader.Close();\r\n \r\n$ScriptBlock = \r\n{\r\n    param\r\n    (\r\n        [PSObject]$InputObject\r\n    )\r\n    return @((Test-Connection $InputObject -quiet), $InputObject)\r\n}\r\n \r\n$Stream = [System.IO.StreamWriter] $LogLocation\r\n$Pool = [RunspaceFactory]::CreateRunspacePool(1, 10)\r\n$Pool.ApartmentState = \"STA\"\r\n$Pool.Open()\r\n$Pipes = @()\r\n \r\nforeach($Object in $ToProcess)\r\n{\r\n    $PipeLine = [System.Management.Automation.PowerShell]::create()\r\n    $PipeLine.RunspacePool = $Pool\r\n    $Mute=$PipeLine.AddScript($ScriptBlock).AddArgument($Object)\r\n    $Pipes+= @{\r\n                Pipe = $PipeLine\r\n                AsyncHandle = $PipeLine.BeginInvoke()\r\n              }\r\n}\r\n \r\n$Complete=$false;\r\n$Completed=0;\r\nwhile(-not $Complete)\r\n{\r\n    $Found = $false;\r\n    for ($Index=0; $Index -lt $Pipes.Count; $Index++)\r\n    {\r\n        if ($Pipes[$Index] -ne $null)\r\n        {\r\n            $Found=$true;\r\n            if ($Pipes[$Index].AsyncHandle.IsCompleted)\r\n            {\r\n                $Result = $Pipes[$Index].Pipe.EndInvoke($Pipes[$Index].AsyncHandle)\r\n                $Pipes[$Index].Pipe.Dispose()\r\n \r\n                if ($Result[0])\r\n                {\r\n                    Write-Host $Result[1] -ForegroundColor Green -NoNewline\r\n                    Write-Host \"`t\"($Completed\/$Pipes.Count*100)\"%\"\r\n                }\r\n                else\r\n                {\r\n                    Write-Host $Result[1] -ForegroundColor Red -NoNewline\r\n                    Write-Host \"`t\"($Completed\/$Pipes.Count*100)\"%\"\r\n \r\n                }\r\n \r\n                $Stream.WriteLine($Result[1]+\"`t\"+$Result[0].ToString());\r\n                $Pipes[$Index] = $null;\r\n                $Completed++;\r\n            }\r\n        }\r\n    }\r\n    if (-not $Found)\r\n    {\r\n        $Complete = $true;\r\n    }\r\n}\r\n$Stream.Close();\r\n$Pool.Close();\r\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Code Release MTPing (or Multi-Threaded Ping) is a powershell script designed to quickly ping a large list of machines. Parameters MachineList This parameter should be a string that points to the location of a list of machines. The list should be formatted so that there is one machine per line. Example: 192.168.1.1 127.0.0.1 www.google.com microsoft.com &hellip; <a href=\"https:\/\/www.ziviz.net\/WP\/2017\/06\/18\/mtping\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;MTPing&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,4,7],"tags":[],"class_list":["post-65","post","type-post","status-publish","format-standard","hentry","category-code-release","category-powershell","category-sysadmin"],"_links":{"self":[{"href":"https:\/\/www.ziviz.net\/WP\/wp-json\/wp\/v2\/posts\/65","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.ziviz.net\/WP\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ziviz.net\/WP\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ziviz.net\/WP\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ziviz.net\/WP\/wp-json\/wp\/v2\/comments?post=65"}],"version-history":[{"count":1,"href":"https:\/\/www.ziviz.net\/WP\/wp-json\/wp\/v2\/posts\/65\/revisions"}],"predecessor-version":[{"id":66,"href":"https:\/\/www.ziviz.net\/WP\/wp-json\/wp\/v2\/posts\/65\/revisions\/66"}],"wp:attachment":[{"href":"https:\/\/www.ziviz.net\/WP\/wp-json\/wp\/v2\/media?parent=65"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ziviz.net\/WP\/wp-json\/wp\/v2\/categories?post=65"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ziviz.net\/WP\/wp-json\/wp\/v2\/tags?post=65"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}