Skip to content

Commit d2d048f

Browse files
matt9uccisdwheeler
authored andcommitted
Update "Host Quickstart" (#4217)
1 parent b5d1347 commit d2d048f

File tree

1 file changed

+35
-13
lines changed

1 file changed

+35
-13
lines changed

developer/hosting/windows-powershell-host-quickstart.md

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,21 @@ caps.latest.revision: 9
1111
---
1212
# Windows PowerShell Host Quickstart
1313

14-
To host Windows PowerShell in your application, you use the [System.Management.Automation.PowerShell](/dotnet/api/System.Management.Automation.PowerShell) class. This class provides methods that create a pipeline of commands and then execute those commands in a runspace. The simplest way to create a host application is to use the default runspace. The default runspace contains all of the core Windows PowerShell commands. If you want your application to expose only a subset of the Windows PowerShell commands, you must create a custom runspace.
14+
To host Windows PowerShell in your application, you use the [System.Management.Automation.PowerShell](/dotnet/api/System.Management.Automation.PowerShell) class.
15+
This class provides methods that create a pipeline of commands and then execute those commands in a runspace.
16+
The simplest way to create a host application is to use the default runspace.
17+
The default runspace contains all of the core Windows PowerShell commands.
18+
If you want your application to expose only a subset of the Windows PowerShell commands, you must create a custom runspace.
1519

1620
## Using the default runspace
1721

1822
To start, we'll use the default runspace, and use the methods of the [System.Management.Automation.PowerShell](/dotnet/api/System.Management.Automation.PowerShell) class to add commands, parameters, statements, and scripts to a pipeline.
1923

2024
### AddCommand
2125

22-
You use the [System.Management.Automation.Powershell.AddCommand*](/dotnet/api/System.Management.Automation.PowerShell.AddCommand) method of the [System.Management.Automation.PowerShell](/dotnet/api/System.Management.Automation.PowerShell) class to add commands to the pipeline. For example, suppose you want to get the list of running processes on the machine. The way to run this command is as follows.
26+
You use the [System.Management.Automation.Powershell.AddCommand](/dotnet/api/System.Management.Automation.PowerShell.AddCommand) method to add commands to the pipeline.
27+
For example, suppose you want to get the list of running processes on the machine.
28+
The way to run this command is as follows.
2329

2430
1. Create a [System.Management.Automation.PowerShell](/dotnet/api/System.Management.Automation.PowerShell) object.
2531

@@ -39,19 +45,22 @@ You use the [System.Management.Automation.Powershell.AddCommand*](/dotnet/api/Sy
3945
ps.Invoke();
4046
```
4147

42-
If you call the [System.Management.Automation.Powershell.AddCommand*](/dotnet/api/System.Management.Automation.PowerShell.AddCommand) method more than once before you call the [System.Management.Automation.Powershell.Invoke*](/dotnet/api/System.Management.Automation.PowerShell.Invoke) method, the result of the first command is piped to the second, and so on. If you do not want to pipe the result of a previous command to a command, add it by calling the [System.Management.Automation.Powershell.AddStatement*](/dotnet/api/System.Management.Automation.PowerShell.AddStatement) instead.
48+
If you call the AddCommand method more than once before you call the [System.Management.Automation.Powershell.Invoke](/dotnet/api/System.Management.Automation.PowerShell.Invoke) method, the result of the first command is piped to the second, and so on.
49+
If you do not want to pipe the result of a previous command to a command, add it by calling the [System.Management.Automation.Powershell.AddStatement](/dotnet/api/System.Management.Automation.PowerShell.AddStatement) instead.
4350

4451
### AddParameter
4552

46-
The previous example executes a single command without any parameters. You can add parameters to the command by using the [System.Management.Automation.PSCommand.AddParameter*](/dotnet/api/System.Management.Automation.PSCommand.AddParameter) method For example, the following code gets a list of all of the processes that are named `PowerShell` running on the machine.
53+
The previous example executes a single command without any parameters.
54+
You can add parameters to the command by using the [System.Management.Automation.PSCommand.AddParameter](/dotnet/api/System.Management.Automation.PSCommand.AddParameter) method.
55+
For example, the following code gets a list of all of the processes that are named `PowerShell` running on the machine.
4756

4857
```csharp
4958
PowerShell.Create().AddCommand("Get-Process")
5059
.AddParameter("Name", "PowerShell")
5160
.Invoke();
5261
```
5362

54-
You can add additional parameters by calling [System.Management.Automation.PSCommand.AddParameter*](/dotnet/api/System.Management.Automation.PSCommand.AddParameter) repeatedly.
63+
You can add additional parameters by calling the AddParameter method repeatedly.
5564

5665
```csharp
5766
PowerShell.Create().AddCommand("Get-Process")
@@ -60,7 +69,7 @@ PowerShell.Create().AddCommand("Get-Process")
6069
.Invoke();
6170
```
6271

63-
You can also add a dictionary of parameter names and values by calling the [System.Management.Automation.PowerShell.AddParameters*](/dotnet/api/System.Management.Automation.PowerShell.AddParameters) method.
72+
You can also add a dictionary of parameter names and values by calling the [System.Management.Automation.PowerShell.AddParameters](/dotnet/api/System.Management.Automation.PowerShell.AddParameters) method.
6473

6574
```csharp
6675
IDictionary parameters = new Dictionary<String, String>();
@@ -75,7 +84,8 @@ PowerShell.Create().AddCommand("Get-Process")
7584

7685
### AddStatement
7786

78-
You can simulate batching by using the [System.Management.Automation.PowerShell.AddStatement*](/dotnet/api/System.Management.Automation.PowerShell.AddStatement) method, which adds an additional statement to the end of the pipeline The following code gets a list of running processes with the name `PowerShell`, and then gets the list of running services.
87+
You can simulate batching by using the [System.Management.Automation.PowerShell.AddStatement](/dotnet/api/System.Management.Automation.PowerShell.AddStatement) method, which adds an additional statement to the end of the pipeline.
88+
The following code gets a list of running processes with the name `PowerShell`, and then gets the list of running services.
7989

8090
```csharp
8191
PowerShell ps = PowerShell.Create();
@@ -86,14 +96,18 @@ ps.Invoke();
8696

8797
### AddScript
8898

89-
You can run an existing script by calling the [System.Management.Automation.PowerShell.AddScript*](/dotnet/api/System.Management.Automation.PowerShell.AddScript) method. The following example adds a script to the pipeline and runs it. This example assumes there is already a script named `MyScript.ps1` in a folder named `D:\PSScripts`.
99+
You can run an existing script by calling the [System.Management.Automation.PowerShell.AddScript](/dotnet/api/System.Management.Automation.PowerShell.AddScript) method.
100+
The following example adds a script to the pipeline and runs it.
101+
This example assumes there is already a script named `MyScript.ps1` in a folder named `D:\PSScripts`.
90102

91103
```csharp
92104
PowerShell ps = PowerShell.Create();
93105
ps.AddScript("D:\PSScripts\MyScript.ps1").Invoke();
94106
```
95107

96-
There is also a version of the [System.Management.Automation.PowerShell.AddScript*](/dotnet/api/System.Management.Automation.PowerShell.AddScript) method that takes a boolean parameter named `useLocalScope`. If this parameter is set to `true`, then the script is run in the local scope. The following code will run the script in the local scope.
108+
There is also a version of the AddScript method that takes a boolean parameter named `useLocalScope`.
109+
If this parameter is set to `true`, then the script is run in the local scope.
110+
The following code will run the script in the local scope.
97111

98112
```csharp
99113
PowerShell ps = PowerShell.Create();
@@ -102,11 +116,15 @@ ps.AddScript(@"D:\PSScripts\MyScript.ps1", true).Invoke();
102116

103117
## Creating a custom runspace
104118

105-
While the default runspace used in the previous examples loads all of the core Windows PowerShell commands, you can create a custom runspace that loads only a specified subset of all commands. You might want to do this to improve performance (loading a larger number of commands is a performance hit), or to restrict the capability of the user to perform operations. A runspace that exposes only a limited number of commands is called a constrained runspace. To create a constrained runspace, you use the [System.Management.Automation.Runspaces.Runspace](/dotnet/api/System.Management.Automation.Runspaces.Runspace) and [System.Management.Automation.Runspaces.InitialSessionState](/dotnet/api/System.Management.Automation.Runspaces.InitialSessionState) classes.
119+
While the default runspace used in the previous examples loads all of the core Windows PowerShell commands, you can create a custom runspace that loads only a specified subset of all commands.
120+
You might want to do this to improve performance (loading a larger number of commands is a performance hit), or to restrict the capability of the user to perform operations.
121+
A runspace that exposes only a limited number of commands is called a constrained runspace.
122+
To create a constrained runspace, you use the [System.Management.Automation.Runspaces.Runspace](/dotnet/api/System.Management.Automation.Runspaces.Runspace) and [System.Management.Automation.Runspaces.InitialSessionState](/dotnet/api/System.Management.Automation.Runspaces.InitialSessionState) classes.
106123

107124
### Creating an InitialSessionState object
108125

109-
To create a custom runspace, you must first create an [System.Management.Automation.Runspaces.InitialSessionState](/dotnet/api/System.Management.Automation.Runspaces.InitialSessionState) object. In the following example, we use the [System.Management.Automation.Runspaces.RunspaceFactory](/dotnet/api/System.Management.Automation.Runspaces.RunspaceFactory) to create a runspace after creating a default [System.Management.Automation.Runspaces.InitialSessionState](/dotnet/api/System.Management.Automation.Runspaces.InitialSessionState) object.
126+
To create a custom runspace, you must first create an [System.Management.Automation.Runspaces.InitialSessionState](/dotnet/api/System.Management.Automation.Runspaces.InitialSessionState) object.
127+
In the following example, we use the [System.Management.Automation.Runspaces.RunspaceFactory](/dotnet/api/System.Management.Automation.Runspaces.RunspaceFactory) to create a runspace after creating a default InitialSessionState object.
110128

111129
```csharp
112130
InitialSessionState iss = InitialSessionState.CreateDefault();
@@ -120,11 +138,15 @@ ps.Invoke();
120138

121139
### Constraining the runspace
122140

123-
In the previous example, we created a default [System.Management.Automation.Runspaces.InitialSessionState](/dotnet/api/System.Management.Automation.Runspaces.InitialSessionState) object that loads all of the built-in core Windows PowerShell. We could also have called the [System.Management.Automation.Runspaces.InitialSessionState.CreateDefault2*](/dotnet/api/System.Management.Automation.Runspaces.InitialSessionState.CreateDefault2) method to create an InitialSessionState object that would load only the commands in the Microsoft.PowerShell.Core snapin. To create a more constrained runspace, you must create an empty [System.Management.Automation.Runspaces.InitialSessionState](/dotnet/api/System.Management.Automation.Runspaces.InitialSessionState) object by calling the [System.Management.Automation.Runspaces.InitialSessionState.Create*](/dotnet/api/System.Management.Automation.Runspaces.InitialSessionState.Create) method, and then add commands to the InitialSessionState.
141+
In the previous example, we created a default [System.Management.Automation.Runspaces.InitialSessionState](/dotnet/api/System.Management.Automation.Runspaces.InitialSessionState) object that loads all of the built-in core Windows PowerShell.
142+
We could also have called the [System.Management.Automation.Runspaces.InitialSessionState.CreateDefault2](/dotnet/api/System.Management.Automation.Runspaces.InitialSessionState.CreateDefault2) method to create an InitialSessionState object that would load only the commands in the Microsoft.PowerShell.Core snapin.
143+
To create a more constrained runspace, you must create an empty InitialSessionState object by calling the [System.Management.Automation.Runspaces.InitialSessionState.Create](/dotnet/api/System.Management.Automation.Runspaces.InitialSessionState.Create) method, and then add commands to the InitialSessionState.
124144

125145
Using a runspace that loads only the commands that you specify provides significantly improved performance.
126146

127-
You use the methods of the [System.Management.Automation.Runspaces.SessionStateCmdletEntry](/dotnet/api/System.Management.Automation.Runspaces.SessionStateCmdletEntry) class to define cmdlets for the initial session state. The following example creates an empty initial session state, then defines and adds the `Get-Command` and `Import-Module` commands to the initial session state. We then create a runspace constrained by that initial session state, and execute the commands in that runspace.
147+
You use the methods of the [System.Management.Automation.Runspaces.SessionStateCmdletEntry](/dotnet/api/System.Management.Automation.Runspaces.SessionStateCmdletEntry) class to define cmdlets for the initial session state.
148+
The following example creates an empty initial session state, then defines and adds the `Get-Command` and `Import-Module` commands to the initial session state.
149+
We then create a runspace constrained by that initial session state, and execute the commands in that runspace.
128150

129151
Create the initial session state.
130152

0 commit comments

Comments
 (0)