Site icon WinCert

How to get an IP address from a server list script

<p>Here&&num;8217&semi;s how you can get the list of IP addresses&comma; FQDN and Ping status with a simple Powershell server list script&period;<&sol;p>&NewLine;<p>For a start create a servers&period;txt file in C&colon;&bsol;temp folder with the list of servers you would like to resolve&period; You need to put one server per line&period; You also need to have Microsoft Excel installed on the system since the result will be exported to &period;csv file&period;<&sol;p>&NewLine;<p><strong>C&colon;&bsol;temp&bsol;servers&period;txt<&sol;strong><&sol;p>&NewLine;<p><strong>servers&period;txt<&sol;strong> example&colon;<&sol;p>&NewLine;<p><em>hostname<&sol;em><br &sol;>&NewLine;<em>hostname2<&sol;em><br &sol;>&NewLine;<em>hostname3<&sol;em><&sol;p>&NewLine;<p>Now create a new text file and rename it to <strong>serverlist&period;ps1<&sol;strong><&sol;p>&NewLine;<pre>&dollar;counter &equals; 1 &NewLine;&dollar;comps &equals; get-content C&colon;&bsol;TEMP&bsol;servers&period;txt &NewLine;&dollar;dnsResults &equals; "C&colon;&bsol;TEMP&bsol;IP address&period;csv" &NewLine; &NewLine;function get-dnsres&lbrace; &NewLine;foreach &lpar;&dollar;comp in &dollar;comps&rpar; &lbrace; &NewLine;&dollar;TempIP &equals; &lpar;&lbrack;system&period;net&period;dns&rsqb;&colon;&colon;GetHostAddresses&lpar;&dollar;comp&rpar;&rpar; &vert; select IPAddressToString &NewLine; &NewLine;&dollar;status &equals; "Processing system &lbrace;0&rcub; of &lbrace;1&rcub;&colon; &lbrace;2&rcub;" -f &dollar;counter&comma;&dollar;comps&period;Count&comma;&dollar;comp &NewLine;Write-Progress 'Resolving DNS' &dollar;status -PercentComplete &lpar;&dollar;counter&sol;&dollar;comps&period;count &ast; 100&rpar; &NewLine;&dollar;counter&plus;&plus; &NewLine;&dollar;comp &vert; &NewLine;select &commat;&lbrace;Name&equals;'ComputerName'&semi;Expression&equals;&lbrace;&dollar;comp&rcub;&rcub;&comma; &grave; &NewLine;&commat;&lbrace;Name&equals;'ResolvesToIP'&semi;Expression&equals;&lbrace;&lbrack;system&period;net&period;dns&rsqb;&colon;&colon;GetHostAddresses&lpar;&dollar;comp&rpar;&rcub;&rcub;&comma; &grave; &NewLine;&commat;&lbrace;Name&equals;'IPResolvesTo'&semi;Expression&equals;&lbrace;&lpar;&lbrack;system&period;net&period;dns&rsqb;&colon;&colon;GetHostEntry&lpar;&dollar;TempIP&period;IPAddressToString&rpar;&rpar;&period;HostName&rcub;&rcub;&comma; &grave; &NewLine;&commat;&lbrace;Name&equals;'PingStatus'&semi; Expression&equals;&lbrace; &grave; &NewLine;if &lpar;&lpar;get-wmiobject -query "SELECT &ast; FROM Win32&lowbar;PingStatus WHERE Address&equals;'&dollar;comp'"&rpar;&period;statuscode -eq 0&rpar; &lbrace;'Host Online'&rcub; &grave; &NewLine;elseif &lpar;&lpar;get-wmiobject -query "SELECT &ast; FROM Win32&lowbar;PingStatus WHERE Address&equals;'&dollar;comp'"&rpar;&period;statuscode -eq 11003&rpar; &lbrace;'Destination Host Unreachable'&rcub; &grave; &NewLine;elseif &lpar;&lpar;get-wmiobject -query "SELECT &ast; FROM Win32&lowbar;PingStatus WHERE Address&equals;'&dollar;comp'"&rpar;&period;statuscode -eq 11010&rpar; &lbrace;'Request Timed Out'&rcub; &grave; &NewLine;elseif &lpar;&lpar;get-wmiobject -query "SELECT &ast; FROM Win32&lowbar;PingStatus WHERE Address&equals;'&dollar;comp'"&rpar;&period;statuscode -eq &dollar;Null&rpar; &lbrace;'NoDNS'&rcub; &NewLine;&rcub; &NewLine;&rcub; &NewLine;&rcub; &NewLine;&rcub; &NewLine; &NewLine;get-dnsres &vert; export-csv &dollar;dnsResults -notypeinformation &NewLine;invoke-item &dollar;dnsResults &NewLine;<&sol;pre>&NewLine;<p>Save the script to the same folder at C&colon;&bsol;temp&period;<&sol;p>&NewLine;<p>Before you&&num;8217&semi;ll be able to run the script you will have to enable execution policy on your system&colon;<&sol;p>&NewLine;<p>Start the Powershell in elevated mode &lpar;Run as Administrator&rpar; and run the following command&colon;<&sol;p>&NewLine;<p><strong>Set-ExecutionPolicy RemoteSigned<&sol;strong><&sol;p>&NewLine;<p>Now run the Powershell script by using this command&colon;<&sol;p>&NewLine;<p><strong>powershell -noexit &&num;8220&semi;&amp&semi; &&num;8220&semi;&&num;8221&semi;C&colon;&bsol;temp&bsol;serverlist&period;ps1&&num;8243&semi;&&num;8221&semi;&&num;8221&semi;<&sol;strong><&sol;p>&NewLine;<p>You should get the results on screen and also saved to <strong>C&colon;&bsol;temp&bsol;IP address&period;csv<&sol;strong> file&period;<&sol;p>&NewLine;<p><a href&equals;"https&colon;&sol;&sol;www&period;wincert&period;net&sol;wp-content&sol;uploads&sol;2016&sol;01&sol;server-list-script&period;png" rel&equals;"attachment wp-att-1637"><img class&equals;"alignnone size-full wp-image-1637" src&equals;"https&colon;&sol;&sol;www&period;wincert&period;net&sol;wp-content&sol;uploads&sol;2016&sol;01&sol;server-list-script&period;png" alt&equals;"server list script" width&equals;"644" height&equals;"148" &sol;><&sol;a><&sol;p>&NewLine;<p>Please feel free to comment if you have any advices regarding this simple server list script&period;<&sol;p>&NewLine;<p>There are also some third-party free tools available for download like&colon;<&sol;p>&NewLine;<p><a href&equals;"http&colon;&sol;&sol;www&period;nirsoft&period;net&sol;utils&sol;fastresolver&period;html" target&equals;"&lowbar;blank">NirSoft FastResolver<&sol;a><br &sol;>&NewLine;<a href&equals;"http&colon;&sol;&sol;www&period;ks-soft&period;net&sol;ip-tools&period;eng&sol;" target&equals;"&lowbar;blank">IP Tools<&sol;a><&sol;p>&NewLine;

Exit mobile version