Skip to content

Commit b0cbe9b

Browse files
fixing object conversion
Also adding tests, so this doesn't happen again
1 parent 5281c03 commit b0cbe9b

File tree

10 files changed

+81
-13
lines changed

10 files changed

+81
-13
lines changed

PSModuleDevelopment/PSModuleDevelopment.psd1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
# Version number of this module.
77

8-
ModuleVersion = '2.2.12.171'
8+
ModuleVersion = '2.2.12.172'
99

1010
# ID used to uniquely identify this module
1111
GUID = '37dd5fce-e7b5-4d57-ac37-832055ce49d6'
@@ -29,7 +29,7 @@
2929
# this module
3030
RequiredModules = @(
3131
@{ ModuleName = 'PSFramework'; ModuleVersion = '1.12.346' }
32-
@{ ModuleName = 'string'; ModuleVersion = '1.1.3' }
32+
@{ ModuleName = 'string'; ModuleVersion = '1.1.5' }
3333
)
3434

3535
# Assemblies that must be loaded prior to importing this module
0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.

PSModuleDevelopment/bin/PSModuleDevelopment.xml

Lines changed: 10 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

PSModuleDevelopment/changelog.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## 2.2.12.172 (2024-10-06)
4+
5+
+ Fix: Invoke-PSMDTemplate - On WinPS, script-logic on templates is not executed
6+
37
## 2.2.12.171 (2024-10-04)
48

59
+ Upd: Raised PSFramework dependency due to critical security update
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
Describe "Verifying templating component" {
2+
BeforeAll {
3+
$tmpFolder = New-PSFTempDirectory -Name Template -ModuleName PSModuleDevelopment
4+
$resourcePath = Resolve-PSFPath -Path "$PSScriptRoot\..\resources"
5+
$templateName = 'TestTemplate-{0}' -f (Get-Random)
6+
}
7+
AfterAll {
8+
Write-Host "Path: $resourcePath"
9+
Remove-PSFTempItem -Name Template -ModuleName PSModuleDevelopment
10+
}
11+
12+
It "Should Record the template correctly" {
13+
{ New-PSMDTemplate -TemplateName $templateName -FilePath "$resourcePath\þnameþ.txt" -EnableException -ErrorAction Stop } | Should -Not -Throw
14+
$templateInfo = Get-PSMDTemplate -TemplateName $templateName
15+
$templateRaw = Import-PSFClixml -Path $templateInfo.Path
16+
$template = [PSModuleDevelopment.Template.Template]$templateRaw
17+
$template.Name | Should -Be $templateName
18+
$template.Parameters.Count | Should -Be 1
19+
$template.Scripts.Count | Should -Be 1
20+
$template.Scripts.Values.ScriptBlock | Should -BeOfType ([scriptblock])
21+
}
22+
23+
It "Should Invoke the template correctly" {
24+
{ Invoke-PSMDTemplate -TemplateName $templateName -OutPath $tmpFolder -Name Test -EnableException } | Should -Not -Throw
25+
$content = Get-Content -Path "$tmpFolder\Test.txt" -ErrorAction Stop
26+
$values = $content | ConvertFrom-StringData -ErrorAction Stop
27+
$values.Name | Should -Be Test
28+
$values.Value | Should -Be '123'
29+
}
30+
31+
It "Should Remove the template correctly" {
32+
{ Remove-PSMDTemplate -TemplateName $templateName -EnableException -Confirm:$false } | Should -Not -Throw
33+
Get-PSMDTemplate -TemplateName $templateName | Should -BeNullOrEmpty
34+
}
35+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Name = þnameþ
2+
Value = þ{ "123" }þ

PSModuleDevelopment/xml/PSModuleDevelopment.Types.ps1xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<Types>
33
<!-- PSModuleDevelopment.Template.ParameterScript -->
4-
<Type>
4+
<!-- <Type>
55
<Name>Deserialized.PSModuleDevelopment.Template.ParameterScript</Name>
66
<Members>
77
<MemberSet>
@@ -33,7 +33,7 @@
3333
<TypeConverter>
3434
<TypeName>PSFramework.Serialization.SerializationTypeConverter</TypeName>
3535
</TypeConverter>
36-
</Type>
36+
</Type> -->
3737

3838
<!-- PSModuleDevelopment.Template.Template -->
3939
<Type>

library/PSModuleDevelopment/PSModuleDevelopment/Template/Template.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,14 +122,14 @@ public Template(PSObject Item)
122122
Description = Item.GetValue<string>("Description");
123123
Author = Item.GetValue<string>("Author");
124124
CreatedOn = Item.GetValue<DateTime>("CreatedOn");
125-
foreach (object item in Item.GetValue<ArrayList>("Tags"))
125+
foreach (object item in Item.GetValues("Tags"))
126126
Tags.Add((string)item);
127-
foreach (object item in Item.GetValue<ArrayList>("Parameters"))
127+
foreach (object item in Item.GetValues("Parameters"))
128128
Parameters.Add((string)item);
129-
foreach (KeyValuePair<string, ParameterScript> entry in Item.GetDictionary<ParameterScript>("Scripts"))
129+
foreach (KeyValuePair<string, ParameterScript> entry in Item.GetParameterScriptDictionary("Scripts"))
130130
Scripts[entry.Key] =entry.Value;
131131
// Parameters2 not used
132-
foreach (object item in Item.GetValue<ArrayList>("Children"))
132+
foreach (object item in Item.GetValues("Children"))
133133
Children.Add(TemplateHost.GetTemplateItem(item));
134134

135135
Generation = Item.GetValue<int>("Generation");

library/PSModuleDevelopment/PSModuleDevelopment/Utility/PSObjectExtension.cs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System;
1+
using PSModuleDevelopment.Template;
2+
using System;
23
using System.Collections;
34
using System.Collections.Generic;
45
using System.Linq;
@@ -26,19 +27,36 @@ public static T GetValue<T>(this PSObject PSObject, string Name)
2627
return (T)value.BaseObject;
2728
}
2829

