Set PC's IP

This topic contains 5 replies, has 2 voices, and was last updated by Profile photo of Mario Rico Mario Rico 1 year, 7 months ago.

  • Author
    Posts
  • #32726
    Profile photo of Mario Rico
    Mario Rico
    Participant

    Im trying to convert this batch file over to power shell but im completely lost dont even know were to start. need help please. also if you guys know of any good books for beginners and script writing i will appreciate it

    @ECHO OFF

    ECHO #############################################
    ECHO ###—CONFIGURE IP AND PRINTER SETTINGS—###
    ECHO #############################################
    ECHO.

    ECHO ###—SETTING VARIABLES—###
    ECHO.

    SET /p _compIP="Enter the IP for the PC:"
    FOR /f "tokens=1-4 delims=. " %%a IN ("%_compIP%") DO (
    SET _octetA=%%a
    SET _octetB=%%b
    SET _octetC=%%c
    SET _octetD=%%d
    )
    SET _printerSIP=%_octetA%.%_octetB%.%_octetC%.58
    SET _printerFDIP=%_octetA%.%_octetB%.%_octetC%.57
    SET _gateway=%_octetA%.%_octetB%.%_octetC%.33

    ECHO ###—CREATING PRINTER PORTS—###
    ECHO.

    CD C:\Windows\System32\Printing_Admin_Scripts\en-US
    CSCRIPT Prnport.vbs -a -o raw -r "%_printerSIP%" -h "%_printerSIP%"
    CSCRIPT Prnport.vbs -a -o raw -r "%_printerFDIP%" -h "%_printerFDIP%"

    ECHO ###—ASSIGNING PORT TO PRINTER—###
    ECHO.

    CSCRIPT Prncnfg.vbs -t -p "Sales" -r "%_printerSIP%"
    CSCRIPT Prncnfg.vbs -t -p "Front Desk" -r "%_printerFDIP%"

    ECHO ###—SETTING IP ADDRESS, SUBNET MASK, GATEWAY AND DNS—###
    ECHO.

    NETSH INT IPV4 SET ADDRESS "LOCAL AREA CONNECTION" STATIC "%_compIP%" "255.255.255.224" "%_gateway%"
    NETSH DNSCLIENT ADD DNSSERVER "LOCAL AREA CONNECTION" x.x.x.x 1
    NETSH DNSCLIENT ADD DNSSERVER "LOCAL AREA CONNECTION" x.x.x.x 2

    ECHO ####################
    ECHO ###—CLEAN UP—###
    ECHO ####################
    ECHO.

    gpupdate /force

    DEL C:\Users\Public\Desktop\1_Set_PC_IP.bat /S /Q

    exit 0

  • #32727
    Profile photo of Mark Hammonds
    Mark Hammonds
    Participant

    here is how to set an ip address with powershell. The script you posted is calling several VB scripts there is no way of knowing everything that document is trying to do. If you give us an outline of what you want done we can write something up for you

    https://technet.microsoft.com/en-us/%5Clibrary/hh826151(v=wps.630).aspx

    Here is how to do it using WMI

    $IP      = "10.0.1.123"
    $NetMask = "255.255.248.0"
    $Gateway = "10.0.0.1"
    $DNS     = "10.0.7.203"
    
    $adapter = Get-WmiObject win32_NetworkAdapterConfiguration `
        -filter "IPEnabled = 'true'"
    $adapter.EnableStatic($IP, $NetMask)
    Sleep -Seconds 4
    $adapter.SetGateways($Gateway)
    $adapter.SetDNSServerSearchOrder($DNS)
    
  • #32730
    Profile photo of Mario Rico
    Mario Rico
    Participant

    Thanks Mark

    So that script that i have above is on my images when i run it it prompts me for what ip do i want to use so say I type
    10.0.1.123
    subnetmask and DNS does not change for us so thats fine i can set that with the commands you gave me

    our gateway is depended on the first 3 octet of the pc's ip in this case it will be 10.0.1.22
    and always ends in .22

    ip scheme changes depending on the store location.

    the following is how my batch file looks like with out all the VB scripts that i was using to add printers

    SET /p _compIP="Enter the IP for the PC:"
    FOR /f "tokens=1-4 delims=. " %%a IN ("%_compIP%") DO (
    SET _octetA=%%a
    SET _octetB=%%b
    SET _octetC=%%c
    SET _octetD=%%d
    )
    SET _gateway=%_octetA%.%_octetB%.%_octetC%.22

    NETSH INT IPV4 SET ADDRESS "LOCAL AREA CONNECTION" STATIC "%_compIP%" "255.255.255.224" "%_gateway%"
    NETSH DNSCLIENT ADD DNSSERVER "LOCAL AREA CONNECTION" 10.23.25.30 1
    NETSH DNSCLIENT ADD DNSSERVER "LOCAL AREA CONNECTION" 10.23.25.40 2

    ===========================================================
    after running it with the above ip it will change the ip, subnet, gateway, dns 1 and 2 to the following

    10.0.1.123
    255.255.255.224
    10.0.1.22

    DNS
    10.23.25.30
    10.23.25.40

  • #32731
    Profile photo of Mark Hammonds
    Mark Hammonds
    Participant

    so if you run this. it will prompt you for an ip address it will then take the first 3 octets and append it with 22 to make the Gateway

    $IP = (read-host -prompt "Please enter IP Address")
    $NetMask = "255.255.255.224"
    $Gateway = ($IP.TrimEnd($ip.Split(".")[-1]) + "22")
    $DNS     = "10.23.25.30"
    
    
    
    $adapter = Get-WmiObject win32_NetworkAdapterConfiguration `
        -filter "IPEnabled = 'true'"
    $adapter.EnableStatic($IP, $NetMask)
    Sleep -Seconds 4
    $adapter.SetGateways($Gateway)
    $adapter.SetDNSServerSearchOrder($DNS)
    
  • #32732
    Profile photo of Mark Hammonds
    Mark Hammonds
    Participant

    here is what you want as a Function you run the script or . source it
    then type

    Set-IPaddress 10.0.1.123

    Function Set-IPaddress {
        [cmdletbinding()]
        Param(
            [parameter(ValueFromPipeline)]
            [ValidatePattern(^([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])(\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])){3}$)]
            [string[]]$IP
        )
        Process {
            $NetMask = "255.255.255.224"
            $Gateway = ($IP.TrimEnd($ip.Split(".")[-1]) + "22")
            $DNS     = "10.23.25.30"
            $adapter = Get-WmiObject win32_NetworkAdapterConfiguration  -filter "IPEnabled = 'true'"
            $adapter.EnableStatic($IP, $NetMask)
            Sleep -Seconds 4
            $adapter.SetGateways($Gateway)
            $adapter.SetDNSServerSearchOrder($DNS) 
        }
    }
    

    this version will check valid IP address

    $Gateway = ($IP.TrimEnd($ip.Split(".")[-1]) + "22")

    this basically says take 10.0.1.123 remove the 123 from the end and add 22 then store it in a variable called $Gateway

  • #32735
    Profile photo of Mario Rico
    Mario Rico
    Participant

    Cool thanks for all your help Mark

You must be logged in to reply to this topic.