Skip to content

Commit

Permalink
Adds presentations and demos from NIC 2017
Browse files Browse the repository at this point in the history
  • Loading branch information
janegilring committed Feb 10, 2017
1 parent fc9b9f5 commit 3154a70
Show file tree
Hide file tree
Showing 37 changed files with 1,581 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# Toggle regions: Ctrl + M

#region Demo setup
Write-Warning 'This is a demo script which should be run line by line or sections at a time, stopping script execution'

break

<#
Author: Jan Egil Ring & Øyvind Kallstad, Crayon AS
Name: 01 - Git.ps1
Description: This demo script is part of the presentation
DevOps for Ops - Automate All the Things!
#>

<#
Putting your code into a version control system is one of the most important steps
in creating an automated pipeline model. We are going to use GitHub; one of the more popular
hosted git services.
It's benefits include:
- backup of your code
- version control
- multiple collaborators
- issue tracking
- project management
#>


# 01 - Create new Repository in GitHub
# NIC.ServerDeployment
& 'C:\Program Files (x86)\Google\Chrome\Application\chrome.exe' 'https://github.com/CrayonAS'
'NIC.ServerDeployment' | Set-Clipboard

# 02 - Clone Repository (Visual Studio Code)
# F1 - git - clone - choose folder
Push-Location '~\Documents\GitHub'
git clone 'https://github.com/CrayonAS/NIC.ServerDeployment.git'

# 03 - Copy code to Repository
$path = '~\Documents\GitHub\Crayon.Demo.DevOps\Module\0.1\*'
$destination = '~\Documents\GitHub\NIC.ServerDeployment'
Copy-Item -Path $path -Destination $destination -Recurse -Force

code $destination

# 04 - Sync and show code in GitHub
Push-Location $destination
git add *
git commit -m 'first commit'
git push origin master

# 05 - Update code, sync, show in GitHub
$path = '~\Documents\GitHub\Crayon.Demo.DevOps\Module\0.2\*'
$destination = '~\Documents\GitHub\NIC.ServerDeployment'
Copy-Item -Path $path -Destination $destination -Recurse -Force

Push-Location $destination
git add *
git commit -m 'add vmmserver parameter'
git push origin master

Pop-Location
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
# Toggle regions: Ctrl + M

#region Demo setup
Write-Warning 'This is a demo script which should be run line by line or sections at a time, stopping script execution'

break

<#
Author: Jan Egil Ring & Øyvind Kallstad, Crayon AS
Name: 02 - PowerShellGet.ps1
Description: This demo script is part of the presentation
DevOps for Ops - Automate All the Things!
#>


#region Module basics

Get-Command *Module*

Get-Module

# List all modules available in $env:PSModulePath (typically C:\Program Files\WindowsPowerShell\Modules, C:\Windows\system32\WindowsPowerShell\v1.0\Modules & $env:USERPROFILE\Documents\WindowsPowerShell\Modules)
Get-Module -ListAvailable

# Creating your own module

# First, let`s clone the demo repository to local disk
git clone https://github.com/CrayonAS/NIC.ServerDeployment.git D:\GitHub\NIC.ServerDeployment

# Copy starting point
Copy-Item -Path D:\GitHub\Crayon.Demo.DevOps\Module\0.1\ -Destination D:\GitHub\NIC.ServerDeployment -Recurse -Container

dir D:\GitHub\NIC.ServerDeployment
tree D:\GitHub\NIC.ServerDeployment /F /A

$PSD1 = 'D:\GitHub\NIC.ServerDeployment\NIC.Serverdeployment.psd1'

$params = @{
AliasesToExport = ''
Author = 'Jan Egil Ring & Øyvind Kallstad'
CmdletsToExport = ''
Copyright = 'Crayon AS'
CompanyName = 'Crayon AS'
Description = 'Demo module for NIC 2017'
Guid = (New-Guid).Guid
FunctionsToExport = 'New-NanoVM','Connect-VMConsole','Invoke-OfflineDomainJoin'
LicenseUri = 'https://github.com/CrayonAS/NIC.ServerDeployment/blob/master/LICENSE'
ModuleVersion = '0.1'
ReleaseNotes = 'Initial version'
RootModule = 'NIC.ServerDeployment.psm1'
Path = $PSD1
PassThru = $true
PowerShellVersion = '5.0'
ProjectUri = 'https://github.com/CrayonAS/NIC.ServerDeployment'
Tags = 'Tools'
VariablesToExport = ''
}

