Changing TCP to dynamic using PowerShell

This topic contains 11 replies, has 2 voices, and was last updated by Profile photo of Viji keswaran Viji keswaran 2 years, 3 months ago.

  • Author
    Posts
  • #17854
    Profile photo of Viji keswaran
    Viji keswaran
    Participant

    That's my code and I'm having difficulties making it work. It does not change my adapter settings.

    #DHCP Function To Enable DCHP On The Local Computer
    ###############################################################

    Function DHCP

    {
    $Adapt = Get-WMIObject Win32_NetworkAdapterConfiguration | Where {$_.IPenable -eq "True"} |`
    Foreach-Object {$_.EnableDHCP();$_.SetDNSServersearchobject()}
    Write-Host "Rebooting Now"
    Start-Sleep -s 5
    Restart-Computer
    }

  • #17856
    Profile photo of Viji keswaran
    Viji keswaran
    Participant

    Part of a bigger code–

    Function Main
    {
    $Host.UI.RawUI.Backgroundcolor = "Black"
    $Host.UI.RawUI.ForegroundColor = "Green"
    }

    Main

    #MenuHelper Function To Create A Box

    #########################################################

    Function MenuHelper ($MenuItems, $Title)
    {
    [String] $Title = "Changing TCP/IP Settings"
    [Array] $MenuItems = @("1. TCP/IP Settings To DHCP, 2. TCP/IP Settings To Static 3.Exit Program")
    $Item1 = "1. TCP/IP Settings To DHCP"
    $Item2 = "2. TCP/IP Settings To Static"
    $Item3 = "3. Exit Program"
    [int] $Width = $Title.Length + 15
    [int] $Bottom = $Line.Length + 39
    [char] $LeftTopB = "╔"
    [char] $Line = "═"
    [char] $RightTopB = "╗"
    [char] $VLine = "║"
    [char] $LeftD = "╠"
    [char] $RightD = "╣"
    [Char] $LeftBottomB = "╚"
    [char] $RightBottomB = "╝"

    Clear-Host
    Write-Host $LeftTopB -NoNewline
    Write-Host ("$Line" * $Width) -NoNewline
    Write-Host $RightTopB
    Write-Host "$Vline `t$Title `t$VLine"
    Write-Host $LeftD -NoNewline
    Write-Host ("$Line" * $Width) -NoNewline
    Write-Host $RightD
    Write-Host "$VLine `t$Item1 `t$VLine"
    Write-Host "$VLine `t$Item2 `t$VLine"
    Write-Host "$VLine `t$Item3 `t`t`t$VLine"
    Write-Host "$VLine `t`t`t`t`t$VLine"
    Write-Host "$LeftBottomB" -NoNewline
    Write-Host ("$Line" * $Bottom) -NoNewline
    Write-Host "$RightBottomB"
    }

    MenuHelper

    #DHCP Function To Enable DCHP On The Local Computer

    ###############################################################

    Function DHCP

    {
    $Adapt = Get-WMIObject Win32_NetworkAdapterConfiguration | Where {$_.IPenable -eq "True"} | Foreach-Object {$_.EnableDHCP();$_.SetDNSServersearchobject()}
    Write-Host "Rebooting Now"
    Start-Sleep -s 5
    Restart-Computer
    }

    #Set Static IP Function To Change From DHCP To Static Addressing

    ###############################################################

    Function Static

    {
    $NIC = Get-WMIObject Win32_NetworkAdapterConfiguration | Where {$_.IPenabled -eq "True"}
    foreach ($Item in $NIC)
    {
    $Item.EnableStatic("10.0.0.18","255.0.0.0")
    $Item.SetGateways("10.0.0.1")
    $Item.SetDNSServerSearchOrder("10.0.0.18")
    $Item.SetDynamicDNSRegistration("False")
    Write-Host "Rebooting Now"
    Start-Sleep -s 5

    }
    }

  • #17857
    Profile photo of Adnan Rashid
    Adnan Rashid
    Participant

    Possibly because you have a spelling error its $_.IPenabled not $_.IPenable

  • #17859
    Profile photo of Viji keswaran
    Viji keswaran
    Participant

    Tried that and this error message comes.

  • #17861
    Profile photo of Viji keswaran
    Viji keswaran
    Participant

    Looking back I also found one more error –

    $_.SetDNSServersearchobject()} suppose to be SetDNSServerSearchOrder()

    But still not working

  • #17862
    Profile photo of Adnan Rashid
    Adnan Rashid
    Participant

    looks like another typo its 'SetDNSServerSearchOrder' not 'SetDNSServerSearchObject'

  • #17863
    Profile photo of Adnan Rashid
    Adnan Rashid
    Participant

    oh soz misread your message – ignore that

  • #17865
    Profile photo of Viji keswaran
    Viji keswaran
    Participant

    Function Set-DHCP
    {
    $Adapt = Get-WMIObject Win32_NetworkAdapterConfiguration -filter "IPenabled = 'True'" ;
    $Adapt.EnableDHCP();
    $Adapt.SetDNSServerSearchOrder();
    Write-Host "Rebooting Now in 10 Seconds"
    Start-Sleep -s 10
    Restart-Computer
    }

    Used that code and managed to make it work 🙂

    Just one more thing.

    how do I create a desktop shortcut to your script and place it in the c:\users\public\desktop folder, so that all logged in users can have access to it.

  • #17866
    Profile photo of Viji keswaran
    Viji keswaran
    Participant

    Function Set-DHCP
    {
    $Adapt = Get-WMIObject Win32_NetworkAdapterConfiguration -filter "IPenabled = 'True'" ;
    $Adapt.EnableDHCP();
    $Adapt.SetDNSServerSearchOrder();
    Write-Host "Rebooting Now in 10 Seconds"
    Start-Sleep -s 10
    Restart-Computer
    }

    Used that code and managed to make it work 🙂

    Just one more thing.

    how do I create a desktop shortcut to your script and place it in the c:\users\public\desktop folder, so that all logged in users can have access to it.

  • #17867
    Profile photo of Adnan Rashid
    Adnan Rashid
    Participant

    You could save it as a module which is a psm1 file. Check your module locations: $env:PSModulePath -split ";"

    I tend to put it in C:\Program Files\WindowsPowerShell\Modules\**Your Function Name**

    then you can just load the module when you open powershell by doing 'import-module DHCP' in your case.

  • #17868
    Profile photo of Adnan Rashid
    Adnan Rashid
    Participant

    In fact you wouldn't even have to load the module you would just type DHCP into Powershell. Really it should be verb-noun so Set-DHCP something like that and then it will run.

  • #17870
    Profile photo of Viji keswaran
    Viji keswaran
    Participant

    when i save it as psm1 file all i get is a blank notepad

    Also, I [b]need [/b]to make a shortcut on desktop that will execute when double clicked

You must be logged in to reply to this topic.