powershell - 将参数传递给函数: Cannot bind argument to parameter

标签 powershell

我收到以下错误消息:

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/

相关文章:

powershell - 奇怪的获取项目行为

powershell - 在 Windows PowerShell 脚本中检测虚拟 (TrueCrypt) 卷的安装

powershell - Convertto-HTML输出的前/后内容显示为System.String []而不是实际内容

powershell - 如何在Azure ASM中指定DNS名称(域名)前缀?

powershell - 使用 PowerShell Invoke-RestMethod 查询 MSGraph API 不会返回与 MSGraph Explorer 相同数量的详细信息

linux - Azure Powershell 模块 - Linux (.NET Core) 支持

Powershell 列表框项目排序错误

powershell - PowerShell 中的一元 "Not"或位翻转运算符?

arrays - 为什么我不能在 Powershell 的 foreach 循环中查找数组索引?

testing - 如何从命令行以管理员身份运行应用程序 (TestComplete)