Powershell how to embedd text files into Powershell Scripts.

A week ago I posted a blog on some of the powershell coding conventions I try to use when developing solutions. Along those lines, I have another example of something in PowerShell that I recommend.

Here is an example of a function I found recently, it’s straight forwards enough, create a text file with some static content:

Function CreateSanPolicyFile {
  if ( Test-Path "San_Policy.xml" ) {
    del .\San_Policy.xml
  }
  $SanPolicyFile = New-Item "San_Policy.xml" -type File
  set-Content $SanPolicyFile '<?xml version="1.0" encoding="utf-8" standalone="yes"?>'
  add-Content $SanPolicyFile '<unattend xmlns="urn:schemas-microsoft-com:unattend">'
  add-Content $SanPolicyFile ' <settings pass="offlineServicing">'
  add-Content $SanPolicyFile ' <component'
  add-Content $SanPolicyFile ' xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State"'
  add-Content $SanPolicyFile ' xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"'
  add-Content $SanPolicyFile ' language="neutral"'
  add-Content $SanPolicyFile ' name="Microsoft-Windows-PartitionManager"'
  add-Content $SanPolicyFile ' processorArchitecture="amd64"'
  add-Content $SanPolicyFile ' publicKeyToken="31bf3856ad364e35"'
  add-Content $SanPolicyFile ' versionScope="nonSxS"'
  add-Content $SanPolicyFile ' >'
  add-Content $SanPolicyFile ' <SanPolicy>4</SanPolicy>'
  add-Content $SanPolicyFile ' </component>'
  add-Content $SanPolicyFile ' <component'
  add-Content $SanPolicyFile ' xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State"'
  add-Content $SanPolicyFile ' xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"'
  add-Content $SanPolicyFile ' language="neutral"'
  add-Content $SanPolicyFile ' name="Microsoft-Windows-PartitionManager"'
  add-Content $SanPolicyFile ' processorArchitecture="x86"'
  add-Content $SanPolicyFile ' publicKeyToken="31bf3856ad364e35"'
  add-Content $SanPolicyFile ' versionScope="nonSxS"'
  add-Content $SanPolicyFile ' >'
  add-Content $SanPolicyFile ' <SanPolicy>4</SanPolicy>'
  add-Content $SanPolicyFile ' </component>'
  add-Content $SanPolicyFile ' </settings>'
  add-Content $SanPolicyFile '</unattend>'
  #return the file object
  $SanPolicyFile
}

The problem is that the script suffers from a lot of overhead with the Add-Content command. It also makes it harder to add lines or to move text around from one line to the other. Now we have to worry about adding text for each line. This method was quite common with older VBScript samples, but it’s unnecessary with Powershell.

Instead, I would recommend the following:

Function CreateSanPolicyFile ( 
    [string] $Path = ".\San_Policy.xml" 
)  {

@" 
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<unattend xmlns="urn:schemas-microsoft-com:unattend">
  <settings pass="offlineServicing">
    <component
        xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        language="neutral"
        name="Microsoft-Windows-PartitionManager"
        processorArchitecture="amd64"
        publicKeyToken="31bf3856ad364e35"
        versionScope="nonSxS"
        >
      <SanPolicy>4</SanPolicy>
    </component>
    <component
        xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        language="neutral"
        name="Microsoft-Windows-PartitionManager"
        processorArchitecture="x86"
        publicKeyToken="31bf3856ad364e35"
        versionScope="nonSxS"
        >
      <SanPolicy>4</SanPolicy>
    </component>
  </settings>
</unattend>

@" | Set-Content $Path -encoding UTF8
   return $Path
}

Here we have replaced the bulk of the output processing with a here-string, a Here-String is a way to represent a large multi-line text segments. This makes them much easier to edit in out script, and now we can just pipe the multi-line string into the set-content cmdlet. This gives a size advantage as well, a 45% savings!

As a developer I would much rather support #2. :^)

-Keith

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s