0

I've written a PowerShell script to perform some pre-installation setup for a series of patches I'm deploying to client computers across our estate and I'm hitting a bit of an odd issue that I can't wrap my head around.

The setup patch checks the 'C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe.config' file due to a "feature" of PowerShell 2.0 whereby the application uses .NET Framework 2.0.0 by default instead of 4.5.2, preventing certain functions from being executed. If the file doesn't exist or the evaluated values don't match a specification, I add the XML file and provide the necessary values.

The command I run is as follows:

$psConfigDir = "C:\Windows\System32\WindowsPowerShell\v1.0"
$psConfigFileName = "powershell.exe.config"

[boolean]$psExeXml = Set-PSRuntimeConfigs -FilePath ( [String]::Format("{0}\{1}", $psConfigDir, $psConfigFileName) ) -CLRVersions @("v4.0.30319", "v2.0.50727")

...and the Set-PSRuntimeConfigs method is found in a PowerShell Module I created with the code below:

Function Set-PSRuntimeConfigs {
    [CmdletBinding()]
    Param(
            [String]$FilePath,
            [System.Collections.ArrayList]$CLRVersions
         )

    Try {
        $xmlWriter = New-Object System.Xml.XmlTextWriter($FilePath, $null)
        $xmlWriter.Formatting = "Indented"
        $xmlWriter.Indentation = 4

        $xmlWriter.WriteStartDocument()
        $xmlWriter.WriteStartElement("configuration")

        $xmlWriter.WriteStartElement("startup")
        $xmlWriter.WriteAttributeString("useLegacyV2RuntimeActivationPolicy", $true)

        $CLRVersions | ForEach-Object {
            $xmlWriter.WriteStartElement("supportedRuntime")
            $xmlWriter.WriteAttributeString("version", $_)
            $xmlWriter.WriteEndElement()
        }

        $xmlWriter.WriteEndElement()
        $xmlWriter.WriteEndElement()
        $xmlWriter.WriteEndDocument()

        $xmlWriter.Close()
        $xmlWriter.Dispose()

        return $true
    } Catch {
        echo "ERROR: Exception occurred during XML write process!"
        echo "ERROR: Exception message: $($_.Exception.Message)"
        return $false
    }
}

However, the function is returning an InvalidCastException when trying to assign the result of the function to the $psExeXml variable. Oddly, PowerShell returns with an error stating that [System.Object()] cannot be converted to type [Boolean] despite the fact that only $true or $false is returned from the function.

My first thought is that an exception was being thrown by the function due to a code issue but the function is written to report the error in the prompt and just return $false in that case... Regardless, I'm stuck and can't figure out where to proceed with this...

Fredulom
  • 908
  • 1
  • 10
  • 23

1 Answers1

0

If the function produces any output then the result will be an array containing the strings that were output and then the final element will be your boolean.

So for this code:

    echo "ERROR: Exception occurred during XML write process!"
    echo "ERROR: Exception message: $($_.Exception.Message)"
    return $false

the function returns an array of two strings and a boolean.

Duncan
  • 92,073
  • 11
  • 122
  • 156
  • Hi @Duncan, so 'echo' (a.k.a. Write-Host) just writes back to the PowerShell console but still is returned as an object? – Fredulom Mar 15 '17 at 14:26
  • Your mistake is that `echo` is an alias for `Write-Output` not for `Write-Host`. You could use `Write-Host` to write to the console but `Write-Output` produces an output from the function which may or may not eventually go to the console. – Duncan Mar 15 '17 at 14:36
  • Thanks Duncan, I'll change the command to `Write-Host` instead :) – Fredulom Mar 15 '17 at 15:51