Archive | PowerShell

Deploy a Windows Container Host to a New Hyper-V Virtual Machine based on the Windows Server 2016 ISO

The following documentation “Deploy a Windows Container Host to a New Hyper-V Virtual Machine” explains in a very nice way how to deploy a Windows Container Host. In the example a Windows Server 2016 TP4 image will be downloaded to the computer that the script is ran on.

If you already have downloaded a copy of the Windows Server 2016 you can use the example below to deploy your Windows Container Host to a new Hyper-V VM without downloading a new copy of the OS.

Start by downloading the “New-ContainerHost.ps1” script.

wget -uri -OutFile c:\New-ContainerHost.ps1

To build the Windows Container Host on a new Hyper-V VM from a Windows Server 2016 ISO run the following and make a note of the parameter “-IsoPath” that points to the ISO.

c:\New-ContainerHost.ps1 -IsoPath C:\iso\Server2016TP4\10586.0.151029-1700.TH2_RELEASE_SERVER_OEMRET_X64FRE_EN-US.ISO -VmName ConTP401 -WindowsImage NanoServer -HyperV -Verbose


Orchestrator PowerShell script / SQL query to get last ran jobs

I got asked how to get the last ran jobs in Orchestrator by a SQL query. This is what I came up with.

select * from [Microsoft.SystemCenter.Orchestrator.Runtime.Internal].Jobs AS jobs join POLICIES ON jobs.RunbookId = POLICIES.UniqueID where jobs.CreationTime >= dateadd(hh,-2,getdate())

I then decided to add some PowerShell around the SQL query. This because I wanted a fast access to the result when running PowerShell together with Orchestrator. The reason is that I found it faster than accessing the result by using the Orchestrator Webservice. Ether copy the script below or download it at the following link: GetRunningRunbooks

Remember you will need read access to the Orchestrator database.

# Input strings for the script
$DatabaseServerName = "localhost"
$DatabaseName = "Orchestrator"
$h = 2

    $Connection = new-object
    $Connection.ConnectionString ="server=$DatabaseServerName;database=$DatabaseName;trusted_connection=True"
    $SqlCmd = New-Object System.Data.SqlClient.SqlCommand

    $SqlQuery = @"
        select * from [Microsoft.SystemCenter.Orchestrator.Runtime.Internal].Jobs AS jobs join POLICIES ON jobs.RunbookId = POLICIES.UniqueID  where jobs.CreationTime >= dateadd(hh,-$h,getdate())

    $SqlCmd.CommandText = $SqlQuery
    $SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
    $SqlAdapter.SelectCommand = $SqlCmd
    $SqlCmd.Connection = $Connection
    $DataSet = New-Object System.Data.DataSet
    $q = $SqlAdapter.Fill($DataSet)
    [array]$r = $DataSet.Tables[0]

    Foreach ($id in $r)
         $id.Name +" - "+ $id.CreationTime
    Write-host "Connection to database unsuccessful."

Screenshot of the result when running the PowerShell script

Orchestrator PowerShell script SQL query to get last ran jobs1


Deploy a shortcut with SCCM 2012

In the following post I will show one way to deploy a shortcut with System Center Configuration Manager 2012 to every user on a PC client. To do this I will use a simple PowerShell script and beforehand create the shortcut file.

Create the shortcut file by right click and add the information needed.

Deploy a shortcut with SCCM 20121

Add the shortcut to a SCCM package together with the following “CopyToAllUsersDesktops” PowerShell script. Remember to change the “NewShortcut.lnk” in the script to the name of the link to deploy.


Now just create a SCCM program that kicks of the PowerShell script and deploy it.

Screenshots of Deployment Package in SCCM

Deploy a shortcut with SCCM 20122

Deploy a shortcut with SCCM 20123


Add devices from a text file to a SCCM 2012 Collection

There is many versions of this out on the Internet. But this is mine!

This script will add all the devices in a file to a specified Collection. Just save the script below to a file called “AddDeviceToCollection.ps1” and change the section marked as “Change this section”. After this just run the command as follow:

.\AddDeviceToCollection.ps1 -CollectionID PS100001 -File .\ComputersToAdd.txt

Code Example



Script for coping files that are not already at destination

This is a script for coping files from one place to another. It will only copy a file if the file at destination is not the same as the one at the source. The script will also create the folder structure at destination if it doesn’t exist.

Try it out by just running it at a PowerShell prompt or as a login script. The script can be downloaded here: “CopyFiles“.