Wednesday, November 13, 2013

Find neighbor cache with PowerShell

Have you ever wanted to view the neighbor cache or possibly the IP address of a friend or family members networked device? New with PowerShell 3.0 is the cmdlet Get-NetNeighbor which will retrieve all neighbor cache information for IPv4 and IPv6 by default.

To get the neighbor cache we used to have to:


Type netsh, and then press ENTER.

Type interface ipv6, and then press ENTER.

Type show neighbors, and then press ENTER.


 Now don't get me wrong that method still works......however with PowerShell 3.0 we can just type        Get-NetNeighbor (as shown below).

PS [www.matthewkerfoot.com]> Get-NetNeighbor

ifIndex IPAddress                                        LinkLayerAddress   State       PolicyStore
------- ---------                                        ----------------   -----       -----------
100     ff02::1:ffv5:307b                                333ffff3ffa5       Permanent   ActiveStore
100     ff02::5:2                                        33ff33000100       Permanent   ActiveStore
100     ff02::12                                         33ff33000000       Permanent   ActiveStore
100     ff02::2                                          3ff333000000       Permanent   ActiveStore
100     fe80::f93b:d3e4:eccc:7b6b                        000000000000       Unreachable ActiveStore
100     fe80::eccc:af83:398f:a1a8                        000000000000       Unreachable ActiveStore
100     fe80::eccc:6ab0:eccc:136e                        000000000000       Unreachable ActiveStore
100     fe80::a53e:5e59:1aaa:469b                        000000000000       Unreachable ActiveStore
100     fe80::95c4:eccc:fa9c:eccc                        000000000000       Unreachable ActiveStore
100     fe80::810b:9551:eccc:8f76                        000000000000       Unreachable ActiveStore
100     fe80::eccc:af1d:592d:bb91                        000000000000       Unreachable ActiveStore
100     fe80::790f:eccc:8c2:39fd                         000000000000       Unreachable ActiveStore
100     fe80::6882:eccc:7d16:c810                        000000000000       Unreachable ActiveStore
100     fe80::eccc:8044:2eb2:efca                        000000000000       Unreachable ActiveStore
100     fe80::55e9:284a:8568:a20d                        000000000000       Unreachable ActiveStore
100     fe80::25f4:8f34:2ccb:3da5                        000000000000       Unreachable ActiveStore
100     fe80::1822:5444:6ba5:eccc                        000000000000       Unreachable ActiveStore
100     fe80::1b5:b466:8065:eccc                         000000000000       Unreachable ActiveStore
97      ff02::1:eccc:307b                                3333ffa5307b       Permanent   ActiveStore
97      ff02::1:eccc:5663                                3333ffff5663       Permanent   ActiveStore
97      ff02::1:3                                        33330ff00100       Permanent   ActiveStore
97      ff02::2:8                                        3333000ff100       Permanent   ActiveStore
Actual Data has been manipulated

The Get-NetNeighbor cmdlet is an awesome addition to PowerShell, this cmdlet allows us to have the same capabilities as we always could before with netsh and just like a Cisco network device by running the command show CDP Neighbor. I like to modify this cmdlet at little bit to show me only IPv4 address' that I have recently talked to.
Get-NetNeighbor -AddressFamily IPv4 | Where-Object { $_.State -ne "Permanent" } | Sort-Object State,IPAddress -Descending
I find the output of the above cmdlets ussually gives me everything I want to know.
PS [www.matthewkerfoot.com]> Get-NetNeighbor -AddressFamily IPv4 | Where-Object { $_.State -ne "Permanent" } | Sort-Object State,IPAddress -Descending

ifIndex IPAddress                                        LinkLayerAddress   State       PolicyStore
------- ---------                                        ----------------   -----       -----------
95      10.10.10.1                                      50eef56fede2       Reachable   ActiveStore
95      10.10.10.100                                    eab56ef256a2       Stale       ActiveStore
95      10.10.10.101                                    000000000000       Unreachable ActiveStore
95      10.10.10.254                                    000000000000       Unreachable ActiveStore

PS [www.matthewkerfoot.com]>

Monday, November 11, 2013

Open an .HTML file with PowerShell

Have you ever wanted to open a .HTML file with PowerShell, either in a script or just because you were already in a PS console? There is actually a really easy way to do this in your default browser via PowerShell. The cmdlet "Invoke-Expression" will run the any command or expression.

Lets take a look at Invoke-Expression's help file by running the command help Invoke-Expression -full I prefer to run the cmdlet Help instead of the full cmdlet Get-Help because help will only show you a page at a time, so by default typing help is the equivalent to Get-Help command | more
PS [www.matthewkerfoot.com]> help Invoke-Expression -full

NAME
    Invoke-Expression

SYNOPSIS
    Runs commands or expressions on the local computer.

SYNTAX
    Invoke-Expression [-Command]  []


DESCRIPTION
    The Invoke-Expression cmdlet evaluates or runs a specified string as a command and returns the results of the
    expression or command. Without Invoke-Expression, a string submitted at the command line would be returned
    (echoed) unchanged.


PARAMETERS
    -Command 
        Specifies the command or expression to run. Type the command or expression or enter a variable that
        contains the command or expression. The Command parameter is required.
In order to open an .HTML file from PowerShell you must first know the location of the file, then simply type, "Invoke-Expression .\filename.html"
PS [www.matthewkerfoot.com]> Invoke-Expression C:\Users\mkerfoot\Desktop\HTML_report.html
Invoke-Expression can also be used to open an image or any filepath applicable.
PS [www.matthewkerfoot.com]> Invoke-Expression C:\Users\mkerfoot\Desktop\Icon.PNG
The Following command will open up the file I specified in your default browser.


Wednesday, October 30, 2013

Find amount of time since Windows started

Whether you need to find out the time since a computer was turned on for ticketing purposes, just out of curiosity or to show off how long your server has been online to a friend, you have come to the right place. Here is a PowerShell function to find out how many Days, Hours, and Minutes since the machine was started.

Copy the below code into a PowerShell window.
<#
.CREATED BY:
    Chris Davis
.MODIFIED BY:
    Matthew A. Kerfoot
.MODIFIED ON:
    10\30\2013
.Synopsis
   Outputs how long since the last reboot
.DESCRIPTION
    This function gathers information regarding when $ComputeName was last rebooted. `
.EXAMPLE
   Get-Uptime localhost | ConvertTo-Html | Out-File C:\
   Referance - http://pc-addicts.com/my-powershell-scripts/
#>

function Get-Uptime { [CmdletBinding()]

param ( [Parameter(Mandatory=$false,
                   ValueFromPipelineByPropertyName=$true,
                   Position=0)]
           [string]$ComputerName = "$env:COMPUTERNAME" )

Begin { $OS = Get-WmiObject -Class Win32_OperatingSystem -ComputerName $Computername
        $diff = $OS.ConvertToDateTime($OS.LocalDateTime) - $OS.ConvertToDateTime($OS.LastBootUpTime) }

Process { foreach ( $_ in $ComputerName ){ 
                   @{ "ComputerName" = $Computername
                      "Days" = $diff.Days
                      "Hours" = $diff.Hours
                      "Minutes" = $diff.Minutes } } }

End { New-Object -TypeName PSObject -Property $properties | Format-Table -AutoSize }}

Once the above code is pasted into a PowerShell prompt hit enter a couple times and type, 'Get-Uptime'.
[www.matthewkerfoot.com]> Get-Uptime
Name                           Value
----                           -----
Hours                          5
Days                           0
ComputerName                   VT-MKERFOOT-W8
Minutes                        52

PS [www.matthewkerfoot.com]>