Skip to content

Commit

Permalink
Fix formatting of code samples (#10713)
Browse files Browse the repository at this point in the history
  • Loading branch information
sdwheeler authored Dec 5, 2023
1 parent 55467e4 commit 3899af6
Showing 1 changed file with 42 additions and 35 deletions.
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

0 comments on commit 3899af6

Please sign in to comment.