New-ModuleManifest @params

psedit $PSD1

#endregion

#region PowerShellGet


Get-Module -Name PowerShellGet -ListAvailable
Get-Command -Module PowerShellGet

# Lists all available modules from all registered repositories
Find-Module

# www.powershellgallery.com is registered by default (untrusted)

Get-PSRepository

# Find and install modules
Find-Module -Name *FTP*
Find-Module -Name PSFTP | Install-Module
Install-Module -Name PSFTP
Find-Module -Name PS* | Out-GridView -OutputMode Multiple -Title 'Select modules to install' | Install-Module

# Register your own repositories. In this example we are using MyGet (www.myget.org) for hosting our internal gallery.
$CrayonRepositorySourceLocation = Get-Content -Path D:\GitHub\Crayon.repository.txt
$CrayonRepositoryPublishLocation = 'https://www.myget.org/F/crayon/api/v2/package'

# Register repository (once per user)
if(-not (Get-PSRepository -Name Crayon -ErrorAction SilentlyContinue))
{
Register-PSRepository -Name Crayon -SourceLocation $CrayonRepositorySourceLocation -PublishLocation $CrayonRepositoryPublishLocation -InstallationPolicy Trusted
}

Find-Module -Repository Crayon

# Publish the initial version of our DevOps module
$NuGetApiKey = Get-Content -Path D:\GitHub\Crayon.repository.NuGetApiKey.txt
Publish-Module -Path D:\GitHub\NIC.ServerDeployment -Repository Crayon -NuGetApiKey $NuGetApiKey

# After publishing, the module is visible to everyone who has registered the Crayon repository
Find-Module -Repository Crayon

Install-Module -Name NIC.ServerDeployment -Repository Crayon

Get-Command -Module NIC.ServerDeployment

# As an operator, I want to use one of the commands to create a virtual machine running Nano Server
$VM = New-NanoVM -VMName NICNanoVM01

# Inspect module to look for errors
Invoke-Item (Get-Module -Name NIC.ServerDeployment).ModuleBase

# Submit issue on GitHub

# After the developer has fixed the issue and published a new version we can update the module
Update-Module -Name NIC.ServerDeployment -Verbose


# Bonus tip: Simply create an SMB file share-based repository in your own environment. It's a one-liner!
# Just make sure you set up a file share first where you have read and write permissions. Next, run this (we used \\server1\Gallery for our file share, so make sure you adjust this part):
Register-PSRepository -Name MyTeam -SourceLocation \\server1\Gallery -InstallationPolicy Trusted

#endregion
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
# Toggle regions: Ctrl + M

#region Demo setup
Write-Warning 'This is a demo script which should be run line by line or sections at a time, stopping script execution'

break

<#
Author: Jan Egil Ring & Øyvind Kallstad, Crayon AS
Name: 03 - Pester.ps1
Description: This demo script is part of the presentation
DevOps for Ops - Automate All the Things!
#>

<#
What is Pester?
Pester provides a framework for running unit tests to execute and validate PowerShell commands from within PowerShell. Pester consists of a simple set of functions that expose a testing domain-specific language (DSL) for isolating, running, evaluating and reporting the results of PowerShell commands.
Pester tests can execute any command or script that is accessible to a Pester test file. This can include functions, cmdlets, modules and scripts. Pester can be run in ad-hoc style in a console or it can be integrated into the build scripts of a continuous integration (CI) system.
Pester also contains a powerful set of mocking functions in which tests mimic any command functionality within the tested PowerShell code.
https://github.com/pester/Pester
http://www.powershellmagazine.com/2014/03/12/get-started-with-pester-powershell-unit-testing-framework/
http://www.powershellmagazine.com/2014/03/27/testing-your-powershell-scripts-with-pester-assertions-and-more/
https://github.com/PowerShell/PowerShell/blob/master/docs/testing-guidelines/WritingPesterTests.md
https://github.com/pester/Pester/wiki
#>

Get-Module -ListAvailable -Name Pester

Find-Module -Name Pester

Get-Command -Module Pester

# This will run all tests inside of files named *.Tests.ps1 recursively from the current directory and print a report of all failing and passing test results to the console.
Invoke-Pester


# But first, let`s create a basic test-file
New-Item $env:temp\PesterDemo.tests.ps1

