Hello. There is a script code that checks all IPs for a given host. Example taken from the site . What does the error described in the topic mean? Specifically, the third line. Tell me how to fix it and what the lines are marked. The site, as you can see for yourself, is far from the most informative explanation.

$hostname = "microsoft.com" ???? $result = [system.Net.Dns]::GetHostByName($hostname) // я так понимаю, переменной result типа DNS присв.имя хоста? $result.AddressList | ForEach-Object {$_.IPAddressToString} // что это? $name='remotehelp.pp.ua','remoteadministrator.pp.ua','moderators.pp.ua', 'itadmin.pp.ua','remotehelp.org.ua','remoteadmin.org.ua','rootus.org.ua', 'rootsupport.org.ua','techroot.org.ua','techsupport.org.ua' // масив хостов $ns='ns1.server.biz' //что это? $i=0 while($i -lt $name.length) // что это? { nslookup $name[$i] $ns // что это? echo '--------------------------------------------------' $i++ } 
  • Copy the script from the site. It works without errors in my Powershall. In general, open CMD and write nslookup "microsoft.com" "8.8.8.8" and you will not have to build a garden in powershall without need. ( nslookup "адрес сайта" "адрес днс сервера" ) - Andrew B
  • @AndrewB, I would have done it, but I’m only told to familiarize myself with writing scripts and there is a source. Therefore, I ask. And thanks for your answer anyway, if I did it myself, I would use exactly your answer. - Muscled Boy
  • @AndrewB, the question may be stupid, but why 8.8.8.8. Why this particular IP? - Muscled Boy 1:51 pm
  • @MuscledBoy 8.8.8.8 - this is google google DNS server - Senior Pomidor
  • @MuscledBoy is not very clear where the error is caused, without the error error - Senior Pomidor

2 answers 2

Powershell works with cmdlets. Among cmdlets there are analogues of commands from the command line (CMD).

He command line cmd is markedly different as follows:

  • It is possible to transfer the results of running one cmdlet to others, filter and sort the results.
  • It is possible to call .Net Framework static functions.

About other differences google, if required.

Let us analyze your example script in rows (first part)

 $hostname = "microsoft.com" $result = [System.Net.Dns]::GetHostByName($hostname) $result.AddressList | ForEach-Object {$_.IPAddressToString} 
  1. Variable declaration
  2. Variable declaration (type IPHostEntry ). The function call .Net Framework System.Net.Dns.GetHostByName (string hostName) . This function returns the result of type System.Net.IPHostEntry
  3. We take from the result the value of AddressList . In fact, this is not a single value, but a list of System.Net.IPAddress values. Next, run the cmdlet, which iterates the list of these System.Net.IPAddress, converts each of them into a string and displays it in the console.

The second part of the script

 # или воспользоваться циклом если нужно проверить несколько хостов: # Массив доменных имен для проверки $name = 'remotehelp.pp.ua','remoteadministrator.pp.ua' # Сервер имен который нужно опросить # если нужно опросить системные ДНС, оставьте пустое значение $ns='ns1.server.biz' $i=0 while($i -lt $name.length) { nslookup $name[$i] $ns echo '--------------------------------------------------' $i++ } 
  1. Variable declaration (array of strings)
  2. Variable declaration
  3. Variable declaration
  4. A cycle from 0 to the number of lines minus 1 (because -lt means less, not less or equal) in the first variable (1). I shortened the list, so in this case the cycle will be executed 2 times
  5. Call the command line utility nslookup with the parameters "string from the list $name " and "DNS server address from the variable $ns ". This utility prints some result to the console.
  6. Output to the console text "----------"
  7. Increasing $i by 1 to select the next line in the $name list

The example of the script knocks out the rut by the fact that it mixes not only two examples, but also a different syntax (for the .Net Framework, for the cmdlet, for the command line utility).

I hope that the answer will help to understand Powershell.

  • I sincerely thank you. Your answer really made things easier. Truth be told, rarely anyone wants to write such a rich answer. Thanks again. Let me turn to you if there are such ambiguities? - Muscled Boy
  • If a new problem arises, create a new question, or make additions to an existing one (if it is not resolved). I will try to answer. - Andrew B
 $hostname = "microsoft.com" $result = [system.Net.Dns]::GetHostByName($hostname) # $result.AddressList - это массив объектов, который резолвит имя $hostname $result.AddressList | ForEach-Object {$_.IPAddressToString} # Проходим по этому массиву и на экран выводим только IP $name='remotehelp.pp.ua','remoteadministrator.pp.ua','moderators.pp.ua', 'itadmin.pp.ua','remotehelp.org.ua','remoteadmin.org.ua','rootus.org.ua', 'rootsupport.org.ua','techroot.org.ua','techsupport.org.ua' # ЭТО список хостов, просто массив $ns='ns1.server.biz' # DNS-server $i=0 while($i -lt $name.length) # итерация по списку хостов { nslookup $name[$i] $ns # just look up 'host' using 'server' echo '--------------------------------------------------' $i++ } 

sample loop output

 PS> nslookup 'remotehelp.pp.ua' 'ns1.server.biz' nslookup : *** Request to UnKnown timed-out DNS request timed out. timeout was 2 seconds. Server: UnKnown Address: 91.212.139.3 DNS request timed out. timeout was 2 seconds. DNS request timed out. timeout was 2 seconds. DNS request timed out. timeout was 2 seconds. DNS request timed out. timeout was 2 seconds. 
  • is not very clear where the error is caused, without the error error - Senior Pomidor
  • Thanks, it became a little clearer. By the way, the error disappeared - Muscled Boy