Tag: write-host

PS: PowerShell Version Verification

PS: PowerShell Version Verification

I have made the following below to validate the version of powershell that is currently running on the machine and if the version is incorrect to print and error and close the script. Pretty handy snippet to put at the start of your script

if ($PSVersionTable.PSVersion.Major -gt 2)
Write-host 'Current System Powershell Version Meets Script Requirements'
Write-host 'Current System Powershell Version does not meet requirements' -ForegroundColor Red
Write-host 'Please Update your Powershell Version to 2.0 or greater' -ForegroundColor Red
write-host 'Script has stopped' -ForegroundColor Red
PS: Changing NIC IP Configuration

PS: Changing NIC IP Configuration

Okay, so it’s taken me a fair while of time to get this script to work needed for one of my latest projects so due to it taking me a while to piece this together I feel that it’s probably worthwhile putting it out on here for you guys to use if you wish. The following code will change the IP of a NIC and reconfigure the DNS settings using NETSH

 <# Get Network Adapter #>
Get-NetAdapter | Format-Table -AutoSize
<# User Input Network Interface #>
write-host 'Please Enter Network Interface Name: ' -ForegroundColor Yellow -NoNewline
$nicname = Read-Host
Write-host 'Please Enter New Host IP: ' -ForegroundColor Yellow -NoNewline
$NewIP = Read-Host
Write-host 'Please Enter New Subnet Mask: ' -ForegroundColor Yellow -NoNewline
$NewMask = Read-Host
Write-Host 'Please Enter New Gateway: ' -ForegroundColor Yellow -NoNewline
$NewGate = Read-Host
Write-Host 'Please Enter DNS Server 1: ' -ForegroundColor Yellow -NoNewline
$NewDNS1 = Read-Host
Write-Host 'Please Enter DNS Server 2:' -ForegroundColor Yellow -NoNewline
$NewDNS2 = Read-Host
write-host 'Setting New IPv4 Configuration'
& "$env:windir\system32\netsh.exe" interface ipv4 set address $nicname static $NewIP $NewMask $NewGate
write-host 'Removing Exsisting DNS Config'
& "$env:windir\system32\netsh.exe" interface ip delete dns $nicname all
write-host 'Adding New DNS Config'
& "$env:windir\system32\netsh.exe" interface ipv4 set dns name=$nicname source=static address=$NewDNS1 primary validate=no
& "$env:windir\system32\netsh.exe" interface ipv4 add dnsserver name=$nicname address=$NewDNS2 index=2 validate=no
PS: Validating User Input for an IP Address or Subnet Mask

PS: Validating User Input for an IP Address or Subnet Mask

Okay in one of my scripts I needed to incorporate user input of an IP address and to validate the input as valid IPv4 address. Take a look at the snippet below

 <#IP Validation Pattern#>
$IPPattern = '^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$'

<#User Input of IP #>
Write-Host "Please Enter IP: " -ForegroundColor Yellow -NoNewline
<#Input Loop Until Validation meets pattern#>
do {
<# Set IP address var #>
$IPADR = Read-Host
$ok = $IPADR -match $IPPattern
if ($ok -eq $false) {
<# Error Message and Prompt to enter an IP #>
Write-Warning ("'{0}' is not an IP address." -f $IPADR)
write-host -fore Yellow "Please Enter IP: " -NoNewline
} until ( $ok )