Author Posts

August 7, 2014 at 9:36 am

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
}

August 7, 2014 at 9:43 am

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

}
}

August 7, 2014 at 9:57 am

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

August 7, 2014 at 10:17 am

Tried that and this error message comes.

August 7, 2014 at 10:24 am

Looking back I also found one more error –

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

But still not working

August 7, 2014 at 10:24 am

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

August 7, 2014 at 10:25 am

oh soz misread your message – ignore that

August 7, 2014 at 10:53 am

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.

August 7, 2014 at 10:53 am

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.

August 7, 2014 at 11:09 am

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.

August 7, 2014 at 11:11 am

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.

August 7, 2014 at 11:29 am

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