Skip to content

Commit e0847d5

Browse files
committed
Implemented ShouldProcess
Make pretty Fix help typo
1 parent 4a7f2bf commit e0847d5

7 files changed

Lines changed: 87 additions & 51 deletions

File tree

Functions/Assert-ScriptString.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ function Assert-ScriptString
3434
Twitter: @ToreGroneng
3535
#>
3636
[cmdletbinding()]
37-
Param(
37+
Param (
3838
[Parameter(ValueFromPipeline)]
3939
[string]
4040
$Data

Functions/ConvertTo-HashString.ps1

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@ function ConvertTo-HashString
1111
.PARAMETER InputObject
1212
The object that is to be converted
1313
14-
.PARAMETER PreSpacing
15-
Number of spaces used for indentation
16-
1714
.EXAMPLE
1815
$hashObject = @{
1916
Name = "Tore"
@@ -36,15 +33,17 @@ function ConvertTo-HashString
3633
Twitter: @ToreGroneng
3734
#>
3835
[cmdletbinding()]
39-
Param(
36+
Param (
4037
[Parameter(ValueFromPipeLine)]
4138
$InputObject
4239
)
40+
4341
Begin
4442
{
4543
$f = $MyInvocation.InvocationName
4644
Write-Verbose -Message "$f - START"
4745
}
46+
4847
Process
4948
{
5049
$out = "@{"
@@ -162,6 +161,11 @@ Process
162161
$out += "}"
163162
$out
164163
}
164+
165+
End
166+
{
167+
Write-Verbose -Message "$f - End"
168+
}
165169
}
166170

167171

Functions/ConvertTo-Hashtable.ps1

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,20 @@ function ConvertTo-Hashtable
3333
Twitter: @ToreGroneng
3434
#>
3535
[cmdletbinding()]
36-
Param(
36+
Param (
3737
[Parameter(ValueFromPipeline)]
3838
[PSCustomObject]$InputObject
3939
)
40-
Begin{
40+
41+
Begin
42+
{
4143
$f = $MyInvocation.InvocationName
4244
Write-Verbose -Message "$f - START"
4345
}
46+
4447
Process
4548
{
46-
Write-Verbose -Message "$F - processing $($inputobject.GetType().Name)"
49+
Write-Verbose -Message "$F - Processing [$($inputobject.GetType().Name)]"
4750
if ($InputObject -is [array])
4851
{
4952
Write-Verbose -Message "Is array object"
@@ -79,7 +82,7 @@ Process
7982

8083
if ($value -is [array])
8184
{
82-
Write-Verbose -Message "Is array value"
85+
Write-Verbose -Message "$f - Value is array"
8386
$hashValue = @()
8487
if ($value[0] -is [hashtable] -or $value[0] -is [System.Collections.Specialized.OrderedDictionary] -or $value[0] -is [PSCustomObject])
8588
{
@@ -99,4 +102,9 @@ Process
99102
}
100103
$hash
101104
}
105+
106+
End
107+
{
108+
Write-Verbose -Message "$f - END"
109+
}
102110
}

Functions/Export-HashData.ps1

Lines changed: 28 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ function Export-HashData
1111
only can create a date from a long tick value.
1212
1313
.PARAMETER Path
14-
The target file that will store the Specialized object
14+
The target file that will store the serialized object
1515
1616
.PARAMETER InputObject
1717
The object that should be Serialized.
@@ -43,10 +43,10 @@ function Export-HashData
4343
Twitter: @ToreGroneng
4444
#>
4545
[cmdletbinding(
46-
SupportsShouldProcess=$true,
47-
ConfirmImpact='Medium'
46+
SupportsShouldProcess=$true,
47+
ConfirmImpact='High'
4848
)]
49-
Param(
49+
Param (
5050
[string]
5151
$Path
5252
,
@@ -55,20 +55,19 @@ Param(
5555
,
5656
[switch]
5757
$Append
58-
,
59-
[switch]
60-
$Force
6158
)
62-
if ($Append.IsPresent)
63-
{
64-
if (-not (Test-Path -Path $Path))
65-
{
66-
Set-Content -Path $Path -Value $null -Encoding UTF8
67-
}
68-
}
6959

60+
Begin
61+
{
62+
$f = $MyInvocation.InvocationName
63+
Write-Verbose -Message "$f - START"
64+
}
65+
66+
Process
67+
{
7068
$fileContent = ""
7169

70+
Write-Verbose -Message "$f - Converting inputobject to string"
7271
if ($InputObject -is [hashtable] -or $InputObject -is [System.Collections.Specialized.OrderedDictionary])
7372
{
7473
$fileContent = $InputObject | ConvertTo-HashString
@@ -85,31 +84,29 @@ Param(
8584
Path = $Path
8685
Value = $fileContent
8786
Encoding = "UTF8"
88-
}
89-
90-
$shouldProcessOperation = "Creating file"
91-
if ($Force.IsPresent)
92-
{
93-
$file.Add("Force", $true)
94-
$shouldProcessOperation = "Overwriting file"
95-
}
87+
}
9688

89+
9790
if ($Append.IsPresent)
98-
{
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-
}
91+
{
92+
if ($PSCmdlet.ShouldProcess("$Path", "$f - Append to file"))
93+
{
94+
Write-Verbose -Message "$f - Appending to file [$Path]"
10595
Add-Content @file
10696
}
10797
}
10898
else
109-
{
110-
if ($PSCmdlet.ShouldProcess("$Path", "$shouldProcessOperation"))
99+
{
100+
if ($PSCmdlet.ShouldProcess("$Path", "$f - Overwriting file"))
111101
{
102+
Write-Verbose -Message "$f - Writing to file [$Path]"
112103
Set-Content @file
113104
}
114105
}
106+
}
107+
108+
End
109+
{
110+
Write-Verbose -Message "$f - END"
111+
}
115112
}

Functions/Import-HashData.ps1

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ function Import-HashData
1111
will result in an error and the import will fail.
1212
1313
.PARAMETER Path
14-
The target file that contains the Specialized object(s).
14+
The target file that contains the Serialized object(s).
1515
1616
.PARAMETER UnsafeMode
1717
If supplied, no you allow any command to be executed in the runspace when the object is deserialized.
@@ -32,28 +32,55 @@ function Import-HashData
3232
Website: www.firstpoint.no
3333
Twitter: @ToreGroneng
3434
#>
35-
[cmdletbinding()]
36-
Param(
35+
[cmdletbinding(
36+
SupportsShouldProcess=$true,
37+
ConfirmImpact='medium'
38+
)]
39+
Param (
3740
[string]
3841
$Path
3942
,
4043
[switch]
4144
$UnsafeMode
4245
)
46+
$f = $MyInvocation.InvocationName
47+
Write-Verbose -Message "$f - START"
48+
4349
if (-not (Test-Path -Path $Path))
4450
{
4551
Write-Error -Message "Unable to find file [$path]" -ErrorAction Stop
4652
}
4753

48-
$data = Get-Content -Path $path -Encoding UTF8 -Raw -ReadCount 0
54+
Write-Verbose -Message "$f - Importing data from [$path]"
55+
56+
[string]$data = Get-Content -Path $path -Encoding UTF8 -Raw -ReadCount 0
57+
4958
if ($UnsafeMode.IsPresent)
5059
{
51-
Write-Warning -Message "You are importing persisted data without cheching RestrictedLanguage because you supplied the UnSafeMode switch."
52-
$script = [scriptblock]::Create($data)
53-
& $script
60+
#fixme this is a hack
61+
$PreviousConfirmPreference = $ConfirmPreference
62+
$ConfirmPreference = "low"
63+
if ($PScmdlet.ShouldProcess($path, "Unsafe script invokation"))
64+
{
65+
$ConfirmPreference = $PreviousConfirmPreference
66+
Write-Warning -Message "You are importing persisted data without cheching RestrictedLanguage because you supplied the UnSafeMode switch."
67+
$script = [scriptblock]::Create($data)
68+
& $script
69+
}
5470
}
5571
else
5672
{
57-
Assert-PersistedData -Data $data -ErrorAction Stop
73+
#fixme this is a hack
74+
$PreviousConfirmPreference = $ConfirmPreference
75+
$ConfirmPreference = "high"
76+
77+
if ($PScmdlet.ShouldProcess($path, "Safe script invokation"))
78+
{
79+
$ConfirmPreference = $PreviousConfirmPreference
80+
Write-Verbose -Message "$f - Asserting script content"
81+
Assert-ScriptString -Data $data -ErrorAction Stop
82+
}
5883
}
84+
85+
Write-Verbose -Message "$f - END"
5986
}

Functions/New-Date.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
function New-Date
33
{
44
[cmdletbinding()]
5-
Param(
5+
Param (
66
[Parameter(Position=0)]
77
[int64]
88
$Ticks

HashData.psd1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
RootModule = 'HashData.psm1'
1313

1414
# Version number of this module.
15-
ModuleVersion = '0.1.0.0'
15+
ModuleVersion = '1.0.0.0'
1616

1717
# Supported PSEditions
1818
# CompatiblePSEditions = @()
@@ -107,7 +107,7 @@ PrivateData = @{
107107
# IconUri = ''
108108

109109
# ReleaseNotes of this module
110-
# ReleaseNotes = ''
110+
ReleaseNotes = 'This is a release candidate.'
111111

112112
} # End of PSData hashtable
113113

0 commit comments

Comments
 (0)