Skip to content

Commit 114dd16

Browse files
committed
mutcho updato, started adding tests
1 parent 22dac5d commit 114dd16

10 files changed

Lines changed: 285 additions & 60 deletions

Functions/Assert-ScriptString.ps1

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#Requires -Version 4.0
2+
function Assert-ScriptString
3+
{
4+
<#
5+
.SYNOPSIS
6+
Validate a scriptblock before execution into current runspace
7+
8+
.DESCRIPTION
9+
Validate a string before execution into current runspace. The only allowed allowed
10+
command/cmdlet is New-Date included in this module. The string is converted to a scriptblock
11+
and is only executed if the validation passes.
12+
13+
.PARAMETER Data
14+
The string that is to be validated for restricted language.
15+
16+
.EXAMPLE
17+
$string = @"
18+
@{
19+
Name = "Tore"
20+
Goal = "Rule the World"
21+
}
22+
"@
23+
$scriptOutput = Assert-ScriptString -Data $string
24+
25+
.INPUTS
26+
String
27+
28+
.OUTPUTS
29+
30+
31+
.NOTES
32+
Author: Tore Groneng
33+
Website: www.firstpoint.no
34+
Twitter: @ToreGroneng
35+
#>
36+
[cmdletbinding()]
37+
Param(
38+
[Parameter(ValueFromPipeline)]
39+
[string]
40+
$Data
41+
)
42+
$previousErrorAction = $ErrorActionPreference
43+
$ErrorActionPreference = "Stop"
44+
try
45+
{
46+
$script = [scriptblock]::Create($data)
47+
[string[]]$allowedCommands = @("New-Date")
48+
[string[]]$allowedVariables = @()
49+
50+
$script.CheckRestrictedLanguage($allowedCommands, $allowedVariables, $false)
51+
52+
& $script
53+
}
54+
catch
55+
{
56+
if ($previousErrorAction -ne "SilentlyContinue")
57+
{
58+
Write-Error -Exception $_.Exception -ErrorAction Stop
59+
}
60+
}
61+
Finally
62+
{
63+
$ErrorActionPreference = $previousErrorAction
64+
}
65+
}

Functions/ConvertTo-HashString.ps1

Lines changed: 19 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#Requires -Version 4.0
12
function ConvertTo-HashString
23
{
34
<#
@@ -21,10 +22,7 @@ function ConvertTo-HashString
2122
$hashObject | ConvertTo-HashString
2223
2324
This will convert the hashtable to the following string
24-
@{
25-
Name = "Tore"
26-
Goal = "Rule the World"
27-
}
25+
@{Name = "Tore";Goal = "Rule the World";}
2826
2927
.INPUTS
3028
Hashtable
@@ -41,29 +39,15 @@ function ConvertTo-HashString
4139
Param(
4240
[Parameter(ValueFromPipeLine)]
4341
$InputObject
44-
,
45-
[string]$PreSpacing
4642
)
4743
Begin
4844
{
4945
$f = $MyInvocation.InvocationName
5046
Write-Verbose -Message "$f - START"
51-
$newLine = [environment]::NewLine
52-
53-
if ($PreSpacing)
54-
{
55-
$endspaceCount = $PreSpacing.Length - 4
56-
if ($endspaceCount -lt 0){$endspaceCount = 0}
57-
$endspace = " " * $endspaceCount
58-
$beginSpace = " " * $endspaceCount
59-
}
6047
}
6148
Process
6249
{
6350
$out = "@{"
64-
65-
$out = $out #+ $newLine
66-
$preSpace = $PreSpacing
6751

6852
if (-not $InputObject -or $InputObject.keys.count -eq 0) {return "@{}"}
6953

@@ -112,7 +96,7 @@ Process
11296
$DisplayKey = $null
11397
}
11498

115-
$out += "$PreSpacing$key = "
99+
$out += "$key = "
116100

117101
switch ($mode)
118102
{
@@ -129,26 +113,26 @@ Process
129113
$ticks = $value.Ticks
130114
$out += "New-Date $ticks;"
131115
}
132-
else {
133-
$out += '"' + $value + '";'# + $newLine
134-
}
135-
116+
else
117+
{
118+
$out += '"' + $value + '";'
119+
}
136120
}
137121

138122
'hashtable'
139123
{
140-
$stringValue = ConvertTo-HashString -InputObject $value #-PreSpacing "$PreSpacing "
141-
$out += $stringValue + ";" #+ $newLine
124+
$stringValue = ConvertTo-HashString -InputObject $value
125+
$out += $stringValue + ";"
142126
}
143127

144128
'HashTableValue'
145129
{
146130
$stringValue = ""
147131
foreach ($arrayHash in $value)
148132
{
149-
$hashString = ConvertTo-HashString -InputObject $arrayHash #-PreSpacing "$PreSpacing "
133+
$hashString = ConvertTo-HashString -InputObject $arrayHash
150134
$hash = "$hashString"
151-
$hash = "$hash,"# + $newLine
135+
$hash = "$hash,"
152136
$stringValue += $hash
153137
}
154138
$separatorIndex = $stringValue.LastIndexOf(",")
@@ -162,16 +146,19 @@ Process
162146
{
163147
$out += ($value -join ",") + ";"
164148
}
165-
else {
166-
$out += '"' +($value -join '","') + '";' #+ $newLine
149+
else
150+
{
151+
$out += '"' +($value -join '","') + '";'
167152
}
168153

169154
}
170155

171-
Default {}
156+
default
157+
{
158+
Write-Error -Message "Invalid mode in funtion $f"
159+
}
172160
}
173-
}
174-
#$out += "$endspace}"
161+
}
175162
$out += "}"
176163
$out
177164
}

