Avatar of whuigens
 asked on

Help with scripting

I work for a MSP and we have lots of clients that have different internal IP addressing some 192.168.1. , 192.168.16. , and 10.1.1.  I'm trying to find a way that you can ping the Default Gateway, DNS Server and DHCP from command line without knowing the IP address of the object beforehand. Trying to set this up to help connectivity troubleshooting with non-technical users so they'd have to click on a .bat icon instead of trying to spend time trying to get them to run CMD commands over the phone. During my scouring online I did find this .bat that works for finding the default gateway. I have no idea how to modify it to output the DNS Server or DHCP.

@Set title=Pinging the Default Gateway...
@(color 6A) & (@Title %title%)
@echo please wait...

::# Define a temp variable 'DefaultGateway'
@For /f "tokens=3" %%* in (
   'route.exe print ^|findstr "\<\>"'
   ) Do @Set "DefaultGateway=%%*"

::# Redirect output to a textfile
@> "pinggateway.txt" (
  @echo [%Date% %time%] - %title%
  @ping %DefaultGateway%
@echo Done
Network ManagementShell Scripting

Avatar of undefined
Last Comment

8/22/2022 - Mon
David Johnson, CD

stick an @ipconfig /all after @ping %DefaultGateway%

View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.

With the method we're using to have users access this. There is no way to run .ps1. My only option is to use command prompt.

Users don't have to run the .PS1 file directly, they can just run the .BAT file like I mentioned.
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.

We use Continuum as a vendor to monitor our clients computers. They have a component to their agent that has an icon in the Notification area that clients can click on to do various troubleshooting steps. We can make customized versions of it but it only runs command prompt. I'd have to load the .bat file to the same location on every PC or make a separate communicator for each site with a custom network path to the .bat file. Not looking to spend that much time customizing it. Otherwise I'd just create a Communicator for each site and just do a ping command for each of their DNS and Gateway locations. Trying to find a command that will ping gateway and ping DNS without knowing the exact address. Trying to have one general command to use in one communicator customization. Otherwise I'd have to create 50-60 different Communicators, one per client site.

Ah, I see what you're getting at now.

Try this.  It's a single command.

Open in new window

To get to that
# In a PowerShell window
$command = 'Get-WmiObject Win32_NetworkAdapterConfiguration -Filter "ipenabled = ''true''" | ForEach { "============"; "Pinging Default Gateway"; ping -n 1 $_.defaultipgateway; ""; "============"; "Pinging DNS servers"; $_.Dnsserversearchorder | ForEach { ping -n 1 $_ }; ""; "============"; "Pinging DHCP server"; ping -n 1 $_.dhcpserver}'
$bytes = [System.Text.Encoding]::Unicode.GetBytes($command)
[Convert]::ToBase64String($bytes) | Out-File encoded.txt
# Open the "encoded.txt" file.  Copy the entire contents as the Base64 encoded command (should not have any line breaks or spaces).

# Run the encoded command in a command prompt as shown above.

Open in new window