Powershell array scope; why is my array empty?

不想你离开。 提交于 2019-12-07 07:11:22

问题


I want to have an array and add elements to it from different functions in my script. My example below illustrates where I might be misunderstanding something regarding scope. My understanding currently is that if an array is defined outside the function, and then elements are added inside the function, those elements should be available outside the function.

Function ListRunningServices
{
    $services+= Get-Service | ?{$_.Status -eq "Running"} | sort Name | select Name;
}

$services = @();
ListRunningServices;
$services;

What am I missing here? Perhaps my style is completely wrong.


回答1:


The $services within the function block is scoped to the function. You can do something like the following instead:

Function ListRunningServices {
    Get-Service | ?{$_.Status -eq "Running"} | sort Name | select Name
}

$services = ListRunningServices
$services

Else, you may explicitly use global: to alter the scope:

Function ListRunningServices {
    $global:services = Get-Service | ?{$_.Status -eq "Running"} | sort Name | select Name
}

$services = @()
ListRunningServices
$services



回答2:


You can solve this with global variables, but using globals is genereally considered bad practice. If you use a generic collection type, like arraylist, instead of an array then you have an add() method that will update the collection in a parent scope without needing to explicitly scope it in the function:

Function ListRunningServices
{
    Get-Service | ?{$_.Status -eq "Running"} | sort Name | select Name |
     ForEach-Object {$services.add($_)}
}

$services = New-Object collections.arraylist
ListRunningServices
$services



回答3:


$service in the scope of the function has nothing to do with the one outside the function.

Try :

Function ListRunningServices
{
    $services = @()
    $services+= Get-Service | ?{$_.Status -eq "Running"} | sort Name | select Name;
    return $services
}


$services = ListRunningServices
$services

Keeping most of your code you can use :

Function ListRunningServices
{
    $global:services+= Get-Service | ?{$_.Status -eq "Running"} | sort Name | select Name;
}

$services = @();
ListRunningServices;
$services;

You can read the full explanation in About_scope.




回答4:


Powershell requires specifying the scope with prefixes on array variables. Seems that "normal String" variables don't. Use them like this:

# Visible everywhere inside the script file (f.ex: myNameScript.ps1)
$script:names = @()

Function AddStringToArray ([string]$i_name) {
    $script:names += $i_name
}

AddStringToArray -i_name "Markie"
AddStringToArray -i_name "Harry"

I know this is late, but I hope this helps at least someone.



来源:https://stackoverflow.com/questions/20520980/powershell-array-scope-why-is-my-array-empty

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!