Azure / azure-devtestlab

Azure DevTestLab artifacts, scripts and samples
MIT License
458 stars 602 forks source link

Add artifact windows-execute-powershell-script #890

Closed bradselw closed 5 months ago

bradselw commented 5 months ago

Description

We are submitting our custom artifact windows-execute-powershell-script to the public repo for the following reasons:

We are submitting this PR as part of a security effort to eliminate all access tokens and improve reliability across our systems.

The windows-execute-powershell-script artifact can be used to execute a PowerShell script with robust logging and other features. Just provide the required script path and optionally the arguments to pass it.

To aid in investigation scenarios, the artifact optionally supports uploading a transcript of the operation and select files to an Azure Artifacts Drop. Simply provide the logs drop service URL, name, and authentication details (access token or managed identity). You can optionally specify files you would like to "snapshot" and add to the logs drop at the time of artifact completion via the LogsDropFilesToInclude and LogsDropFilesToExclude parameters.

Validation

You can test this artifact via the following steps:

  1. Create a file script.ps1 with the following content:
    param (
       [Parameter(Mandatory)]
       [string]
       $Name
    )
    Write-Output "Hello, $Name!"
  2. Open a command prompt, navigate to the artifact's directory, and execute the following command:
    powershell.exe -ExecutionPolicy bypass "& ./ExecutePowerShellScript.ps1 -ScriptPath '<path to script.ps1>' -ScriptParameters '-Name ''World'''"
    • Replace <path to script.ps1 with the path to the script you created in the previous step.
  3. The output should include:
    Hello, World!
  4. Navigate to %SystemDrive%\DevTestLabs\Artifacts\Logs, open up the latest .log file, and confirm it looks something like this:
    **********************
    Windows PowerShell transcript start
    Start time: 20240423103944
    Username: CONTOSO\foobar
    RunAs User: CONTOSO\foobar
    Configuration Name: 
    Machine: foobar-laptop (Microsoft Windows NT 10.0.22631.0)
    Process ID: 22464
    PSVersion: 5.1.22621.2506
    PSEdition: Desktop
    PSCompatibleVersions: 1.0, 2.0, 3.0, 4.0, 5.0, 5.1.22621.2506
    BuildVersion: 10.0.22621.2506
    CLRVersion: 4.0.30319.42000
    WSManStackVersion: 3.0
    PSRemotingProtocolVersion: 2.3
    SerializationVersion: 1.1.0.1
    **********************
    Transcript started, output file is C:\DevTestLabs\Artifacts\Logs\20240423T1639439885Z-script.ps1.log
    VERBOSE: Script path resolved to 'C:\temp\script.ps1'.
    VERBOSE: Attempting to set 0 environment variables...
    INFO: Successfully set 0 environment variables.
    VERBOSE: Attempting to execute the script 'C:\temp\script.ps1' with parameters '-Name 'World''...
    Hello, World!
    INFO: Successfully executed script 'C:\temp\script.ps1'.
    **********************
    Windows PowerShell transcript end
    End time: 20240423103944
    **********************

Successful Result

Following is a screenshot of the artifact logs of a successful execution of this artifact:

image