Using Powershell Packagemanager (OneGet) basics

One of the new great features of WMF5, included in Windows 10 RTM is Powershell Packagemanager (previously called OneGet).

I will not go into the details on how it works or what it is (that you can find here!) I do want to show you how you can use it to simply install some packages from the powershell gallery, or from chocolatey which is basically a community based package source that already includes alot of applications and tools.

Lets take a look at the basic command sets:
Find-Package: find the package you are looking for in the registered package sources in powershell packagemanager
Get-Package: finds the packages on your system that have been installed using powershell packagemanager
Install-Package: installs a package on your system using powershell packagemanager
Uninstall-Package: removes a package from your system that was installed using powershell packagemanager

When you first kick off find-package you may get the request that the package provider nuget has not been installed and needs to be downloaded and installed.

The provider 'nuget v2.8.5.127' is not installed.
nuget may be manually downloaded from https://oneget.org/nuget-anycpu-2.8.5.127.exe and installed.
Would you like PackageManagement to automatically download and install 'nuget' now?
[Y] Yes  [N] No  [S] Suspend  [?] Help (default is "Y"):

If you select Yes, you will now (by default) get a list of available packages -from the powershell gallery only-.

Lets now install the chocolatey package provider so that we can use PowerShell Packagemanager to install available community packages.

PS C:\Users\Administrator> get-packageprovider -name chocolatey

The provider 'chocolatey v2.8.5.130' is not installed.
chocolatey may be manually downloaded from https://oneget.org/ChocolateyPrototype-2.8.5.130.exe and installed.
Would you like PackageManagement to automatically download and install 'chocolatey' now?
[Y] Yes  [N] No  [S] Suspend  [?] Help (default is "Y"):

If you now once again do a find-package you will suddenly see alot of additional packages that are available from the source ‘chocolatey’. Please be aware however that not all of these packages can be trusted (please read the chocolatey FAQ)

You can install simply the packages by using the install-package, or pipe the install package behind the find-package to also install the dependencies (if any). For example:

Install-package -Name 'GoogleChrome'
Find-Package 'GoogleChrome' -IncludeDependencies | Install-Package

After the software has been installed you can review the installed software by using get-package

Get-Package -Name 'GoogleChrome'

Name                           Version          Source           Summary
----                           -------          ------           -------
GoogleChrome                   45.0.2454.101    C:\Chocolatey...

Here is an example script that you can use for installing some software to your local desktop with powershell packagemanager: oneget_example.ps1

I hope you can enjoy this new functionality as much as myself. This new functionality could greatly help with package deployment and management in the future. You could for example set up your own package repository and deploy your own internal packages using Nuget Server.

Using Powershell to get your server’s uptime!

I have always missed the option in windows to just easily see the uptime from a server.

Ofcourse we have the uptime.exe available from Microsoft, but I wanted to have something available directly from powershell.

For starters we could get the last bootuptime from WMI with the following query

(Get-WmiObject -Class win32_operatingsystem).lastbootuptime
20151005150834.490241+120

Since this is not really in a nice format, lets change it into something we can read

[System.Management.ManagementDateTimeconverter]::ToDateTime((Get-WmiObject -Class win32_operatingsystem).lastbootuptime)
Tuesday, October 6, 2015 10:37:14 AM

Now that we have the bootup time/date in a readable format we can substract this date from our current time/date to get our actual uptime.

((Get-Date)-([System.Management.ManagementDateTimeconverter]::ToDateTime((Get-WmiObject -Class win32_operatingsystem).lastbootuptime)))

Days              : 0
Hours             : 0
Minutes           : 16
Seconds           : 8
Milliseconds      : 736
Ticks             : 9687365862
TotalDays         : 0.0112122290069444
TotalHours        : 0.269093496166667
TotalMinutes      : 16.14560977
TotalSeconds      : 968.7365862
TotalMilliseconds : 968736.5862

And there you go, you can now see the uptime for your server. I took this along a bit and created a powershell function to make it more usable for everyone in the company.

Function Get-Uptime 
{
    <#
            .SYNOPSIS
            Get uptime for Server(s)
            .DESCRIPTION
            This script will provide you the uptime for your servers in easy format
            .PARAMETER Servers
            One or multiple servers
            .EXAMPLE
            Get-Uptime -Server "computername"
            Request the uptime for a server
    #>
    [CmdletBinding(SupportsShouldProcess = $true)]

    param(
        [parameter(Mandatory = $true, HelpMessage = 'Please enter a computername.')]
        [alias('Computernames','Computername','Server','Computer')]
        [string[]]$servers
    )

    ForEach ($server in $servers) 
    {
        Write-Verbose -Message "Retrieving information from $server"
        $uptime = ((Get-Date )-([System.Management.ManagementDateTimeconverter]::ToDateTime((Get-WmiObject -Class win32_operatingsystem -ComputerName $server -ErrorAction SilentlyContinue ).lastbootuptime)))
        $server | Select-Object -Property @{
            Name       = 'Computername'
            Expression = {
                $server
            }
        }, 
        @{
            Name       = 'Days'
            Expression = {
                $uptime.Days
            }
        }, 
        @{
            Name       = 'Hours'
            Expression = {
                $uptime.Hours
            }
        }, 
        @{
            Name       = 'Minutes'
            Expression = {
                $uptime.Minutes
            }
        }, 
        @{
            Name       = 'Seconds'
            Expression = {
                $uptime.Seconds
            }
        }, 
        @{
            Name       = 'Milliseconds'
            Expression = {
                $uptime.Milliseconds
            }
        }
    }
}

Now you only need to type Get-Uptime “servername” to get the uptime for the server you wish 🙂

PS C:\> get-uptime localhost | FT

Computername Days Hours Minutes Seconds Milliseconds
------------ ---- ----- ------- ------- ------------
localhost       0    19      49      32          478