Site icon Da3mon Computer

Automating Network Configuration Backup and DHCP to Static IP Conversion in PowerShell

images

Network configuration is crucial for system administrators managing multiple servers and devices. Automating the backup and conversion of network settings from DHCP to a static IP can save time and reduce errors. The PowerShell script below accomplishes this by backing up current network configurations and then converting DHCP-assigned IP addresses to static ones.

Here’s a detailed breakdown of how this script works and how you can utilize it effectively.

Script Overview

The PowerShell script ensures it runs with administrative privileges, checks if the backup directory exists and has the necessary write permissions, backs up current network configurations, and then converts DHCP-configured network adapters to static IP configurations.

Script Breakdown

1. Ensure Administrative Privileges

The script starts by checking if it is running with administrative privileges. Many network configuration commands require elevated permissions.

2. Define Backup File Path

The script defines a backup directory and file where the network configuration will be saved.

3. Ensure Backup Directory Exists

It ensures the backup directory exists or creates it if it doesn’t.

4. Check Write Permissions

Before proceeding, the script checks for write permissions to the backup directory by attempting to create and delete a test file.

5. Backup Current Configuration

It defines a function to backup current network configurations using Get-NetIPConfiguration and Export-Clixml.

6. Identify Connected Network Adapters

The script identifies network adapters that are currently connected.

7. Backup Configuration

The network configuration is backed up to the specified file.

8. Convert DHCP to Static IP

For each adapter, the script checks if DHCP is enabled. If so, it retrieves the current DHCP settings, then converts these to static IP settings using New-NetIPAddress, Set-NetIPAddress, Set-NetRoute, and Set-DnsClientServerAddress.

Using the Script

Preparation:

1. Ensure you have administrative privileges.
Modify $backupDirectory and $backupFile paths as needed.
Execution:

2. Run the script in a PowerShell session with elevated privileges.
Observe the output to confirm successful backup and conversion.
Verification:

3. Check the backup file for accuracy.
Verify the static IP configuration using Get-NetIPConfiguration or similar commands.

Complete Script Convert DHCP IP to Static with Directory Creation

Conclusion
Automating the backup and configuration of network settings using PowerShell can streamline network management tasks. This script offers a reliable way to ensure network configurations are saved and easily convert DHCP settings to static IP addresses, simplifying network administration. Adapt and extend this script as needed to fit your specific network environment.

Exit mobile version