function - 无法使用powershell函数在数组中添加元素

标签 function powershell scripting powershell-2.0 powershell-3.0

我有一个奇怪的问题,我有一个非常简单的函数。

这是我的powershell代码

$exclude = @()

function GetOracleDb {
param([string]$servername)
$exclude += $servername
}

GetOracleDb "Myserver2"

$exclude

为什么我的 $exclude 数组是空的?

谢谢你的帮助

最佳答案

改成这样:

$global:exclude += $servername

函数内部 $exclude 的作用域与外部不同。

关于function - 无法使用powershell函数在数组中添加元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17028355/

相关文章:

javascript - 圆括号javascript

c++ - const 引用模板参数中的函数?

java - 是否有一种独立于语言的方法可以将函数添加到 JSR223 脚本绑定(bind)?

powershell - 有没有办法在 PowerShell 中使用单个开关匹配来定位多个条件?

powershell - Powershell-Active Directory-按今天过滤PasswordLastSet

用于检查网站标签的 Python 脚本

PHP 函数和变量以字符串形式存储在数据库中

internet-explorer - 使用 powershell 加载新的 IE 窗口并在后台执行操作

linux - 如何将文本/纯文本转换为文本/x.shellscript

scripting - 在较大的应用程序中托管 .fsx 脚本