Set-Content -Path $env:temp\PesterDemo.tests.ps1 -Value @'
Describe "Prerequisites" {
It 'PowerShell version should be at least 4.0' {
$PSVersionTable.PSVersion.Major | Should BeGreaterThan 4
}
}
Describe "Properties" {
It 'Get-Service should contain a Status property' {
(Get-Service | Get-Member).Name -contains 'Status' | Should Be $true
}
}
'@

psedit $env:temp\PesterDemo.tests.ps1

cd $env:temp
Invoke-Pester

Invoke-Pester -TestName Prerequisites

Invoke-Pester -TestName Properties

Invoke-Pester -PassThru

$Tests = Invoke-Pester -PassThru

$Tests.FailedCount

if ($Tests.FailedCount -eq 0) {

#Do stuff

} else {

throw "Pester tests failed, aborting execution..."

}

#Setup tests for our demo module NIC.ServerDeployment

cd 'D:\GitHub\Crayon.Demo.DevOps\Module\0.3\Tests'

psedit .\New-NanoVM.Tests.ps1

Invoke-Pester

<#
Test Driven Development forces you think before you start scripting. If you are unable to write the tests, chances are you don’t fully understand the problem you are trying to solve.
#>


<#
Operation-Validation-Framework
A set of tools for executing validation of the operation of a system. It provides a way to organize and execute Pester tests which are written to validate operation (rather than limited feature tests)
Modules which include a Diagnostics directory are inspected for Pester tests in either the "Simple" or "Comprehensive" directories.
The module structure required is as follows:
ModuleBase\
Diagnostics\
Simple simple tests are held in this location (e.g., ping, serviceendpoint checks)
Comprehensive comprehensive scenario tests should be placed here
https://github.com/PowerShell/Operation-Validation-Framework
#>

Install-Module -Name OperationValidation

Get-Command *operationvalidation*

$TestsFile ='D:\GitHub\Crayon.Demo.DevOps\Scripts\Operational Validation\HyperV.Tests.ps1'
$OutputFile = 'D:\temp\Report.xml'

psedit $TestsFile

Invoke-OperationValidation -testFilePath $TestsFile -IncludePesterOutput
Invoke-OperationValidation -testFilePath $TestsFile

$test = Invoke-Pester -Script $TestsFile -OutputFile $OutputFile -OutputFormat NUnitXml -PassThru

& D:\temp\ReportUnit.exe $OutputFile

Invoke-Item D:\temp\Report.html

Invoke-Item D:\temp\Environment-Report.html

# Many options for consuming the report, for example: Copy-Item to a webserver or Send-MailMessage to yourself with a daily operational validation report

<#
It`s not practical to deploy a new VM for every time a test runs, so how can we solve that? A dedicated test environment? Seems overkill for this kind of testing.
Mocking with Pester
With the set of Mocking functions that Pester exposes, one can:
Mock the behavior of ANY powershell command.
Verify that specific commands were (or were not) called.
Verify the number of times a command was called with a set of specified parameters.
https://github.com/pester/Pester/wiki/Mocking-with-Pester
#>

Invoke-OperationValidation -ModuleName Crayon.Demo.DevOps -TestType Simple -IncludePesterOutput
Invoke-OperationValidation -testFilePath 'D:\GitHub\Crayon.Demo.DevOps\Module\Diagnostics\Simple\Crayon.Demo.DevOps.Simple.Tests.ps1' -IncludePesterOutput

#endregion
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Toggle regions: Ctrl + M

#region Demo setup
Write-Warning 'This is a demo script which should be run line by line or sections at a time, stopping script execution'

break

<#
Author: Jan Egil Ring & Øyvind Kallstad, Crayon AS
Name: 05 - AppVeyor.ps1
Description: This demo script is part of the presentation
DevOps for Ops - Automate All the Things!
#>




#region Demo 1

# 01 https://www.appveyor.com/
& 'C:\Program Files (x86)\Google\Chrome\Application\chrome.exe' 'https://www.appveyor.com/'

# 02 - Add new project

# 03 Update appveyor.yml
$path = '~\Documents\GitHub\Crayon.Demo.DevOps\Module\0.3\*'
$destination = '~\Documents\GitHub\NIC.ServerDeployment'
Copy-Item -Path $path -Destination $destination -Recurse -Force
Loading

0 comments on commit 3154a70

Please sign in to comment.