-1

I have just encountered a very weird error: whenever I just add an empty line in a ps1 file which is otherwise working really fine, my script generates errors.

Here is the setup:

Because I had dependencies issues, I now have an Includes.ps1 file with that content:

$ScriptDirectory = Split-Path -Path $MyInvocation.MyCommand.Definition -Parent

. ("$ScriptDirectory\HelperFunctions.ps1")
. ("$ScriptDirectory\BuildParameters.ps1")
. ("$ScriptDirectory\Platform.ps1")
. ("$ScriptDirectory\Configuration.ps1")
. ("$ScriptDirectory\Action.ps1")
. ("$ScriptDirectory\Backup.ps1")

I have a Package.ps1 file which includes that file, and does the stuff of instanciating the needed classes and make them work together:

$ScriptDirectory = Split-Path -Path $MyInvocation.MyCommand.Definition -Parent
. ("$ScriptDirectory\Includes.ps1")

Clear-Host

$Parameters = New-Object -TypeName "BuildParameters"

$user_variables_path = "./UserVariables.ps1"

if ( Test-Path $user_variables_path )
{
    . $user_variables_path
}

$Parameters.ValidateParameters()

$PlatformClass = $PlatformFactory.MakePlatform( $Parameters.Platform )
$PlatformClass.ValidateParameters( $Parameters )

$ConfigurationClass = $ConfigurationFactory.MakeConfiguration( $Parameters.Configuration )
$ConfigurationClass.ValidateParameters( $Parameters )

$ActionClass = $ActionsFactory.MakeAction( $Parameters.Action, $PlatformClass, $ConfigurationClass, $Parameters )
$ActionClass.ValidateParameters()

$Backup = [ Backup ]::new( $Parameters, $PlatformClass )
$Backup.ValidateParameters();

$ActionClass.Execute()

if ( $Parameters.BackupVersion )
{
    $Backup.BackupVersion()
}

Here is the Platforms.ps1 file which gives me errors when I edit it:

class Platform
{
    [Boolean] $CanBePatched
    [Boolean] $CanCompressData

    Platform()
    {
        $this.CanBePatched = $false
        $this.CanCompressData = $true
    }

    [void] ValidateParameters( [BuildParameters] $Parameters )
    {
    }
}

class PlatformWin64 : Platform
{
    PlatformWin64()
    {
    }
}

class PlatformXboxOne : Platform
{
    PlatformXboxOne()
    {
    }
}

class PlatformSwitch : Platform
{
    PlatformSwitch()
    {
        $this.CanBePatched = $true
    }

    [void] ValidateParameters( [BuildParameters] $Parameters )
    {
        if ( [string]::IsNullOrEmpty( $Parameters.Region ) )
        {
            WriteErrorAndExit( "You need to specify a region for the PS4" )
        }
    }
}

class PlatformPS4 : Platform
{
    PlatformPS4()
    {
        $this.CanBePatched = $true
        $this.CanCompressData = $false
    }

    [void] ValidateParameters( [BuildParameters] $Parameters )
    {
        if ( [string]::IsNullOrEmpty( $Parameters.Region ) )
        {
            WriteErrorAndExit( "You need to specify a region for the PS4" )
        }
    }
}

class PlatformFactory
{
    [Platform] MakePlatform( [String] $name )
    {
        return (New-Object -TypeName "Platform$name")
    }
}

$PlatformFactory = [PlatformFactory]::new()

When I change the contents of that file, even by adding empty lines, running the script gives me those errors:

Cannot convert argument "Platform", with value: "PlatformWin64", for
"MakeAction" to type "Platform": "Cannot convert the "PlatformWin64" value
of type "PlatformWin64" to type "Platform"."
At F:\Projects\RWC\BuildScripts\Utils\Package.ps1:55 char:1
+ $ActionClass = $ActionsFactory.MakeAction( $Parameters.Action, $Platf ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodException
    + FullyQualifiedErrorId : MethodArgumentConversionInvalidCastArgument

You cannot call a method on a null-valued expression.
At F:\Projects\RWC\BuildScripts\Utils\Package.ps1:56 char:1
+ $ActionClass.ValidateParameters()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

Cannot convert argument "Platform", with value: "PlatformWin64", for
".ctor" to type "Platform": "Cannot convert the "PlatformWin64" value of
type "PlatformWin64" to type "Platform"."
At F:\Projects\RWC\BuildScripts\Utils\Package.ps1:58 char:1
+ $Backup = [ Backup ]::new( $Parameters, $PlatformClass )
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodException
    + FullyQualifiedErrorId : MethodArgumentConversionInvalidCastArgument

You cannot call a method on a null-valued expression.
At F:\Projects\RWC\BuildScripts\Utils\Package.ps1:59 char:1
+ $Backup.ValidateParameters();
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

You cannot call a method on a null-valued expression.
At F:\Projects\RWC\BuildScripts\Utils\Package.ps1:61 char:1
+ $ActionClass.Execute()
+ ~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

You cannot call a method on a null-valued expression.
At F:\Projects\RWC\BuildScripts\Utils\Package.ps1:65 char:5
+     $Backup.BackupVersion()
+     ~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
Emidee
  • 1,245
  • 1
  • 14
  • 33
  • 2
    `Cannot convert argument "Platform", with value: "PlatformWin64", for "MakeAction" to type "Platform"` You have a type mismatch where implicit conversion is failing. – Maximilian Burszley Aug 29 '18 at 14:12
  • I give to the script the **Platform** argument with Win64. This then calls PlatformFactory::MakePlatform with PlatformWin64, which is a valid class type. And this does not make any clearer why by adding some empty lines, I have this error ^^ – Emidee Aug 29 '18 at 14:33
  • `Platform` needs to exist **before** you can inherit from it. Having it in the same file is probably the cause of your problem due to how powershell parses classes. – Maximilian Burszley Aug 29 '18 at 14:42
  • I just created a PlatformWin64.ps1 file with the class inside, add that new script to the includes.ps1, but I still get the same error – Emidee Aug 29 '18 at 14:47

1 Answers1

1

Well it turns out I had to close the powershell command line and run it again to have my modifications not generate any errors.

That post helped me find the cause...

Emidee
  • 1,245
  • 1
  • 14
  • 33