Skip to content
This repository was archived by the owner on Jul 1, 2025. It is now read-only.

Commit ef2024e

Browse files
committed
Initial commit
0 parents  commit ef2024e

8 files changed

Lines changed: 628 additions & 0 deletions

File tree

.gitignore

Lines changed: 400 additions & 0 deletions
Large diffs are not rendered by default.

LICENSE

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
BSD 3-Clause License
2+
3+
Copyright (c) 2025, PowerShell App Deployment Toolkit
4+
5+
Redistribution and use in source and binary forms, with or without
6+
modification, are permitted provided that the following conditions are met:
7+
8+
1. Redistributions of source code must retain the above copyright notice, this
9+
list of conditions and the following disclaimer.
10+
11+
2. Redistributions in binary form must reproduce the above copyright notice,
12+
this list of conditions and the following disclaimer in the documentation
13+
and/or other materials provided with the distribution.
14+
15+
3. Neither the name of the copyright holder nor the names of its
16+
contributors may be used to endorse or promote products derived from
17+
this software without specific prior written permission.
18+
19+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.12.35707.178 d17.12
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PSAppDeployToolkit.CustomRunspaceDemo", "PSAppDeployToolkit.CustomRunspaceDemo\PSAppDeployToolkit.CustomRunspaceDemo.csproj", "{BC150573-CC9D-40BA-BAE0-9D00CAB4E061}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{BC150573-CC9D-40BA-BAE0-9D00CAB4E061}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{BC150573-CC9D-40BA-BAE0-9D00CAB4E061}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{BC150573-CC9D-40BA-BAE0-9D00CAB4E061}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{BC150573-CC9D-40BA-BAE0-9D00CAB4E061}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
EndGlobal
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<configuration>
3+
<startup>
4+
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.2" />
5+
</startup>
6+
</configuration>
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
4+
<PropertyGroup>
5+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7+
<ProjectGuid>{BC150573-CC9D-40BA-BAE0-9D00CAB4E061}</ProjectGuid>
8+
<OutputType>Exe</OutputType>
9+
<RootNamespace>PSAppDeployToolkit.CustomRunspaceDemo</RootNamespace>
10+
<AssemblyName>PSAppDeployToolkit.CustomRunspaceDemo</AssemblyName>
11+
<TargetFrameworkVersion>v4.6.2</TargetFrameworkVersion>
12+
<FileAlignment>512</FileAlignment>
13+
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
14+
<Deterministic>true</Deterministic>
15+
</PropertyGroup>
16+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
17+
<PlatformTarget>AnyCPU</PlatformTarget>
18+
<DebugSymbols>true</DebugSymbols>
19+
<DebugType>full</DebugType>
20+
<Optimize>false</Optimize>
21+
<OutputPath>bin\Debug\</OutputPath>
22+
<DefineConstants>DEBUG;TRACE</DefineConstants>
23+
<ErrorReport>prompt</ErrorReport>
24+
<WarningLevel>4</WarningLevel>
25+
<Prefer32Bit>false</Prefer32Bit>
26+
<PreferNativeArm64>true</PreferNativeArm64>
27+
</PropertyGroup>
28+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
29+
<PlatformTarget>AnyCPU</PlatformTarget>
30+
<DebugType>pdbonly</DebugType>
31+
<Optimize>true</Optimize>
32+
<OutputPath>bin\Release\</OutputPath>
33+
<DefineConstants>TRACE</DefineConstants>
34+
<ErrorReport>prompt</ErrorReport>
35+
<WarningLevel>4</WarningLevel>
36+
<Prefer32Bit>false</Prefer32Bit>
37+
<PreferNativeArm64>true</PreferNativeArm64>
38+
</PropertyGroup>
39+
<ItemGroup>
40+
<Reference Include="System" />
41+
<Reference Include="System.Core" />
42+
</ItemGroup>
43+
<ItemGroup>
44+
<Reference Include="System.Management.Automation">
45+
<HintPath>C:\Windows\Microsoft.NET\assembly\GAC_MSIL\System.Management.Automation\v4.0_3.0.0.0__31bf3856ad364e35\System.Management.Automation.dll</HintPath>
46+
<Private>False</Private>
47+
</Reference>
48+
</ItemGroup>
49+
<ItemGroup>
50+
<Compile Include="Program.cs" />
51+
<Compile Include="Properties\AssemblyInfo.cs" />
52+
</ItemGroup>
53+
<ItemGroup>
54+
<None Include="App.config" />
55+
</ItemGroup>
56+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
57+
</Project>
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
using System;
2+
using System.Linq;
3+
using System.Management.Automation;
4+
using System.Management.Automation.Runspaces;
5+
6+
namespace PSAppDeployToolkit.CustomRunspaceDemo
7+
{
8+
internal class Program
9+
{
10+
private static void WriteLogEntry(string message)
11+
{
12+
// Write a log entry to the console.
13+
Console.WriteLine($"[{DateTime.Now.ToString("O")}] :: {message}");
14+
}
15+
16+
static void Main()
17+
{
18+
// Set up the initial session state for the new runspace.
19+
WriteLogEntry("Setting up the initial session state for the new runspace.");
20+
var iss = InitialSessionState.CreateDefault();
21+
22+
// Import the PSAppDeployToolkit module into the initial session state.
23+
WriteLogEntry("Importing the PSAppDeployToolkit module into the initial session state.");
24+
iss.ImportPSModule(new[] { "D:\\Repos\\PSAppDeployToolkit\\src\\PSAppDeployToolkit\\PSAppDeployToolkit.psd1" });
25+
26+
// Create a new runspace with the initial session state.
27+
WriteLogEntry("Creating a new runspace with the initial session state.");
28+
using (var rs = RunspaceFactory.CreateRunspace(iss))
29+
{
30+
// Open the runspace to enable the use of the session state.
31+
WriteLogEntry("Opening the runspace to enable the use of the session state.");
32+
rs.Open();
33+
34+
// Create a new PowerShell instance with our runspace.
35+
WriteLogEntry("Creating a new PowerShell instance.");
36+
using (var ps = PowerShell.Create())
37+
{
38+
// Set the runspace for the PowerShell instance.
39+
WriteLogEntry("Setting the runspace for the PowerShell instance.");
40+
ps.Runspace = rs;
41+
42+
// Define the script to be executed.
43+
string script = "Get-Command -Name Initialize-ADTFunction -Module PSAppDeployToolkit | Out-String; Set-ADTRegistryKey -Key 'HKEY_CURRENT_USER\\SOFTWARE\\PSAppDeployToolkit.CustomRunspaceDemo' -Name 'Testing' -Type 'String' -Value 'This is a test'";
44+
45+
// Perform the invocation.
46+
try
47+
{
48+
// Add the script to the PowerShell instance and invoke it.
49+
WriteLogEntry($"Adding the script [{script}] to the PowerShell instance and invoking it.");
50+
var output = ps.AddScript(script).Invoke();
51+
52+
// Throw if we have any errors from the invocation.
53+
if (ps.HadErrors)
54+
{
55+
throw ps.Streams.Error.First().Exception;
56+
}
57+
58+
// Print any output to the console.
59+
WriteLogEntry("Invocation has completed with zero errors. Captured output:");
60+
foreach (var item in output)
61+
{
62+
Console.WriteLine(item);
63+
}
64+
}
65+
catch
66+
{
67+
// Rethrow any exceptions that occurred during the invocation.
68+
throw;
69+
}
70+
finally
71+
{
72+
// Close the runspace.
73+
WriteLogEntry("Closing the runspace and exiting process.");
74+
ps.Runspace.Close();
75+
}
76+
}
77+
}
78+
}
79+
}
80+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// General Information about an assembly is controlled through the following
6+
// set of attributes. Change these attribute values to modify the information
7+
// associated with an assembly.
8+
[assembly: AssemblyTitle("PSAppDeployToolkit.CustomRunspaceDemo")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("")]
12+
[assembly: AssemblyProduct("PSAppDeployToolkit.CustomRunspaceDemo")]
13+
[assembly: AssemblyCopyright("Copyright © 2025")]
14+
[assembly: AssemblyTrademark("")]
15+
[assembly: AssemblyCulture("")]
16+
17+
// Setting ComVisible to false makes the types in this assembly not visible
18+
// to COM components. If you need to access a type in this assembly from
19+
// COM, set the ComVisible attribute to true on that type.
20+
[assembly: ComVisible(false)]
21+
22+
// The following GUID is for the ID of the typelib if this project is exposed to COM
23+
[assembly: Guid("bc150573-cc9d-40ba-bae0-9d00cab4e061")]
24+
25+
// Version information for an assembly consists of the following four values:
26+
//
27+
// Major Version
28+
// Minor Version
29+
// Build Number
30+
// Revision
31+
//
32+
[assembly: AssemblyVersion("1.0.0.0")]
33+
[assembly: AssemblyFileVersion("1.0.0.0")]

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# PSAppDeployToolkit.CustomRunspaceDemo
2+
A C# example project of how to import PSAppDeployToolkit into a custom PowerShell runspace and leverage its functionality.

0 commit comments

Comments
 (0)