Archive | PowerShell

Install and configure an Apache web server on Linux with DSC and Azure Automation

This post will describe how to install an apache server, check that the apache service is running and configure the default web page with Desired State Configuration and Azure Automation. After deployed, I will show how to monitor that the configuration is not drifting with DSC in Azure Automation. This DSC service with Azure Automation works both for servers in the on-prem datacenter or at a public cloud provider.

The blog post will also show how to create an easy DSC Configuration file, add it so DSC in Azure Automation, and deploy it to a Linux computer. I will not show how to create an Azure Automation account or deploy the DSC agent. If you need help to deploy DSC on Linux, please see my blog post “Easy installation and registration script for OMS and DSC on Linux” where I install OMI, DSC and the OMS agent.

Get started

To get started we have to create a DSC Configuration file. Below is anexample that will deploy an Apache web server, check that Apache is running and that the web page looks like it should. To test this configuration save the below configuration in a file named DSCLinux01.ps1. This configuration uses the Apt package installer in Linux and is tested on Debian and Ubuntu.

When the configuration file is created go to the Azure Automation account and click DSC Configurations.

Install and configure an Apache web server on Linux with DSC and Azure Automation1

After entered the DSC Configuration click “Add a configuration file” and upload the file DSCLinux01.ps1.

Install and configure an Apache web server on Linux with DSC and Azure Automation2

 

Click on the “DSCLinux01” and then “Compile”. When the status is “Completed” it is time to deploy the configuration to a Linux computer.

Install and configure an Apache web server on Linux with DSC and Azure Automation3

Before the configuration can be deploy the DSC Configuration DSC needs to be installed on the Linux server that is going to use the configuration. See my blog post “Easy installation and registration script for OMS and DSC on Linux” where I install OMI, DSC and the OMS if you need help with this.

When DSC is installed and registered the server shows up under DSC Nodes. Click DSC Nodes and the server to deploy DSC configurations to.

Install and configure an Apache web server on Linux with DSC and Azure Automation4

Click Assign Node Configuration to be able to select the configuration you wish to use.

Install and configure an Apache web server on Linux with DSC and Azure Automation5

Select “DSCLinux01.apache2”.

Install and configure an Apache web server on Linux with DSC and Azure Automation6

Check that the configuration was selected in the “Node Configuration” section.

Install and configure an Apache web server on Linux with DSC and Azure Automation7

Now it is just to wait for the configuration to be added to the server. In the picture below one can see that the configuration has been added and checked that the server is compliant with the configuration file.

Install and configure an Apache web server on Linux with DSC and Azure Automation8

To check the end result, enter the IP or DNS for the server in a web browser to check if one can see the webpage. The result should look like this.

Install and configure an Apache web server on Linux with DSC and Azure Automation10

Let’s also make a quick check on the server to see the apache is installed and the service is started.

Install and configure an Apache web server on Linux with DSC and Azure Automation11

0

Create an Azure Automation DSC Configuration with PowerShell

This is a short introduction on how you can create an Azure Resource Group, Azure Automation Account and DSC configuration with PowerShell.

The first one need is a DSC configuration file to upload into the DSC Configuration. In this example I will use an DSC Configuration file with configuration for the IIS role. To try this, save the following script as a PowerShell ps1 file as C:\DSC\IISFeature.ps1.

To create a Resource Group, Azure Automation Account and DSC Configuration with PowerShell run the following.

0

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 https://aka.ms/tp4/New-ContainerHost -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

0

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

try
{
    $Connection = new-object system.data.sqlclient.sqlconnection
    $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())
"@

    $Connection.open()
    $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]
    $DataSet.Tables.Clear()

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

Screenshot of the result when running the PowerShell script

Orchestrator PowerShell script SQL query to get last ran jobs1

0

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

2