Skip to content
This repository was archived by the owner on Apr 18, 2020. It is now read-only.

PowerShell Scripts

Jaime Still edited this page Sep 9, 2016 · 7 revisions

For detailed information on PowerShell development, see Getting Started with Windows PowerShell.

Before adding scripts to the solution, they should be written and tested in the PowerShell IDE provided by the Windows Management Framework. This way, the script can be tested in real time apart from the solution. Once the script is in working order, you might have something like the following that executes WMI queries on remote workstations in a domain:

$credential = Get-Credential

$properties = (Read-Host 'Set Select-Object Details') -split ' +|, *'
$query = Read-Host 'Input Test Query'

$wmi = Get-WmiObject -ComputerName SomeComputerName -Namespace root\cimv2 
    -Query $query -Credential $credential

$wmi | select $properties

In order for this script to work in the framework, any parameters that should be set by the user or would change depending on the execution scenario should be parameterized. In the above script, the following data should be parameterized:

$credential
$properties
$query
$computername
$wmiNamespace

This would look like the following:

[CmdletBinding()]
Param(
    [Parameter()]
    [string]$query,
    [Parameter()]
    [string[]]$properties,
    [Parameter()]
    [string]$computername,
    [Parameter()]
    [string]$wmiNamespace,
    [Parameter()]
    [PSCredential]$credential
)
$wmi = Get-WmiObject -ComputerName $computername -Namespace $wmiNamespace 
    -Query $query -Credential $credential

$wmi | select $properties

Now, when writing the extension method that executes the script, the CLR objects that define the parameters can be set against the Command class that contains the script. Move the script into the Scripts folder in the .Core project, open the properties for the file, and set Build Action: Embedded Resource.

Clone this wiki locally