因为我想知道为什么我的脚本需要这么长时间我在谷歌搜索,也在这里stackoverflow。
但是我能找到任何有用的东西都是这里的,Powershell Script Running Slowly
因为我对Powershell还是新手,所以通过并接管我的脚本有点复杂,因为我不知道如何处理那些提到的事情,因为我以前从未听说过。
我的脚本非常简单,只要有东西可以返回回声,就给我一些信息。
我想“扫描”整个网络,所以我用本地网络IP制作了一个csv,并将它传递给Powershell以“Ping”那些。
但我意识到“没有响应”部分需要很长时间才能执行。
$list = Import-Csv -Path D:\ipcheck3.csv -UseCulture
$x=$list.IP
$ErrorActionPreference = "SilentlyContinue"
foreach ($y in $x)
{
try
{
if(Test-Connection $y -Count 1 -quiet)
{
write-host "$y responded"
$y | Export-Csv -Path D:\PingSucceded.csv -Append
}
else
{
Write-Host "$y was not responding"
$y | Export-Csv -Path D:\Pingfailed.csv -Append
}
}
catch
{
Write-Warning "Other Error occured"
}
}
不仅有Windows客户端,所以WMI不是一个选项,我不知道如何解决这个问题
编辑:
在工作流输入后,这是我的“试用版”
workflow Test-IPrange
{
Param
(
$IPs
)
$tocheck= $IPs.IP
foreach -parallel ($IP in $tocheck)
{
$pingsucceed = Test-Connection $IP -Count 1 -quiet
if($pingsucceed -eq "True")
{
$IP | Export-Csv -Path D:\testj.csv -Append
}
else
{
$IP | Export-Csv -Path D:\testn.csv -Append
}
}
}
Test-IPrange -IPs $(Import-Csv -Path D:\ipcheck3.csv -UseCulture)
我的工作流输出尝试
#TYPE System.String
PSComputerName,"PSShowComputerName","PSSourceJobInstanceId","Length"
localhost,"True","4e208e38-f7c2-492f-9d81-6583a103c3ac","12"
localhost,"True","4e208e38-f7c2-492f-9d81-6583a103c3ac","12"
在@Fourat的帮助下
我将我的代码编辑到此表单
Function Custom-Ping {
Param(
[string]$Address
)
$ping = ping $Address /w 1 /n 1
$result = ![string]::IsNullOrEmpty($ping -Like "*(0% Verlust)*")
return $result
}
$list = Import-Csv -Path D:\ipcheck3.csv -UseCulture
$x=$list.IP
$ErrorActionPreference = "SilentlyContinue"
foreach ($y in $x)
{
try
{
if(Custom-Ping $y)
{
Write-Host "$y responded"
$y | Export-Csv -Path D:\PingsuccededV3.csv -Append
}
else
{
Write-Host "$y was not responding"
$y | Export-Csv -Path D:\PingfailedV3.csv -Append
}
}
catch
{
Write-Warning "Textline from CMD Command or other Error"
}
}
哪个效果很好而且速度更快
我认为你的处理时间被超时所破坏。如果所有IP都在本地网络中,请尝试减少超时(因为默认值为5秒)。
如果你有Powershell 6:
Test-Connection $y -Count 1 -quiet -TimeoutSeconds 1
如果你不这样做,只需使用ping:
ping 58.47.45.1 /w 1 /n 1
您也可以为每个循环使用并行,但如果您有多个失败,它将无济于事:
ForEach -Parallel ($x in $y)
{
...
}
UPDATE
为了处理ping
结果,您可以使用这样的函数(我使用关键字'perte',因为我的计算机是法语):
Function Custom-Ping {
Param(
[string]$Address
)
$ping = ping $Address /w 1 /n 1
$result = ![string]::IsNullOrEmpty($ping -Like "*(perte 0%)*")
return $result
}
我用Workflow来解决这个问题。这是几年前我做到的,所以更好更新的东西在那里。但是这对我来说很有用......我在几分钟内ping了2000多台电脑......
workflow Test-ComputersConnection
{
Param
(
# Param1 help description
$Computernames#,
# Param2 help description
# [int]
# $Param2
)
foreach -parallel ($ComputerName in $Computernames)
{
$ConnectionTest = Test-Connection -ComputerName $ComputerName -ErrorAction SilentlyContinue -Count 1
if ($ConnectionTest.Address -eq $ComputerName) {
Write-Output $(Add-Member -MemberType NoteProperty -Name "Computername" -Value $ComputerName -InputObject $ConnectionTest -PassThru )
#Write-Verbose -Verbose -Message "[$($ComputerName)]: Replays on Ping."
}
Else {
#Write-Verbose -Verbose -Message "[$($ComputerName)]: Do not replays on Ping."
}
}
}
$OnlineNow0 = Test-ComputersConnection -Computernames $( Import-Csv -Path D:\ipcheck3.csv -UseCulture |
Select-Object -ExpandProperty name)
上面的代码是对我使用的内容的快速编辑...您需要首先编辑$(Import ...)语句,以确保PC名称正在传递给工作流程。
我只是在自己的电脑上测试,它给了我一个回复......