Search code examples
jsonpowershellinvoke-command

Invoke-Command fails with $env Variable fails when configuration is read from a file


I am trying to retrieve files from a remote system via PowerShell. In order to do this i utilize New- PSSession and Invoke-Command in this session.

$latestFolder = Invoke-Command -Session $remoteSession -ScriptBlock{param($path) Get-ChildItem $path -Directory -Name} -ArgumentList $path

If i set path to $Path = "$env:SystemRoot\System32" it works just fine, but if i set it to a string read from a configuration file (json) it gives me the weirdest problems. One is that it cannot find the parameter -Directory, if i omit the -Directory and -Name parameters the error message is Ein Laufwerk mit dem Namen "$env" ist nicht vorhanden. Roughly translatet to A drive named "$env" is not available.

The configuration file looks like this:

{
    "File": [
        {
            "Name": "TEST",
            "Active": true,
            "Path": "$env:SystemRoot\\System32"
        }
    ]
}

The Powershell script is the following:

$logFilePath = Join-Path (get-item $PSScriptRoot).FullName "Test.json"
$logConfig = Get-Content -Path $logFilePath | ConvertFrom-Json

$windowsUser = "username"
$windowsUserPassword = "password"
$windowsUserPasswordsec = $windowsUserPassword | ConvertTo-SecureString -AsPlainText -Force
$Server = "server"

$Path = "$env:SystemRoot\System32"
$Path = $logConfig.File[0].Path

$sessioncred = new-object -typeName System.Management.Automation.PSCredential -ArgumentList $windowsUser, $windowsUserPasswordsec
$remoteSession = New-PSSession -ComputerName $Server -Credential $sessioncred -Authentication "Kerberos"

$latestFolder = Invoke-Command -Session $remoteSession -ScriptBlock{param($path) Get-ChildItem $path -Directory -Name} -ArgumentList $Path
$latestFolder

Solution

  • You need to explicitely expand the string read from the Path element in the Json file.

    This should do it:

    $Path = $ExecutionContext.InvokeCommand.ExpandString($logConfig.File[0].Path)
    

    An alternative could be by using Invoke-Expression:

    $Path = Invoke-Expression ('"{0}"' -f $logConfig.File[0].Path)