您可以在一个脚本块包起来,做好启动工作在foreach循环做以下的正向和反向查找:
$ComputerName= ‘computername here’
[System.Net.Dns]::GetHostAddresses(“$ComputerName”).IPAddressToString
$ComputerIPAddress = ‘that computer ip here'
[System.Net.Dns]::GetHostEntry($ComputerIPAddress).HostName
例如
$whateverlist = Get-Content .\yourlistofservers.txt
# or you can..
$whateverlist = @"
machine1
machine2
machine3
etc
"@
$Scriptblock = {
param($machine);
$pingOk = Test-Connection -cn $machine -BufferSize 16 -Count 1 -EA silentlyContinue
if ($pingOk)
{
# Do whatever if it responds to pinging
# Maybe store the property in a list, put it out to a file etc.
[System.Net.Dns]::GetHostAddresses(“$machine”).IPAddressToString
# Use whatever method you like to get IP of the computer, even use the above output.
# Me being lazy:
$ip = [System.Net.Dns]::GetHostAddresses(“$machine”).IPAddressToString
[System.Net.Dns]::GetHostEntry($ip).HostName
}
}
# Then you can get the job, do whatever. Do it in a foreach for best results.
foreach ($machine in $whateverlist)
{
Start-Job -ScriptBlock $Scriptblock -ArgumentList $machine
}
# To crack open the eggs and get the goodies:
Receive-Job * -Keep | Out-File ".\whatevermanijustworkhere.txt"
这里有一个干净的副本:
$whateverlist = Get-Content .\yourlistofservers.txt
$whateverlist = @"
machine1
machine2
machine3
etc
"@
$Scriptblock = {
param($machine);
$pingOk = Test-Connection -cn $machine -BufferSize 16 -Count 1 -EA silentlyContinue
if ($pingOk)
{
$ip = [System.Net.Dns]::GetHostAddresses(“$machine”).IPAddressToString
$ip
[System.Net.Dns]::GetHostEntry($ip).HostName
}
}
foreach ($machine in $whateverlist)
{
Start-Job -ScriptBlock $Scriptblock -ArgumentList $machine
}
Receive-Job * -Keep | Out-File ".\whatevermanijustworkhere.txt"
来源:
https://adsecurity.org/?p=305
我使用Invoke-Parallel来并行化动作https://gallery.technet.microsoft.com/scriptcenter/Run-Parallel-Parallel-377fd430 –
@MickyBalladelli'Invoke-Parallel -scriptfile c:\ work \ reverse.ps1 - inputobject $(get-content c:work \ test.txt)-runspacetimeout 10 -throttle 10'我正在使用这一行来尝试和运行测试。在reverse.ps1文件中的代码如下:'$ hostname = [System.Net.Dns] :: GetHostEntry($ _)。HostName $ results = @()foreach($ hostname中的$ host){if($主机名){$ results + = $ _ +“,”+ $ hostname} else {results + = $ _ +“,”+“No Hostname Found”}} $ result | out-file c:\ work \ Results.txt'输出为空。你看到我要去哪里吗? –
尝试使用'-ScriptBlock'参数代替脚本文件,这是我使用它的方式。 '-ImportVariables'参数对于让运行空间导入您定义的变量也很有意思。 –