在Powershell脚本中添加空行会产生错误

时间:2018-08-29 13:56:37

标签: powershell powershell-v5.0

我刚刚遇到了一个非常奇怪的错误:每当我在ps1文件中添加一个空行(否则本来就可以正常工作)时,我的脚本都会产生错误。

这是设置:

因为遇到依赖关系问题,所以现在有了一个Includes.ps1文件,其中包含以下内容:

$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")

我有一个Package.ps1文件,其中包含该文件,并且可以实例化所需的类并使它们协同工作:

$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()
}

这是Platforms.ps1文件,当我对其进行编辑时会出现错误:

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()

当我更改该文件的内容时,即使添加空行,运行脚本也会给我这些错误:

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

1 个答案:

答案 0 :(得分:0)

事实证明,我不得不关闭powershell命令行并再次运行它,以使我的修改不会产生任何错误。

post帮助我找到了原因...