Script to ping from the list of remote server then change gateway on server with specific network, if server have no gateway, skip it.

Hello Script expert,

I have 100 + servers need to change to the new gateway. I would like to ping the new gateway from servers first, then look through the server's NIC to see if the NIC is on specific subnet xxx.xxx. with the old gateway then change to the new gateway, do nothing if server(s) have no gateway. 

I have the script but only check for the NIC have IP address on specific subnet. When tested, sometime it throw out an error : 

"Get-WmiObject: Cannot validate argument on parameter 'ComputerName'. The argument is null or empty."

Here is the script:

_______________________________________________________________________

$DefGateway = "xxx.xxx.xxx.xxx"
$server_list = get-content "C:\server.txt"
foreach($server in $server_list)
    {
    $nics = Get-WmiObject -Class Win32_NetworkAdapterConfiguration -ComputerName $server -Filter "IPEnabled=TRUE"
    foreach($nic in $nics)
        {
        if($nic.ipaddress -match "xxx.xxx." & ) s
       
            {
            $nic.SetGateways($DefGateway)
            }
       
            
          
        }
    }

________________________________________________________________

Here is the script to ping from servers:

$servers = Get-Content "C:\Ping.txt"
    Invoke-Command -ComputerName $servers -ScriptBlock { cmd /c ping -n xxx.xxx.xxx.xxx } 

_______________________________________________________________________

Again, it throw an error: "Get-WmiObject: Cannot validate argument on parameter 'ComputerName'. The argument is null or empty."

I'm really bad with scripting. Please help!

Thanks in advance for the help from all the experts here.

DTNG



  • Edited by dtng 10 hours 39 minutes ago
August 20th, 2015 4:32pm

 Get-Content C:\Ping.txt |
    ForEach-Object{
        Invoke-Command -ComputerName $_ -ScriptBlock { cmd /c ping -n xxx.xxx.xxx.xxx } 

    }

Free Windows Admin Tool Kit Click here and download it now
August 20th, 2015 4:53pm

What is in servers.txt.  It appears to have no information or bad information.

August 20th, 2015 4:55pm

Thanks for your reply. A list of server name in the servers.txt.

DTNG

Free Windows Admin Tool Kit Click here and download it now
August 20th, 2015 5:20pm

Post a sample of the file.

August 20th, 2015 5:28pm

ChangeGWTest1
ChangeGWTest2
ChangeGWTest3
Free Windows Admin Tool Kit Click here and download it now
August 20th, 2015 5:29pm

Are those names all on separate lines?

Try this:

foreach($server in (get-content C:\server.txt)){
     Write-Host ">>>>>$server<<<<<<" -fore green
     $nics = Get-WmiObject -Class Win32_NetworkAdapterConfiguration -ComputerName $server -Filter "IPEnabled=TRUE"
     foreach($nic in $nics){
         if($nic.ipaddress -match "xxx.xxx." & ){
             $nic.SetGateways($DefGateway)
         }         
    }
} 

August 20th, 2015 5:32pm

Yes, they are. The script works thought sometime it throw an error. What I'm looking for is to have if/else on the script that check if server's NIC is on specific subnet and have the old gateway of xxx.xxx.xxx.xxx then change to the new gateway, if the NIC have no gateway, skip it.

Again, thanks for your speed reply. I really appreciated.

DTNG

Free Windows Admin Tool Kit Click here and download it now
August 20th, 2015 5:49pm

Is that a question?  YOU apparently have some crap inn your file if you are still getting t he same errors.

It is not possible to be of much help if you are going to be so vague.  You have to try to make clear unambiguous statements.

August 20th, 2015 5:57pm

Sorry for not clear on my message. 

What I would like to look for in a script is that 

1. Ping the gateway from the list of remote servers.

2. Search for the server have a NIC that on specific subnet and have the old gateway xxx.xxx.xxx.xxx, then change to the new gateway.

3. If the server have no gateway then leave it alone

I hope this is clear enough.

Again, sorry for the confusion and thanks for your reply.

DTNG


Free Windows Admin Tool Kit Click here and download it now
August 20th, 2015 6:24pm

That is not a question.  You are asking a laundry list of things. What is the question?  What is the error?  What is it that is not happening?

You need to be specific.

August 20th, 2015 6:34pm

The script I have is working as expected. excepted that when doing testing, sometime it throws the error: ""Get-WmiObject: Cannot validate argument on parameter 'ComputerName'. The argument is null or empty."

The script that I have will change the gateway on all server, whether server have gateway or not. What I needed is that the script to check for if server have no gateway then leave it alone.

Free Windows Admin Tool Kit Click here and download it now
August 20th, 2015 6:42pm

As I posted above.  It is your input file.  You cannot have any blank lines.  Fix the file and the problem will go away.
August 20th, 2015 6:55pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics