-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix formatting of code samples (#10713)
- Loading branch information
Showing
1 changed file
with
42 additions
and
35 deletions.
There are no files selected for viewing
77 changes: 42 additions & 35 deletions
77
...rence/docs-conceptual/developer/cmdlet/how-to-invoke-scripts-within-a-cmdlet.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,55 @@ | ||
--- | ||
description: How to Invoke Scripts Within a Cmdlet | ||
ms.date: 09/13/2016 | ||
ms.date: 12/05/2023 | ||
ms.topic: reference | ||
title: How to Invoke Scripts Within a Cmdlet | ||
--- | ||
# How to Invoke Scripts Within a Cmdlet | ||
|
||
This example shows how to invoke a script that is supplied to a cmdlet. The script is executed by the cmdlet, and its results are returned to the cmdlet as a collection of [System.Management.Automation.PSObject](/dotnet/api/System.Management.Automation.PSObject) objects. | ||
This example shows how to invoke a script that is supplied to a cmdlet. The script is executed by | ||
the cmdlet, and its results are returned to the cmdlet as a collection of | ||
[System.Management.Automation.PSObject][02] objects. | ||
|
||
## To invoke a script block | ||
|
||
1. The command verifies that a script block was supplied to the cmdlet. If a script block was supplied, the command invokes the script block with its required parameters. | ||
|
||
```csharp | ||
if (script != null) | ||
{ | ||
WriteDebug("Executing script block."); | ||
|
||
// Invoke the script block with the required arguments. | ||
Collection<PSObject> PSObjects = | ||
script.Invoke( | ||
line, | ||
simpleMatch, | ||
caseSensitive | ||
); | ||
``` | ||
|
||
2. Then, the script iterates through the returned collection of [System.Management.Automation.PSObject](/dotnet/api/System.Management.Automation.PSObject) objects and perform the necessary operations. | ||
|
||
```csharp | ||
foreach (PSObject psObject in psObjects) | ||
{ | ||
if (LanguagePrimitives.IsTrue(psObject)) | ||
{ | ||
result = new MatchInfo(); | ||
result.Line = line; | ||
result.IgnoreCase = !caseSensitive; | ||
|
||
break; | ||
} | ||
} | ||
|
||
``` | ||
1. The command verifies that a script block was supplied to the cmdlet. If a script block was | ||
supplied, the command invokes the script block with its required parameters. | ||
|
||
```csharp | ||
if (script != null) | ||
{ | ||
WriteDebug("Executing script block."); | ||
|
||
// Invoke the script block with the required arguments. | ||
Collection<PSObject> PSObjects = script.Invoke( | ||
line, | ||
simpleMatch, | ||
caseSensitive | ||
); | ||
// more code as needed... | ||
} | ||
``` | ||
|
||
1. Then, the script iterates through the returned collection of | ||
[System.Management.Automation.PSObject][02] objects and perform the necessary operations. | ||
|
||
```csharp | ||
foreach (PSObject object in PSObjects) | ||
{ | ||
if (LanguagePrimitives.IsTrue(object)) | ||
{ | ||
result = new MatchInfo(); | ||
result.Line = line; | ||
result.IgnoreCase = !caseSensitive; | ||
break; | ||
} | ||
} | ||
``` | ||
|
||
## See Also | ||
|
||
[Writing a Windows PowerShell Cmdlet](./writing-a-windows-powershell-cmdlet.md) | ||
[Writing a Windows PowerShell Cmdlet][01] | ||
|
||
<!-- link references --> | ||
[01]: ./writing-a-windows-powershell-cmdlet.md | ||
[02]: /dotnet/api/System.Management.Automation.PSObject |