Functions/ConvertTo-Hashtable.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#Requires -Version 5.0
1+
#Requires -Version 4.0
22
function ConvertTo-Hashtable
33
{
44
<#
@@ -25,7 +25,7 @@ function ConvertTo-Hashtable
2525
PSCustomObject
2626
2727
.OUTPUTS
28-
hashtable
28+
System.Collections.Specialized.OrderedDictionary
2929
3030
.NOTES
3131
Author: Tore Groneng

Functions/Export-HashData.ps1

Lines changed: 70 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,64 @@
1+
#Requires -Version 4.0
12
function Export-HashData
23
{
3-
[cmdletbinding()]
4+
<#
5+
.SYNOPSIS
6+
Serialize an object to a file as an HashTable.
7+
8+
.DESCRIPTION
9+
Serialize an object to a file as an HashTable. The majority of the types of the object
10+
is preserved in the output. To handle datetime, the module have a function New-Date that
11+
only can create a date from a long tick value.
12+
13+
.PARAMETER Path
14+
The target file that will store the Specialized object
15+
16+
.PARAMETER InputObject
17+
The object that should be Serialized.
18+
19+
.PARAMETER Append
20+
Append the Serialized object to the file.
21+
22+
.EXAMPLE
23+
$obj = [pscustomobject]@{
24+
Name = "Tore"
25+
Age = 45
26+
CodeCoverage = 11.21
27+
Date = (Get-Date)
28+
IntArray = 1,2,3
29+
}
30+
31+
Export-HashData -Path C:\Temp\hash.txt -InputObject $obj
32+
33+
This will serialize the $obj variable and save this string in the file hash.txt
34+
@{Name = "Tore";Age = 45;CodeCoverage = 11.21;Date = New-Date 636221857704582734;IntArray = 1,2,3;}
35+
36+
.INPUTS
37+
38+
.OUTPUTS
39+
40+
.NOTES
41+
Author: Tore Groneng
42+
Website: www.firstpoint.no
43+
Twitter: @ToreGroneng
44+
#>
45+
[cmdletbinding(
46+
SupportsShouldProcess=$true,
47+
ConfirmImpact='Medium'
48+
)]
449
Param(
550
[string]
651
$Path
752
,
53+
[Parameter(ValueFromPipeline)]
854
$InputObject
955
,
1056
[switch]
1157
$Append
58+
,
59+
[switch]
60+
$Force
1261
)
13-
#FixMe Implement shouldprocess
1462
if ($Append.IsPresent)
1563
{
1664
if (-not (Test-Path -Path $Path))
@@ -38,13 +86,30 @@ Param(
3886
Value = $fileContent
3987
Encoding = "UTF8"
4088
}
41-
89+
90+
$shouldProcessOperation = "Creating file"
91+
if ($Force.IsPresent)
92+
{
93+
$file.Add("Force", $true)
94+
$shouldProcessOperation = "Overwriting file"
95+
}
96+
4297
if ($Append.IsPresent)
4398
{
44-
Add-Content @file
99+
if ($PSCmdlet.ShouldProcess("$Path", "Append to file"))
100+
{
101+
if (-not (Test-Path -Path $Path))
102+
{
103+
Set-Content -Path $Path -Value $null -Encoding UTF8
104+
}
105+
Add-Content @file
106+
}
45107
}
46108
else
47109
{
48-
Set-Content @file
110+
if ($PSCmdlet.ShouldProcess("$Path", "$shouldProcessOperation"))
111+
{
112+
Set-Content @file
113+
}
49114
}
50115
}

Functions/Import-HashData.ps1

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,37 @@
1+
#Requires -Version 4.0
12
function Import-HashData
23
{
4+
<#
5+
.SYNOPSIS
6+
Import serialized objects from disk.
7+
8+
.DESCRIPTION
9+
Import serialized objects from disk. The import will check the content of the file
10+
for RestrictedLanguage. Only New-Date function is allowed in the script. Other commands
11+
will result in an error and the import will fail.
12+
13+
.PARAMETER Path
14+
The target file that contains the Specialized object(s).
15+
16+
.PARAMETER UnsafeMode
17+
If supplied, no you allow any command to be executed in the runspace when the object is deserialized.
18+
19+
.EXAMPLE
20+
$objects = Import-Hashdata -Path c:\temp\Hash.txt
21+
22+
This will deserialize the content/objects in the hash.txt file
23+
24+
.INPUTS
25+
String
26+
27+
.OUTPUTS
28+
hashtable
29+
30+
.NOTES
31+
Author: Tore Groneng
32+
Website: www.firstpoint.no
33+
Twitter: @ToreGroneng
34+
#>
335
[cmdletbinding()]
436
Param(
537
[string]
@@ -16,11 +48,12 @@ Param(
1648
$data = Get-Content -Path $path -Encoding UTF8 -Raw -ReadCount 0
1749
if ($UnsafeMode.IsPresent)
1850
{
51+
Write-Warning -Message "You are importing persisted data without cheching RestrictedLanguage because you supplied the UnSafeMode switch."
1952
$script = [scriptblock]::Create($data)
2053
& $script
2154
}
2255
else
2356
{
24-
Import-PersistedData -Data $data -ErrorAction Stop
57+
Assert-PersistedData -Data $data -ErrorAction Stop
2558
}
2659
}

Functions/Import-PersistedData.ps1

Lines changed: 0 additions & 20 deletions
This file was deleted.

Functions/New-Date.ps1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#Requires -Version 4.0
12
function New-Date
23
{
34
[cmdletbinding()]

HashData.psd1

-3.75 KB
Binary file not shown.

0 commit comments

Comments
 (0)