function Show-Menu
{
param (
[string]$Title = 'Windows Server Automation with Powershell '
)
Clear-Host
Write-Host "================ $Title ================"
Write-Host "1: Press '1' for Setup Network Interface Card and IP Address."
Write-Host "2: Press '2' for Setup ADDS, DNS."
Write-Host "3: Press '3' for Setup DHCP."
Write-Host "4: Press '4' for Setup BULK User from CSV File."
Write-Host "Q: Press 'Q' to quit."
}
#variable
#function IP-Setting
function IP-SetupFunction{
Write-Host "IP Configuration "
$IPAddress= Read-Host "Enter IP Address (192.168.40.2) "
$SubnetMask= Read-Host "Enter Subnetmask (24) "
$DefaultGateway= Read-Host "Default Gateway (192.168.40.1) "
# find out Interface card Index
$adapter = Get-NetIPConfiguration | select InterfaceIndex
foreach ($ad in $adapter){
$_ins = $ad.InterfaceIndex
}
#Set Network
Try{
New-NetIPAddress -InterfaceIndex $_ins -IPAddress $IPAddress -PrefixLength $SubnetMask -DefaultGateway $DefaultGateway
Set-DnsClientServerAddress -InterfaceIndex $_ins -ServerAddresses $IPAddress
Write-Host "IP Address successfully set to $($IPAddress)"-ForegroundColor Green
}
catch{
Write-Host "Failed to apply network setting"-ForegroundColor Red
}
#Set Hostname
$HostName = Read-Host "Enter your Host Name : "-ForegroundColor Green
Write-Host "Rebooting Now !!! "-ForegroundColor Green
Sleep 30
Try{
Rename-Computer -NewName $HostName -Restart
}
catch{
Write-Host "Failed to set Host Name "-ForegroundColor Red
}
}
# SetUp ADDS
function ACtive-Directory-Setup{
#ADDS
Add-WindowsFeature AD-Domain-Services
Import-Module ADDSDeployment
Install-ADDSForest
get-addomain
Sleep 30
#DNS
Install-windowsFeature DNS -IncludeManagementTools
Get-windowsFeature | ?{$_.name -like "dns"}
}
#SetUp DHCP
function Dhcp-Setup{
#DHCP
Install-windowsfeature DHCP -IncludeManagementTools
netsh dhcp add securitygroups
$_StartRange= Read-Host "Start Range (192.168.40.30) "
$_EndRange= Read-Host "End Range (192.168.40.50) "
$_SubnetMask= Read-Host "Subnet Mask (255.255.255.0) "
Add-DHCPServerv4Scope -Name “Primary Scope” -StartRange $_StartRange -EndRange $_EndRange -SubnetMask $_SubnetMask –LeaseDuration 2.00:00:00 -State Active
Add-DhcpServerInDc -DnsName kkk.com -IPAddress 192.168.40.2 # Here you change your Dns Name and Server IP Address
Get-DhcpServerv4scope
Get-DhcpServerInDC
Restart-Service DHCPServer
Write-Host "DHCP Setup Complete !!! "-ForegroundColor Green
}
#Setup Bulk User and read data from CSV file
function BulkUser-Setup{
# Import the AD module
Import-Module activedirectory
# Store the data from NewADUsers.csv in the $ADUsers variable
$ADUsers = Import-csv C:\NewADUsers1.csv -Delimiter ';'
# Loop through each row in the CSV Sheet
foreach ($User in $ADUsers)
{
# Read data from each field in the row and assign data to a variable
$Username = $User.username
$Password = $User.password
$Firstname = $User.firstname
$Lastname = $User.lastname
$OU = $User.ou
$email = $User.email
$streetaddress = $User.streetaddress
$city = $User.city
$zipcode = $User.zipcode
$state = $User.state
$country = $User.country
$description = $User.description
$office = $User.office
$telephone = $User.telephone
$jobtitle = $User.jobtitle
$company = $User.company
$department = $User.department
$Password = $User.Password
# Check if user already exists
if (Get-ADUser -F {SamAccountName -eq $Username})
{
# If user exists, give warning
Write-Warning "User account $Username already exists."
}
else
{
# User does not exist so proceed with creation of new user account
New-ADUser `
-SamAccountName $Username `
-UserPrincipalName "$Username@praktik.com" `
-Name "$Firstname $Lastname" `
-GivenName $Firstname `
-Surname $Lastname `
-Enabled $True `
-DisplayName "$Lastname, $Firstname" `
-Path $OU `
-City $city `
-Company $company `
-State $state `
-StreetAddress $streetaddress `
-OfficePhone $telephone `
-EmailAddress $email `
-Title $jobtitle `
-Department $department `
-AccountPassword (convertto-securestring $Password -AsPlainText -Force) -ChangePasswordAtLogon $True
}
}
}
#Main Menu
do
{
Show-Menu –Title 'Windows Server Automation with Powershell'
$selection = Read-Host "Please make a selection"
switch ($selection)
{
'1' {
IP-SetupFunction
} '2' {
ACtive-Directory-Setup
} '3' {
Dhcp-Setup
}
'4' {
BulkUser-Setup
}
#'q' {
# return
#}
}
pause
} until ($selection -eq 'q')