How do I store metadata inside powershell script?

Hello,

I need to be able to pull some XML or other type of structred data from inside powershell script file itself. I can not store it in external file becouse this script is executed over PowerShell remoting and I don’t want to deal with double hop issue. Is it possible to do or what other alternatives might be short of dealing with double hop? I figured storing this in TXT record in DNS would work as well but there are some drawback issues there.

The double hop issue only comes into play where windows authentication is involved. If you hosted your data on a web server somewhere and downloaded it with Invoke-WebRequest or Invoke-RestMethod, it should be fine.

Alternatively, you could fetch the data on the local computer (even from a share that would require windows authentication), then pass it to your script as an argument via Invoke-Command’s -ArgumentList parameter.

Yes. I’m having issue with double hop via Windows authentication to DFS backed file share where I want to store settings file. I want it to store in central location outside of server where script is being executed since I don’t know ahead of time where it’s going to be executed. So double hop will be part of the issue. TXT record in DNS does not require authentication and fault tolerant like DFS.

GJ,

You have stated you are using PowerShell Remoting. Therefore a solution could be to pass the structured data like a hash table as argument to Invoke-Command.

Arbitrary Example 1 - Single script with data:

$Data = @{
    'Notepad++' = @{
        Path = 'C:\Program Files (x86)\Notepad++'
    }
    'Google Chrome' = @{
        Path = 'C:\Program Files (x86)\Google\Chrome\Application'
    }
}

$ScriptBlock = {
    Param (
        [Hashtable]
        $Data
    )
    foreach ($App in $Data.GetEnumerator()) {
        Remove-App -Name $App.Key -Path $App.Value.Path
    }
}

Invoke-Command -ComputerName localhost -ScriptBlock $ScriptBlock -ArgumentList $Data

Arbitrary Example 2 - Script with data in a psd1 file:

Applications.psd1 file

@{
    'Notepad++' = @{
        Path = 'C:\Program Files (x86)\Notepad++'
    }
    'Google Chrome' = @{
        Path = 'C:\Program Files (x86)\Google\Chrome\Application'
    }
}

Main controller script

# Using Dave's import function - Published here for convenience: https://gist.github.com/Dan1el42/32361edda25436a775bd
$Data = Import-DataFile -Path $PSScriptRoot\Applications.psd1

$ScriptBlock = {
    Param (
        [Hashtable]
        $Data
    )
    foreach ($App in $Data.GetEnumerator()) {
        Remove-App -Name $App.Key -Path $App.Value.Path
    }
}

Invoke-Command -ComputerName localhost -ScriptBlock $ScriptBlock -ArgumentList $Data

Arbitrary Example 3 - Controller script with script block in external ps1 file and data in Applications.psd1 (see above example):

Script block in external ps1 next to the main controller script called Invoke-ApplicationRemoval.ps1

Param (
    [Hashtable]
    $Data
)
foreach ($App in $Data.GetEnumerator()) {
    Remove-App -Name $App.Key -Path $App.Value.Path
}

Main controller script

# Using Dave's import function - Published here for convenience: https://gist.github.com/Dan1el42/32361edda25436a775bd
$Data = Import-DataFile -Path $PSScriptRoot\Applications.psd1

Invoke-Command -ComputerName localhost -FilePath $PSScriptRoot\Invoke-ApplicationRemoval.ps1 -ArgumentList $Data

I hope above gives you a couple of ideas.

Best,
Daniel