Disable NIC on remote server in powershell

  • Hi I have a requirement to disable NIC using powershell remotely. I'm using below command. It works when i hardcode NIC's Name in the command but it is failing when i pass the NIC's Name in variable.

    Working Fine: Invoke-Command -Computer MYREMOTESERVER {(Disable-NetAdapter –Name "Trunk1" –Confirm:$false)}

    Failing: Invoke-Command -Computer MYREMOTESERVER {(Disable-NetAdapter –Name $NICName –Confirm:$false)}

  • You will want to use the "-args" parameter for Invoke-Command.

    Invoke-Command -Computer MYREMOTESERVER {Disable-NetAdapter –Name $NICName –Confirm:$false}

    Would become

    Invoke-Command -Computer MyRemoteServer { Disable-NetAdapter -Name $NICName -Confirm:$false } -Args $nic

    The parameter name you pass does not matter that much as long as you only have one parameter. If you for some reason need to pass multiple, maybe for passing the $false or $true you could do it as this I think:

    Invoke-Command -Computer MyRemoteServer { Disable-NetAdapter -Name $args[0] -Confirm:$args[1] } -Args $nic,$true

    Pretty sure that should work...

    Shawn Melton
    Twitter: @wsmelton
    Blog: wsmelton.github.com
    Github: wsmelton

  • Mac1986 (1/7/2016)


    Hi I have a requirement to disable NIC using powershell remotely. I'm using below command. It works when i hardcode NIC's Name in the command but it is failing when i pass the NIC's Name in variable.

    Working Fine: Invoke-Command -Computer MYREMOTESERVER {(Disable-NetAdapter –Name "Trunk1" –Confirm:$false)}

    Failing: Invoke-Command -Computer MYREMOTESERVER {(Disable-NetAdapter –Name $NICName –Confirm:$false)}

    Very curious... why would someone want to do this? What is the requirement that makes this a requirement?

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • It is still failing with same error Cannot validate argument on parameter 'Name'. The argument is null. Provide a valid value for the argument, and then try running the command again.

    I've used below 2 commands.

    Invoke-Command -Computer MyRemoteServer { Disable-NetAdapter -Name $NICName -Confirm:$false } -Args $nic

    Invoke-Command -Computer MyRemoteServer { Disable-NetAdapter -Name $args[0] -Confirm:$args[1] } -Args $nic,$true

  • I might be the dummest person and i'm sure it might wront to recycle NICs one at a time at 15 hour interval. I'm seeing that IPV6 is slowing down and recycling NICs is getting me back the normal latency.

  • Mac1986 (1/8/2016)


    I might be the dummest person and i'm sure it might wront to recycle NICs one at a time at 15 hour interval. I'm seeing that IPV6 is slowing down and recycling NICs is getting me back the normal latency.

    Cool. Never heard of someone trying to automate such a thing.

    Of course, that also means that the reason why we haven't heard of it is because it might be a really bad idea (and I do mean "might" because I don't actually know for sure but seems like it). I'll also say that all sounds like a symptom of a larger problem. For example, I've seen it where a simple switch setting like "auto negotiate" on a local router and/or the NICs themselves can cause huge problems that appear to "build up" over time.

    Something else is going on, here, Mac. You should find a good hardware/network forum and dig into it because I think changing the band aid on a stab wound every night isn't the right thing to do.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Mac1986 (1/8/2016)


    I might be the dummest person and i'm sure it might wront to recycle NICs one at a time at 15 hour interval. I'm seeing that IPV6 is slowing down and recycling NICs is getting me back the normal latency.

    Quick question, are you using IP6 on that server? If not, disable it!

    😎

  • Jeff Moden (1/8/2016)


    Never heard of someone trying to automate such a thing.

    Don't think automating this is a good idea and especially on a remote server, anything goes wrong and it's dead in the water.

    😎

  • Eirikur Eiriksson (1/10/2016)


    Jeff Moden (1/8/2016)


    Never heard of someone trying to automate such a thing.

    Don't think automating this is a good idea and especially on a remote server, anything goes wrong and it's dead in the water.

    😎

    Agreed... like I said in my previous post... "Of course, that also means that the reason why we haven't heard of it is because it might be a really bad idea"

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Hello Shawn,

    can you please help me figure out the answer to disable NIC using "-args"

    $NICName = 'Trunk1'

    Invoke-Command -Computer MyRemoteServer { Disable-NetAdapter -Name $NICName -Confirm:$false } -Args $nic

Viewing 10 posts - 1 through 9 (of 9 total)

You must be logged in to reply to this topic. Login to reply