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 []> help Invoke-Expression -full


    Runs commands or expressions on the local computer.

    Invoke-Expression [-Command]  []

    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.

        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 []> Invoke-Expression C:\Users\mkerfoot\Desktop\HTML_report.html
Invoke-Expression can also be used to open an image or any filepath applicable.
PS []> 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.
    Chris Davis
    Matthew A. Kerfoot
   Outputs how long since the last reboot
    This function gathers information regarding when $ComputeName was last rebooted. `
   Get-Uptime localhost | ConvertTo-Html | Out-File C:\
   Referance -

function Get-Uptime { [CmdletBinding()]

param ( [Parameter(Mandatory=$false,
           [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'.
[]> Get-Uptime
Name                           Value
----                           -----
Hours                          5
Days                           0
ComputerName                   VT-MKERFOOT-W8
Minutes                        52

PS []>

Tuesday, October 29, 2013

Get all stopped services that are set to autostart

Have you ever wanted to check which services were set to run automatically at boot up but are currently stopped?


I find myself wanting to know this all to often, this is one of the first things I check when there is an issue with a service that a server is hosting. The below function will help you find out any and all services that have been configured to start automatically but are currently stopped or not running.

Copy\Paste the below code into a PowerShell window.
    Matthew A. Kerfoot
   Finds Services set to start automatically but are stopped
   This function will find all services that are set to start automatically at startup but are not currently running.
   Get-Stopped -ComputerName localhost | Out-file "$env:USERPROFILE\Desktop\StoppedServices.txt"

function Get-Stopped {

              Param ( [Parameter(Mandatory=$false,
                      $Computername )

         Begin { $Obj = Get-WmiObject -Class Win32_Service }

     Process { $Stopped = $Obj | Where-Object { ($_.StartMode -eq "Auto") -and ($_.State -eq "Stopped") } | 
              Select-Object Name, DisplayName, StartMode, State, Description }

 End { $Stopped | Format-Table -AutoSize }

Hit Enter a couple times and then type 'Get-Stopped' to get a table of all service that are set to start when windows boots yet are not running.
PS []> Get-Stopped

Name           DisplayName                     StartMode State   Description
----           -----------                     --------- -----   -----------
gupdate        Google Update Service (gupdate) Auto      Stopped Keeps your Google software up to d
RemoteRegistry Remote Registry                 Auto      Stopped Enables remote users to modify reg
SCardSvr       Smart Card                      Auto      Stopped Manages access to smart cards read
sppsvc         Software Protection             Auto      Stopped Enables the download, installation
wuauserv       Windows Update                  Auto      Stopped Enables the detection, download, a

PS []>
Now you can restart any service you feel should be running for example to start the Windows Update service just type, 'Get-Service -Name wuauserv | Start-Service -Verbose'.