PowerShell 通过引用传递对我不起作用

标签 powershell powershell-2.0

我正在尝试在 PowerShell 中创建一个简单的交换函数,但通过引用传递似乎对我不起作用。

function swap ([ref]$object1, [ref]$object2){
  $tmp = $object1.value
  $object1.value = $object2.value
  $object2.value = $tmp
}

$a = 1
$b = 2
$a, $b
swap ([ref]$a) ,([ref]$b)
$a, $b

这应该有效,但是不行......

Output:
    1
    2
    1
    2

我做错了什么?

最佳答案

这样调用:

swap ([ref]$a) ([ref]$b)

使用,的错误在Common Gotchas for PowerShell here on Stack Overflow中有描述。 .

关于PowerShell 通过引用传递对我不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7149783/

相关文章:

php - 如何在 PHP 中运行 Powershell 脚本?

powershell - 为什么使用不存在的变量或属性时Powershell不输出错误?

powershell - 字符串替换在运行时在 powershell 脚本中不起作用

使用 $matches 数组中的值时出现 PowerShell Set-Item 奇怪现象

powershell - 测试连接内解析 DnsName

powershell - 是否可以使用 Powershell 在 Active Directory 中设置用户 memberOf 属性

powershell - Azure 资源模板中的 dataDisk.image 参数

git - 在触发器上从 GitLab 存储库发布到 Azure Web Apps

Powershell 检查本地管理员凭据

powershell - 为什么 Powershell ISE 显示 Powershell 控制台不显示的错误?