|
1 | 1 | --- |
2 | | -title: Writing a custom DSC resource with PowerShell classes |
3 | | -ms.date: 2016-05-16 |
4 | | -keywords: powershell,DSC |
5 | | -description: |
6 | | -ms.topic: article |
| 2 | +ms.date: 2017-06-12 |
7 | 3 | author: eslesar |
8 | | -manager: dongill |
9 | | -ms.prod: powershell |
| 4 | +ms.topic: conceptual |
| 5 | +keywords: dsc,powershell,configuration,setup |
| 6 | +title: Writing a custom DSC resource with PowerShell classes |
10 | 7 | --- |
11 | 8 |
|
12 | 9 | # Writing a custom DSC resource with PowerShell classes |
@@ -472,6 +469,44 @@ Test |
472 | 469 | Start-DscConfiguration -Wait -Force Test |
473 | 470 | ``` |
474 | 471 |
|
| 472 | +## Supporting PsDscRunAsCredential |
| 473 | + |
| 474 | +>**Note:** **PsDscRunAsCredential** is supported in PowerShell 5.0 and later. |
| 475 | +
|
| 476 | +The **PsDscRunAsCredential** property can be used in [DSC configurations](configurations.md) resource block to specify that the |
| 477 | +resource should be run under a specified set of credentials. |
| 478 | +For more information, see [Running DSC with user credentials](runAsUser.md). |
| 479 | + |
| 480 | +### Require or disallow PsDscRunAsCredential for your resource |
| 481 | + |
| 482 | +The **DscResource()** attribute takes an optional parameter **RunAsCredential**. |
| 483 | +This parameter takes one of three values: |
| 484 | + |
| 485 | +- `Optional` **PsDscRunAsCredential** is optional for configurations that call this resource. This is the default value. |
| 486 | +- `Mandatory` **PsDscRunAsCredential** must be used for any configuration that calls this resource. |
| 487 | +- `NotSupported` Configurations that call this resource cannot use **PsDscRunAsCredential**. |
| 488 | +- `Default` Same as `Optional`. |
| 489 | + |
| 490 | +For example, use the following attribute to specify that your custom resource does not support using **PsDscRunAsCredential**: |
| 491 | + |
| 492 | +```powershell |
| 493 | +[DscResource(RunAsCredential=NotSupported)] |
| 494 | +class FileResource { |
| 495 | +} |
| 496 | +``` |
| 497 | + |
| 498 | +### Access the user context |
| 499 | + |
| 500 | +To access the user context from within a custom resource, you can use the automatic variable `$global:PsDscContext`. |
| 501 | + |
| 502 | +For example the following code would write the user context under which the resource is running to the verbose output stream: |
| 503 | + |
| 504 | +```powershell |
| 505 | +if (PsDscContext.RunAsUser) { |
| 506 | + Write-Verbose "User: $global:PsDscContext.RunAsUser"; |
| 507 | +} |
| 508 | +``` |
| 509 | + |
475 | 510 | ## See Also |
476 | 511 | ### Concepts |
477 | 512 | [Build Custom Windows PowerShell Desired State Configuration Resources](authoringResource.md) |
|
0 commit comments