DXH
DXH

Reputation: 51

Azure DSC - Execute PS Script

I was not able to find any examples on how to execute my powershell script after it was downloaded and unzip on the targeted node.

I used DSC xRemoteFile to download the package then Archive to unzip my msi and powershell script.

There is a DSC Script resource I can use to Invoke my powershell script, can any of you provide any examples for me to start off? My PS script and MSI is located under C:\Installations.

Example:

Script install
{
    GetScript = {
    }
    SetScript = {
    }
    TestScript = {
    }
}

Upvotes: 0

Views: 2145

Answers (3)

jeo
jeo

Reputation: 1

This worked for me

Configuration Deploy
{
    Import-DscResource -ModuleName 'PSDesiredStateConfiguration'
    Import-Dscresource -ModuleName 'PowerShellModule' 
    Import-DscResource -ModuleName 'Az.Storage'

    Node 'localhost'
    {
       
    # SCRIPT RESOURCE TO DOWNLOAD FILE FROM STORAGE ACCOUNT
        Script DownloadFile{
         GetScript = {""}
         SetScript = {

            $staccname = "strdsctest234r"
            $dest = "C:\Temp"
            $SasToken= "sp=r3D"

            $Context = New-AzStorageContext -StorageAccountName $staccname -SasToken $SasToken
            $Context | Get-AzStorageBlob -Container "test"
            $blobs = Get-AzStorageBlob -Container "test"  -Context $Context

            # Download each blob from container into destination directory
            $blobs | Get-AzStorageBlobContent -Destination $dest -Force
         }
         TestScript = {$false}
      }}}
#Deploy -OutputPath C:\Temp\Deploy
#Start-DscConfiguration -path C:\Temp\Deploy -wait -Verbose -Force

Upvotes: 0

satoukum
satoukum

Reputation: 1258

Had a similar issue and this is how our team went about the problem:

xRemoteFile DownloadHelloWorldScript
{
    DestinationPath = "c:\Temp\Hello-World.ps1"
    Uri = "https://<account>.blob.core.windows.net/public/Temp/Hello-World.ps1"
    MatchSource = $false
}

Script RunHelloWorldScript
{
    SetScript = { . "c:\Temp\Hello-World.ps1" }
    TestScript = { $false } 
    GetScript = { @{ Result = (Get-Content "c:\Temp\Hello-World.ps1") } }
    DependsOn = "[xRemoteFile]DownloadHelloWorldScript"
}

When I had TestScript = { Test-Path "c:\Temp\Hello-World.ps1" } instead of { $false }, DSC would see that the file had be downloaded and skip running the script. I think I will end up replacing this with some things to check that will verify the script previously ran successfully.

Upvotes: 3

4c74356b41
4c74356b41

Reputation: 72191

First of all, you should use script extension for that, it will download the file and execute it.

and in your case you just write the powershell code to execute a script:

. .\script.ps1

you just need to put in proper path

Upvotes: 4

Related Questions