我收到以下错误消息:
Cannot bind argument to parameter 'FilePath' because it is an empty string.
从此代码:
$image = "image"
$logfile_name = "log.txt"
cleanup $image $logfile_name
function cleanup($image, $logfile) {
log_message "message" $logfile
}
function log_message($msg, $logfile) {
$msg | Tee-Object -Append -FilePath "$logfile"
}
我尝试过带引号和不带引号,我做错了什么?
编辑:我尝试回显该值并且它存在,但是当我在 log_message 命令中运行它时,它显示它的 null/空字符串。
最佳答案
您的代码对我不起作用,因为您没有使用“函数”一词来创建两个函数:函数 cleanup(..) 而不仅仅是 cleanup(..) 并且您应该在调用它之前将这些函数放入您的脚本。
我已按如下方式更改了您的代码,现在它可以工作了:
function cleanup($image, $logfile) {
log_message "message" $logfile
}
function log_message($msg, $logfile) {
$msg | Tee-Object -Append -FilePath "$logfile"
}
$image = "image"
$logfile_name = "log.txt"
cleanup $image $logfile_name
关于powershell - 将参数传递给函数: Cannot bind argument to parameter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51651797/