30+
/// <summary>
31+
/// Get the Values!
32+
/// Always as an arraylist, empty if null
33+
/// </summary>
34+
/// <param name="PSObject">The object to extend</param>
35+
/// <param name="Name">>The name of the property that has the values</param>
36+
/// <returns>The values</returns>
37+
public static ArrayList GetValues(this PSObject PSObject, string Name)
38+
{
39+
if (null == PSObject.Properties[Name].Value)
40+
return new ArrayList();
41+
PSObject value = PSObject.AsPSObject(PSObject.Properties[Name].Value);
42+
if (null == value.BaseObject)
43+
return new ArrayList();
44+
return (ArrayList)value.BaseObject;
45+
}
46+
2947
/// <summary>
3048
/// Get a hashtable value!
3149
/// </summary>
3250
/// <typeparam name="T">The type of the values in hashtable</typeparam>
3351
/// <param name="PSObject">The object to extend</param>
3452
/// <param name="Name">The name of the property that has the hashtable</param>
3553
/// <returns>The hashtable!</returns>
36-
public static Dictionary<string, T> GetDictionary<T>(this PSObject PSObject, string Name)
54+
public static Dictionary<string, ParameterScript> GetParameterScriptDictionary(this PSObject PSObject, string Name)
3755
{
3856
Hashtable temp = PSObject.GetValue<Hashtable>(Name);
39-
Dictionary<string, T> result = new Dictionary<string, T>();
57+
Dictionary<string, ParameterScript> result = new Dictionary<string, ParameterScript>();
4058
foreach (DictionaryEntry pair in temp)
41-
result[(string)pair.Key] = (T)pair.Value;
59+
result[(string)pair.Key] = new ParameterScript((PSObject)pair.Value);
4260

4361
return result;
4462
}

0 commit comments

Comments
 (0)