简体   繁体   English

PowerShell将脚本参数传递给配置功能

[英]PowerShell pass script argument to configurate function

I have a global 我有一个全球

function log( [string] $message, $logType = 'console' ){}
log( 'hello' );

It write logs to console by default. 默认情况下,它将日志写入控制台。 And i want to configure it by command line argument to keep all calls the same, but with another logging type. 我想通过命令行参数对其进行配置,以使所有调用保持不变,但使用另一种日志记录类型。 function can take or arguments, or command line parameters. 函数可以接受或参数,或命令行参数。 and i want to mix them 我想把它们混合

function log($message,$logtype = 'console'){
    switch ($logtype){
        'console' {Write-Host $message}
        'verbose' {Write-Verbose $message}
        'file' {Out-File $message $path}
        default {$message}
    }
}
log 'FOO' 'verbose'

For the $logtype parameter you might want to set Mandatory to $false. 对于$ logtype参数,您可能需要将Mandatory设置为$ false。 Parameters 参量

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM