首页 > 解决方案 > Powershell 错误 + 无法将值“System.String”转换为类型“System.Management.Automation.SwitchParameter”

问题描述

我有一个 powershell 模块,它在执行 powershell 脚本时写入日志

PowerShell 模块:module.psm1

function Write-Log
{
    [CmdletBinding()]
    param(
        [Parameter(Mandatory = $true, Position = 0)]
        [alias("LogText")]
        [string]$Text,

        [Parameter(Mandatory = $false, Position = 1)]
        [alias("TextType")]
        [string]$Type,

        [Parameter(Mandatory = $true, Position = 2)]
        [alias("LogFile")]
        [string]$File
    )
    begin{
        $stamp = (get-date).ToString("dd-MM-yyyy HH:mm:ss")
        $output = ""
    }
    process{

        switch ($Type)
        {
           'info' {
                $output = "$stamp <span style='color;#23a1cc'><strong>INFO`t: </strong></span> $Text<br>"
                break
            }
            'warning' {
                $output = "$stamp <span style='color;#f5a900'><strong>WARNING`t: </strong></span> $Text<br>"
                break
            }
            'error' {
                $output = "$stamp <span style='color;#cf000f'><strong>ERROR`t: </strong></span> $Text<br>"
                break
            }
            'success' {
                $output = "$stamp <span style='color;#009944'><strong>SUCCESS`t: </strong></span> $Text<br>"
                break
            }
            'header' {
                $output = "<H3 style='color;#009944'>$Text</H3><br>"
                break
            }
            default {
                $output = "$stamp <span style='color;#23a1cc'><strong>INFO`t: </strong></span> $Text<br>"
                break
            }
        }

        $output | Out-File -FilePath $File -Append
        Write-Host "$Type : $Text"
    }
    end{
        $stamp = ""
        $output = ""
    }
}

我在我的 powershell 脚本 script.ps1 中导入了这个模块

当我打电话时

Write-Log -Text "Successfully Imported Module" -Type 'info' -File 'C:\Temp\log.html'

我得到错误

Write-Log : Cannot process argument transformation on parameter 'Type'. Cannot convert value "System.String" to type "System.Management.Automation.SwitchParameter". 

我是 powershell 的新手,第一次使用 switch ......不确定发生了什么并且有点困惑,因为我发现文章说 switch 只接受 Boolean ?现在确定在我的情况下如何解决。任何建议将不胜感激。

标签: powershell

解决方案


推荐阅读