MilestoneSystemsInc / PowerShellSamples

A collection of samples for managing your Milestone XProtect VMS using MilestonePSTools in PowerShell
https://www.milestonepstools.com
MIT License
36 stars 12 forks source link

Contributors Forks Stargazers Issues MIT License


Logo

Manage Milestone with PowerShell

A collection of sample scripts to bootstrap your Milestone XProtect VMS Management Adventure
Explore the samples »

Report Bug · Request Feature

Table of Contents

About The Project

This repository contains sample PowerShell scripts for managing and automating tasks related to Milestone XProtect VMS software using MilestonePSTools. It is intended to be a supplement to the built-in documentation available in many cmdlets within MilestonePSTools which you can access using Get-Help.

[Product Name Screen Shot]()

Getting Started

In order to make the most out of MilestonePSTools you will need at least some basic familiarity with PowerShell or other shells. It's possible to perform very basic actions in a few lines of PowerShell with limited experience. With a bit more experience and ambition, it's also possible to develop extensive and powerfull tools or entire modules to automate important business and maintenance tasks unique to your organization.

IMAGE ALT TEXT

If you're new to PowerShell, there are a wide range of resources available to learn. One popular example is the Learn Windows PowerShell in a Month of Lunches series which, while last updated in 2014, is still a good resource for picking up the syntax and style.

Prerequisites

MilestonePSTools is developed using the latest build of Milestone's MIP SDK. As such, it has the following prerequisits:

Installation

You can install MilestonePSTools on the computer where your Milestone VMS is installed, but that is not required. In fact, from a security standpoint it is best to avoid installing software on the same server as your VMS if it's not necessary. Just as the XProtect Smart Client and Management Client may be used from a networked PC, so too can MilestonePSTools be used from a remote system.

You can copy & paste the script below into a PowerShell prompt, or PowerShell ISE, and as long as you have PowerShell 5.1 installed, it should get you up and running.

$script = @"
Write-Host 'Setting SecurityProtocol to TLS 1.2, Execution Policy to RemoteSigned' -ForegroundColor Green
[Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Confirm:`$false -Force

Write-Host 'Registering the NuGet package source if necessary' -ForegroundColor Green
if (`$null -eq (Get-PackageSource -Name NuGet -ErrorAction Ignore)) {
    `$null = Register-PackageSource -Name NuGet -Location https://www.nuget.org/api/v2 -ProviderName NuGet -Trusted -Force
}

Write-Host 'Installing the NuGet package provider' -ForegroundColor Green
`$nugetProvider = Get-PackageProvider -Name NuGet -ErrorAction Ignore
`$requiredVersion = [Microsoft.PackageManagement.Internal.Utility.Versions.FourPartVersion]::Parse('2.8.5.201')
if (`$null -eq `$nugetProvider -or `$nugetProvider.Version -lt `$requiredVersion) {
    `$null = Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force
}

Write-Host 'Setting PSGallery as a trusted repository' -ForegroundColor Green
Set-PSRepository -Name PSGallery -InstallationPolicy Trusted

Write-Host 'Installing PowerShellGet 2.2.5 or greater if necessary' -ForegroundColor Green
if (`$null -eq (Get-Module -ListAvailable PowerShellGet | Where-Object Version -ge 2.2.5)) {
    `$null = Install-Module PowerShellGet -MinimumVersion 2.2.5 -Force
}

Write-Host 'Installing or updating MilestonePSTools' -ForegroundColor Green
if (`$null -eq (Get-Module -ListAvailable MilestonePSTools)) {
    Install-Module MilestonePSTools
}
else {
    Update-Module MilestonePSTools
}
"@
Start-Process -FilePath powershell.exe -ArgumentList "-Command $script" -Verb RunAs

Holy moly that's a lot of PowerShell

The script above may look intimidating, and you should never copy and paste code of any kind if you don't know what it does. In short, the script is designed to make it as easy and fast as possible to get MilestonePSTools installed. Sometimes your PowerShell environment needs a few adjustments and updates to be able to install PowerShell modules. Let's break down what this actually does, just in case your boss asks!

  1. A script is launched in a new PowerShell instance requiring elevation
  2. Sets the HTTPS security protocol to TLS 1.2 which is required for communicating with PSGallery
  3. Sets the execution policy to RemoteSigned. The default on Windows 10 is Restricted and that will prevent you from actually using any PowerShell modules. RemoteSigned means any scripts that are "blocked" must be signed.
  4. Registers NuGet.org as a package source. This is not strictly necessary, but can be useful if you need to install .NET packages from PowerShell, and in the future we may update MilestonePSTools to depend on the official MIP SDK NuGet packages.
  5. Installs the NuGet package provider which is used by PowerShellGet as a source for installing PowerShell modules. You may have an older version of PowerShellGet so this will ensure it gets updated.
  6. Sets the PSGallery repository as a trusted source for PowerShell modules. This is not mandatory but will prevent you from having to acknowledge each module you install.
  7. Updates to the latest PowerShellGet module
  8. Installs/updates MilestonePSTools to C:\Program Files\WindowsPowerShell\Modules\

Usage

Get connected

List all enabled cameras

# Slower but uses Configuration API so each object can be used to modify configuration
Get-Hardware | Where-Object Enabled | Get-Camera | Where-Object Enabled | Select Name

# Faster but provides limited access to camera properties and Items cannot be used to modify configuration
Get-PlatformItem -Kind (Get-Kind -List | ? DisplayName -eq Camera).Kind | Select Name

Add hardware

Save a live snapshot from all cameras

$cameras = Get-Hardware | ? Enabled | Get-Camera | ? Enabled
foreach ($camera in $cameras) {
    $null = Get-Snapshot -Camera $camera -Live -Save -Path C:\demo\snapshots -UseFriendlyName -LocalTimestamp
}

Roadmap

See the open issues for a list of proposed features (and known issues).

Contributing

Contributions are what make the open source community such an amazing place to be learn, inspire, and create. Any contributions you make are greatly appreciated.

  1. Fork the Project
  2. Create your Feature Branch (git checkout -b feature/AmazingFeature)
  3. Commit your Changes (git commit -m 'Add some AmazingFeature')
  4. Push to the Branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

License

Distributed under the MIT License. See LICENSE for more information.

Contact

Josh Hendricks - LinkedIn - jh@milestonesys.com

Acknowledgements