Powershell Notes¶
- Author:
Dimitry Dukhovny
Note
To donate to the maintenance of these pages, do send BTC to bc1qkw0pp78kv67zrgp8xds7qrqen7mhlz0rs5p8p5
Networking¶
Also see Windows networking.
Get IP address¶
1# All of them
2Get-NetIPAddress
3# The address for NIC 3
4# note the "InterfaceAlias" is "Ethernet 4"
5Get-NetIPAddress | Where-Object ${_.InterfaceIndex -eq 3}
Set the new IP address¶
For this example, we will assign 192.168.100.100 in our /24 to the interface alias Ethernet 4 with a default gateway of 192.168.100.254.
Our domain controllers in this example serve DNS and are at 192.168.100.1 and 192.168.100.2.
1New-NetIPAddress -InterfaceAlias "Ethernet 4" -IPAddress "192.168.100.100" -PrefixLength 24 -DefaultGateway 192.168.100.254
2Set-DnsClientServerAddress -InterfaceAlias "Ethernet 4" -ServerAddresses 192.168.100.1, 192.168.100.2
Port scan an address¶
For this example, we want to see if port 3389 is open on 192.168.100.0/24.
1$port = 3389
2$net = "192.168.100"
3$range = 1..254
4foreach ($r in $range)
5{
6 $ip = "{0}.{1}" -F $net,$r
7 if(Test-Connection -BufferSize 32 -Count 1 -Quiet -ComputerName $ip)
8 {
9 $socket = new-object System.Net.Sockets.TcpClient($ip, $port)
10 If($socket.Connected)
11 {
12 "$ip listening to port $port"
13 $socket.Close()
14 }
15 }
16}
Replace DNS records from CSV¶
1# From Microsoft at
2# https://gallery.technet.microsoft.com/Update-DNS-records-with-da10910d
3
4# Environment Setup
5$DNSServer = "YourDNSServer"
6$DNSZone = "YourZoneName"
7$InputFile = "dnsrecords.csv"
8
9# Read the input file which is formatted as name,type,address with a header row
10$records = Import-CSV $InputFile
11
12# Now we loop through the file to delete and re-create records
13# DNSCMD does not have a modify option so we must use /RecordDelete first followed by a /RecordAdd
14
15ForEach ($record in $records) {
16
17 # Capture the record contents as variables
18 $recordName = $record.name
19 $recordType = $record.type
20 $recordAddress = $record.address
21
22 # Build our DNSCMD DELETE command syntax
23 $cmdDelete = "dnscmd $DNSServer /RecordDelete $DNSZone $recordName $recordType /f"
24
25 # Build our DNSCMD ADD command syntax
26 $cmdAdd = "dnscmd $DNSServer /RecordAdd $DNSZone $recordName $recordType $recordAddress"
27
28 # Now we execute the command
29 Write-Host "Running the following command: $cmdDelete"
30 Invoke-Expression $cmdDelete
31
32 Write-Host "Running the following command: $cmdAdd"
33 Invoke-Expression $cmdAdd
34}
Promote a domain controller¶
For this example, assume serverB is a member of domain mydomain.gov.
1# Install the AD DS role
2Install-WindowsFeature -name AD-Domain-Services -IncludeManagementTools
3
4# Run a test to make sure you will not foxtrot the system in the process
5# This will prompt for a password
6Test-ADDSForestInstallation -DomainName mydomain.gov -InstallDns
7
8# If everything works, do it for real
9# This will also prompt for a password
10# Select "A" for "Yes to all" if you really want to do this
11Install-ADDSForest -DomainName mydomain.gov -InstallDNS
12# The output should have a bunch of "Success" statements
13
14# Check your work
15Get-ADDomainController
16# The first line should show something like...
17# CN=serverB,OU=Domain Controllers,DC=mydomain,DC=gov