forked from brianary/scripts
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Import-Variables.ps1
66 lines (54 loc) · 2.33 KB
/
Import-Variables.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<#
.SYNOPSIS
Creates local variables from a data row or dictionary (hashtable).
.INPUTS
System.Collections.IDictionary with keys and values to import as variables,
or System.Management.Automation.PSCustomObject with properties to import as variables.
.FUNCTIONALITY
PowerShell
.LINK
Add-ScopeLevel.ps1
.EXAMPLE
if($line -match '\AProject\("(?<TypeGuid>[^"]+)"\)') {Import-Variables.ps1 $Matches}
Copies $Matches.TypeGuid to $TypeGuid if a match is found.
.EXAMPLE
Invoke-Sqlcmd "select ProductID, Name, ListPrice from Production.Product where ProductID = 1;" -Server 'Server\instance' -Database AdventureWorks |Import-Variables.ps1
Copies field values into $ProductID, $Name, and $ListPrice.
.EXAMPLE
if($env:ComSpec -match '^(?<ComPath>.*?\\)(?<ComExe>[^\\]+$)'){Import-Variables.ps1 $Matches -Verbose}
Sets $ComPath and $ComExe from the regex captures if the regex matches.
.EXAMPLE
Invoke-RestMethod https://api.github.com/ |Import-Variables.ps1 ; Invoke-RestMethod $emojis_url
Sets variables from the fields returned by the web service: $current_user_url, $emojis_url, &c.
Then fetches the list of GitHub emojis.
#>
#Requires -Version 3
[CmdletBinding()][OutputType([void])] Param(
<#
A hash of string names to any values to set as variables,
or a DataRow or object with properties to set as variables.
Works with DataRows.
#>
[Parameter(Position=0,Mandatory=$true,ValueFromPipeline=$true)][PSObject] $InputObject,
# The type of object members to convert to variables.
[Alias('Type')][Management.Automation.PSMemberTypes] $MemberType = 'Properties',
# The scope of the variables to create.
[string] $Scope = 'Local',
# Indicates that created variables should be hidden from child scopes.
[switch] $Private
)
Begin
{
$Scope = Add-ScopeLevel.ps1 $Scope
$sv = if($Private) {@{Scope=$Scope;Option='Private'}} else {@{Scope=$Scope}}
}
Process
{
$isDict = $InputObject -is [Collections.IDictionary]
[string[]]$vars =
if($isDict) {$InputObject.Keys |Where-Object {$_ -is [string]}}
else {Get-Member -InputObject $InputObject -MemberType $MemberType |Select-Object -ExpandProperty Name}
if(!$vars){return}
Write-Verbose "Importing $($vars.Count) $(if($isDict){'keys'}else{"$MemberType properties"}): $vars"
foreach($var in $vars) {Set-Variable $var $InputObject.$var @sv}
}