David February 2016

Weird characters in Powershell DSC File Contents

I'm new to PowerShell DSC and I'm writing a script that should ensure if a certain File is present with the correct content. It's fairly simple:

File BatFile
{
   Ensure          = "Present"
   DestinationPath = $BatPath 
   Contents        = "start $($FullPath)" 
   Type            = "File"            
}

When I open the file in notepad, the contents seem to be OK. However, when I execute it I get an error message:

C:\Directory>start C:\Directory\AppIWantToStart.exe
'start' is not recognized as an internal or external command, operable program or batch file.

Suddenly there's these weird  characters in the file! I assume this is some sort of encoding problem but I have no clue how to solve this. Does anyone know of a way to fix this?

EDIT: So apparently these characters are the Byte Order Mark (BOM) and don't show up in notepad due to encoding. See this topic. My question is: Using the DSC File resource, can I write the file content without the BOM?

(Yes, I'm trying to create a Batch script in a PowerShell script. This may be weird, but not relevant to this question!)

Answers


Frode F. February 2016

There's no Encoding property for the File-resource and for some reason I think it writes using UTF-16 w/BOM. You may try using a Script-resource.

Script BatFile
{
  SetScript = {
    "start $($FullPath)" | Set-Content -Path $BatPath -Force
  }
  TestScript = { Test-Path -Path $BatPath }
  GetScript = { @{BatFileExists = (Test-Path $BatPath )} }
}

Post Status

Asked in February 2016
Viewed 3,893 times
Voted 11
Answered 1 times

Search




Leave an answer