Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Justin Grote's Lab Answers #2

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions YourFirstPSModuleInCSharp.sln
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Example.7.PowerShell.MultiT
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Example.4.Hybrid", "src\Example.4.Hybrid\Example.4.Hybrid.csproj", "{FE5DF71E-4AE5-4440-AD30-A9588E92CE21}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lab.1.RandomEmail", "src\Lab.1.RandomEmail\Lab.1.RandomEmail.csproj", "{2713ADDB-20DA-43C2-AE4D-1F2DCA6675D3}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lab.2.RandomSentence", "src\Lab.2.RandomSentence\Lab.2.RandomSentence.csproj", "{50408EFA-E99F-4A90-BFF7-A4AB8518BFC6}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lab.3.Bouncer", "src\Lab.3.Bouncer\Lab.3.Bouncer.csproj", "{1EA614E4-F977-4CC3-AA27-1D609CC289D2}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -56,6 +62,18 @@ Global
{FE5DF71E-4AE5-4440-AD30-A9588E92CE21}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FE5DF71E-4AE5-4440-AD30-A9588E92CE21}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FE5DF71E-4AE5-4440-AD30-A9588E92CE21}.Release|Any CPU.Build.0 = Release|Any CPU
{2713ADDB-20DA-43C2-AE4D-1F2DCA6675D3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2713ADDB-20DA-43C2-AE4D-1F2DCA6675D3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2713ADDB-20DA-43C2-AE4D-1F2DCA6675D3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2713ADDB-20DA-43C2-AE4D-1F2DCA6675D3}.Release|Any CPU.Build.0 = Release|Any CPU
{50408EFA-E99F-4A90-BFF7-A4AB8518BFC6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{50408EFA-E99F-4A90-BFF7-A4AB8518BFC6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{50408EFA-E99F-4A90-BFF7-A4AB8518BFC6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{50408EFA-E99F-4A90-BFF7-A4AB8518BFC6}.Release|Any CPU.Build.0 = Release|Any CPU
{1EA614E4-F977-4CC3-AA27-1D609CC289D2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1EA614E4-F977-4CC3-AA27-1D609CC289D2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1EA614E4-F977-4CC3-AA27-1D609CC289D2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1EA614E4-F977-4CC3-AA27-1D609CC289D2}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{C686146C-58E6-4D85-B36D-8E1AC5F18D7C} = {8142F2E5-265E-4F9F-A9C9-EA30A735ABD7}
Expand All @@ -65,5 +83,8 @@ Global
{9D4F9AAA-D7A5-4C61-8352-DA050AD934DA} = {8142F2E5-265E-4F9F-A9C9-EA30A735ABD7}
{69B43DD8-1DA6-4556-A813-4570786B162E} = {8142F2E5-265E-4F9F-A9C9-EA30A735ABD7}
{FE5DF71E-4AE5-4440-AD30-A9588E92CE21} = {8142F2E5-265E-4F9F-A9C9-EA30A735ABD7}
{2713ADDB-20DA-43C2-AE4D-1F2DCA6675D3} = {8142F2E5-265E-4F9F-A9C9-EA30A735ABD7}
{50408EFA-E99F-4A90-BFF7-A4AB8518BFC6} = {8142F2E5-265E-4F9F-A9C9-EA30A735ABD7}
{1EA614E4-F977-4CC3-AA27-1D609CC289D2} = {8142F2E5-265E-4F9F-A9C9-EA30A735ABD7}
EndGlobalSection
EndGlobal
18 changes: 18 additions & 0 deletions src/Lab.1.RandomEmail/GetPEURandomEmailCmdlet.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
namespace PEURandom;
using System.Management.Automation;
using System.Net.Mail;
using LoremNET;

[Cmdlet(VerbsCommon.Get, "PEURandomEmail")]
public class GetPEURandomEmailCmdlet : PSCmdlet
{
[Parameter()]
public SwitchParameter UsernameOnly;

protected override void EndProcessing()
{
MailAddress email = new(Lorem.Email());
string output = UsernameOnly ? email.User : email.ToString();
WriteObject(output);
}
}
32 changes: 32 additions & 0 deletions src/Lab.1.RandomEmail/Lab.1.RandomEmail.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<!--
This targets PowerShell 7.2+, the current oldest supported release as of Feburary 2023. This gives us
the broadest compatibility across PowerShell while still allowing us to use new-ish features.

To target others:
PowerShell 7.3+: net7.0
PowerShell 7.2+: net6.0
PowerShell 5.1+: netstandard2.0 (single set of assemblies) or net471 (multi-targeting)
-->
<TargetFramework>net6.0</TargetFramework>

<!-- This cuts down on the amount of stuff that shows up in our out directory, if localization isn't important to you-->
<SatelliteResourceLanguages>en</SatelliteResourceLanguages>
</PropertyGroup>

<!-- Packages you add with dotnet add package will show up in this first itemgroup you define. -->
<ItemGroup>
<!-- This package gives us some nice functions to generate some random data-->
<PackageReference Include="Lorem.Universal.Net" Version="4.0.80" />

<!-- This package gives us some nice tools for working with networks -->
<PackageReference Include="IPNetwork2" Version="2.6.548" />

<!-- PowerShell 7.2+ Module Configuration -->
<!-- These DLLs ship in PS7 so we say PrivateAssets meaning "dont include this in the publish".
You may need to add more to the list depending on your app's dependencies -->
<PackageReference Include="System.Management.Automation" Version="7.2.0" PrivateAssets="all" />
</ItemGroup>
</Project>
26 changes: 26 additions & 0 deletions src/Lab.2.RandomSentence/GetPEURandomSentenceCmdlet.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
namespace PEURandom;
using System.Management.Automation;
using LoremNET;

[Cmdlet(VerbsCommon.Get, "PEURandomSentence")]
public class GetPEURandomSentenceCmdlet : PSCmdlet
{
[Parameter(ValueFromPipeline = true)]
public string Name;

[Parameter()]
public int Min = 5;
[Parameter()]
public int Max = 10;

protected override void ProcessRecord()
{
var randomSentence = Lorem.Sentence(Min, Max);
if (Name is not null) {
randomSentence = randomSentence.Replace(".$", string.Empty) + $", {Name}";
}
WriteObject(
randomSentence
);
}
}
32 changes: 32 additions & 0 deletions src/Lab.2.RandomSentence/Lab.2.RandomSentence.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<!--
This targets PowerShell 7.2+, the current oldest supported release as of Feburary 2023. This gives us
the broadest compatibility across PowerShell while still allowing us to use new-ish features.

To target others:
PowerShell 7.3+: net7.0
PowerShell 7.2+: net6.0
PowerShell 5.1+: netstandard2.0 (single set of assemblies) or net471 (multi-targeting)
-->
<TargetFramework>net6.0</TargetFramework>

<!-- This cuts down on the amount of stuff that shows up in our out directory, if localization isn't important to you-->
<SatelliteResourceLanguages>en</SatelliteResourceLanguages>
</PropertyGroup>

<!-- Packages you add with dotnet add package will show up in this first itemgroup you define. -->
<ItemGroup>
<!-- This package gives us some nice functions to generate some random data-->
<PackageReference Include="Lorem.Universal.Net" Version="4.0.80" />

<!-- This package gives us some nice tools for working with networks -->
<PackageReference Include="IPNetwork2" Version="2.6.548" />

<!-- PowerShell 7.2+ Module Configuration -->
<!-- These DLLs ship in PS7 so we say PrivateAssets meaning "dont include this in the publish".
You may need to add more to the list depending on your app's dependencies -->
<PackageReference Include="System.Management.Automation" Version="7.2.0" PrivateAssets="all" />
</ItemGroup>
</Project>
28 changes: 28 additions & 0 deletions src/Lab.3.Bouncer/AssertPEUAgeCmdlet.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;
using System.IO;
using System.Management.Automation;

namespace PEURandom;

[Cmdlet(VerbsLifecycle.Assert, "PEUAge")]
[OutputType(typeof(Person))]
public class AssertPEUAgeCmdlet : PSCmdlet
{
[Parameter(Position = 0, ValueFromPipeline = true)]
public IPerson Person;

[Parameter()]
public int Age = 18;

protected override void ProcessRecord()
{
const int daysInAYear = 365;
var ageSpan = DateTime.Now - Person.BirthDate;
int personAge = (int)Math.Floor(ageSpan.TotalDays / daysInAYear);
if (personAge < Age) {
throw new InvalidDataException($"{Person.Name} is under the age of {Age}");
}

WriteObject(Person);
}
}
26 changes: 26 additions & 0 deletions src/Lab.3.Bouncer/GetPEUAgeCmdlet.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System.Management.Automation;
using LoremNET;

namespace PEURandom;

[Cmdlet(VerbsCommon.Get, "PEUAge")]
[OutputType(typeof(Person))]
public class GetPEUAgeCmdlet : PSCmdlet
{
[Parameter(Position = 0, ValueFromPipeline = true, Mandatory = true)]
public string Name;

protected override void ProcessRecord()
{
WriteObject(
GetPerson()
);
}

Person GetPerson() {
return new Person(
Name,
Lorem.DateTime()
);
}
}
32 changes: 32 additions & 0 deletions src/Lab.3.Bouncer/Lab.3.Bouncer.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<!--
This targets PowerShell 7.2+, the current oldest supported release as of Feburary 2023. This gives us
the broadest compatibility across PowerShell while still allowing us to use new-ish features.

To target others:
PowerShell 7.3+: net7.0
PowerShell 7.2+: net6.0
PowerShell 5.1+: netstandard2.0 (single set of assemblies) or net471 (multi-targeting)
-->
<TargetFramework>net6.0</TargetFramework>

<!-- This cuts down on the amount of stuff that shows up in our out directory, if localization isn't important to you-->
<SatelliteResourceLanguages>en</SatelliteResourceLanguages>
</PropertyGroup>

<!-- Packages you add with dotnet add package will show up in this first itemgroup you define. -->
<ItemGroup>
<!-- This package gives us some nice functions to generate some random data-->
<PackageReference Include="Lorem.Universal.Net" Version="4.0.80" />

<!-- This package gives us some nice tools for working with networks -->
<PackageReference Include="IPNetwork2" Version="2.6.548" />

<!-- PowerShell 7.2+ Module Configuration -->
<!-- These DLLs ship in PS7 so we say PrivateAssets meaning "dont include this in the publish".
You may need to add more to the list depending on your app's dependencies -->
<PackageReference Include="System.Management.Automation" Version="7.2.0" PrivateAssets="all" />
</ItemGroup>
</Project>
10 changes: 10 additions & 0 deletions src/Lab.3.Bouncer/Person.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System;

namespace PEURandom;

public interface IPerson {
string Name { get; }
DateTime BirthDate { get; }
}

public record Person(string Name, DateTime BirthDate) : IPerson;
4 changes: 2 additions & 2 deletions tests/Lab.2.RandomSentence.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Describe 'Lab 2: Get a random sentence' {
}
Context 'Names Via Pipeline' {
BeforeAll {
$name = 'Pester1', 'Pester2', 'Pester3'
$name = 'Pester1','Pester2','Pester3'
$SCRIPT:actual = $name | Get-PEURandomSentence
}

Expand All @@ -42,7 +42,7 @@ Describe 'Lab 2: Get a random sentence' {
}

It 'Each sentence ends with the corresponding name provided via the pipeline' {
# Check that each item output matches the corresponding name.
# Check that each item output matches the corresponding name.
#NOTE: This is a bad test if you are attempting to do things in parallel as they will possibly arrive out of order, but it is fine here.
$i = 0
foreach ($actualItem in $actual) {
Expand Down