-
Notifications
You must be signed in to change notification settings - Fork 0
PowerShell Scripts
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 $propertiesIn 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 $propertiesNow, 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.