Please feel free to provide feedback or file bugs here.

Bug: Here string - When using invoke-command when here string starts with [

Using a here string to create an LCM file using a Hyper-V VM and invoke-command.
If the here string starts with [ the file created is blank if viewing with get-content. Viewing the file with PSEdit, the file is full of squares.

Code used to generate this:

$lcmscript = @'
[DSCLocalConfigurationManager()]
configuration LCMPush
{
Node $AllNodes.Where{$_.Role -eq "Primary DC"}.Nodename
{
Settings
{
RebootNodeIfNeeded = $true
ActionAfterReboot = 'ContinueConfiguration'
ConfigurationMode = 'ApplyAndAutoCorrect'
CertificateID = "REPLACEWITHFRESHLYCREATEDTHUMBPRINT"
}
}
}

# Create the computer.meta.mof in folder
LCMPush -configurationData c:\dsc\dscconfigdata.psd1 `
-OutputPath c:\dsc\LCM
'@

Invoke-Command -VMName $vmname -Credential $cred -ScriptBlock { New-Item c:\dsc\lcm -ItemType Directory; new-item c:\dsc\lcm\dsclcm.ps1 -ItemType File -Value $USING:lcmscript -Force }

Sometimes using this method works correctly. Mostly it produces an empty file though.

I used this same method for other types of here strings in the same script and they work fine.

Workaround for me was to use the here string as in the code below:
$lcmscript | out-file $env:TEMP\dsclcm.ps1
Copy-VMFile -VMName $vmname -SourcePath "$env:Temp\dsclcm.ps1" -DestinationPath 'c:\dsc\lcm\dsclcm.ps1' -CreateFullPath -FileSource Host -Force
start-sleep -seconds 2

Not sure if the file takes too long to be created. I put it before two other here strings that worked correctly and it still failed.

1 vote
Sign in
Check!
(thinking…)
Reset
or sign in with
  • facebook
  • google
    Password icon
    I agree to the terms of service
    Signed in as (Sign out)

    We’ll send you updates on this idea

    Tim HaintzTim Haintz shared this idea  ·   ·  Flag idea as inappropriate…  ·  Admin →

    Interesting…I’m not able to repro it from a PowerShell 5.1 to 5.1 box. Could you paste the output of your $PSVersionTable?

    Also, I’m assuming you’ve saved the script that includes the here string somewhere? Could you paste the output of Format-Hex .\hereString.ps1 | Select-Object -First 1? Even better, could you paste the same thing on the resultant output (i.e. Format-Hex C:\dsc\lcm\dsclcm.ps1 | Select-Object -First 1)?

    5 comments

    Sign in
    Check!
    (thinking…)
    Reset
    or sign in with
    • facebook
    • google
      Password icon
      I agree to the terms of service
      Signed in as (Sign out)
      Submitting...
      • Tim HaintzTim Haintz commented  ·   ·  Flag as inappropriate

        Another thing I am using is a differencing disk to create my VM. I'm creating that in another .ps1 file. Not sure if that will have any impact though. I'm using the below method to create the disk.

        new-vm -Name $vmname `
        -VHDPath (New-VHD -Differencing -ParentPath "$vhdreferencedisk" -Path "$vmNameVhdPath" -SizeBytes 20GB).Path `
        -MemoryStartupBytes 4GB `
        -Generation 2 `
        -SwitchName "InternalNetwork"

        Thanks, Tim.

      • Tim HaintzTim Haintz commented  ·   ·  Flag as inappropriate

        TEMP FILE CREATED LOCALLY
        PS P:\> format-hex C:\Users\xxxxx\AppData\Local\Temp\dsclcm.ps1 | Select-Object -First 1

        Path: C:\Users\xxxxx\AppData\Local\Temp\dsclcm.ps1

        00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F

        00000000 FF FE 5B 00 44 00 53 00 43 00 4C 00 6F 00 63 00 .þ[.D.S.C.L.o.c.

      • Tim HaintzTim Haintz commented  ·   ·  Flag as inappropriate

        PSVERSIONTABLE on 2016 server running on HyperV.
        [dc1]: PS C:\dsc\lcm> $PSVersionTable

        Name Value
        ---- -----
        PSVersion 5.1.14393.576
        PSEdition Desktop
        PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
        BuildVersion 10.0.14393.576
        CLRVersion 4.0.30319.42000
        WSManStackVersion 3.0
        PSRemotingProtocolVersion 2.3
        SerializationVersion 1.1.0.1

      • Tim HaintzTim Haintz commented  ·   ·  Flag as inappropriate

        FILE CREATED USING HERE STRING ON WINDOWS SERVER 2016 CORE
        [dc1]: PS C:\dsc\lcm> format-hex .\dsclcm.ps1 | Select-Object -First 1

        Path: C:\dsc\lcm\dsclcm.ps1

        00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F

        00000000 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................

      • Tim HaintzTim Haintz commented  ·   ·  Flag as inappropriate

        Hi Joey, thanks for the response. I will break my next couple of responses up to keep the comments clean.
        PSVersion Table:
        PS P:\> $PSVersionTable

        Name Value
        ---- -----
        PSVersion 5.1.14393.576
        PSEdition Desktop
        PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
        BuildVersion 10.0.14393.576
        CLRVersion 4.0.30319.42000
        WSManStackVersion 3.0
        PSRemotingProtocolVersion 2.3
        SerializationVersion 1.1.0.1

      Feedback and Knowledge Base