################ Powershell Notes ################ :Author: Dimitry Dukhovny .. contents:: .. .. note:: .. To help support this site and learn more, check out .. `Windows PowerShell Cookbook: The Complete Guide to Scripting Microsoft's Command Shell `_ .. note:: To donate to the maintenance of these pages, do send BTC to bc1qkw0pp78kv67zrgp8xds7qrqen7mhlz0rs5p8p5 .. .. image:: images/qrcode-gnomon-static.png Networking ========== Also see `Windows networking `_. Get IP address -------------- .. code-block:: powershell :linenos: # All of them Get-NetIPAddress # The address for NIC 3 # note the "InterfaceAlias" is "Ethernet 4" Get-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**. .. code-block:: powershell :linenos: New-NetIPAddress -InterfaceAlias "Ethernet 4" -IPAddress "192.168.100.100" -PrefixLength 24 -DefaultGateway 192.168.100.254 Set-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**. .. code-block:: powershell :linenos: $port = 3389 $net = "192.168.100" $range = 1..254 foreach ($r in $range) { $ip = "{0}.{1}" -F $net,$r if(Test-Connection -BufferSize 32 -Count 1 -Quiet -ComputerName $ip) { $socket = new-object System.Net.Sockets.TcpClient($ip, $port) If($socket.Connected) { "$ip listening to port $port" $socket.Close() } } } Replace DNS records from CSV ---------------------------- :download:`Download ReplaceDNSRecord.ps1 ` .. literalinclude:: code_samples/ReplaceDNSRecord.ps1 :linenos: :language: powershell Promote a domain controller --------------------------- For this example, assume **serverB** is a member of domain **mydomain.gov**. .. code-block:: powershell :linenos: # Install the AD DS role Install-WindowsFeature -name AD-Domain-Services -IncludeManagementTools # Run a test to make sure you will not foxtrot the system in the process # This will prompt for a password Test-ADDSForestInstallation -DomainName mydomain.gov -InstallDns # If everything works, do it for real # This will also prompt for a password # Select "A" for "Yes to all" if you really want to do this Install-ADDSForest -DomainName mydomain.gov -InstallDNS # The output should have a bunch of "Success" statements # Check your work Get-ADDomainController # The first line should show something like... # CN=serverB,OU=Domain Controllers,DC=mydomain,DC=gov