Remotely change DHCP address to static

Welcome Forums General PowerShell Q&A Remotely change DHCP address to static

This topic contains 6 replies, has 4 voices, and was last updated by

3 years, 6 months ago.

  • Author
  • #28041

    Points: 4
    Rank: Member

    Trying to script to set IP address remotely. Can't seem to get it to work. It changes DHCP to static, but doesn't actually set the new IP address.

    $compname = read-host "Computer to configure"
    $IPAddress = read-host "Computer IP address"
    $cred = Get-Credential
    Invoke-Command -ComputerName $compname -InDisconnectedSession -ScriptBlock {
    Get-NetAdapter | Set-NetIPInterface -Dhcp Disabled; Get-NetAdapter | New-NetIPAddress -IPAddress $IPAddress -DefaultGateway "" -PrefixLength 24
    Set-DnsClientServerAddress -InterfaceIndex $netindex -ServerAddresses ("","","","","")
    } -Credential $cred

  • #28042

    Points: 1,811
    Helping HandTeam Member
    Rank: Community Hero

    So... you're very likely getting multiple Network Adapters since Get-NetAdapter also returns all the virtual ones by default. Keep that in mind. Could this be failing because it's assigning the address to a virtual adapter first, and then failing on the others because the address is already assigned to an adapter?

  • #28054

    Points: 4
    Rank: Member

    Actually, I'm running this against a VM, which only comes back with one adapter. So while that part isn't the best coding, it isn't where my problem lies.

    The problem appears to be that it turns off DHCP, then doesn't set a static address.

  • #28055

    Points: 41
    Rank: Member

    I think the problem is that you're setting $IPAddress locally but using it on the remote computer. In your script block, try it with $using:IPAddress.

    Look at Get-Help about_Remote_Variables

  • #28056

    Points: 527
    Helping Hand
    Rank: Major Contributor

    Add -ArgumentList $IPAddress to your Invoke-Command and update the variable to using:$IPAddress. Above go to Resources > Free E-Books > Secrets of Powershell Remoting for additional information

  • #28150

    Points: 4
    Rank: Member

    I tried it. Again it turns off DHCP, but doesn't assign the static IP address.

  • #28830

    Points: 4
    Rank: Member

    Anyone else have any ideas? It seems like once it runs the command to turn off DHCP, it stops running any of the other commands. I think it needs to send the entire contents of the Invoke-Command to the remote machine at once, but it is sending it over one command at a time. So the next command in the scriptblock isn't executing (to set the new IP address).

    The code works perfectly when run from the local console.

The topic ‘Remotely change DHCP address to static’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort