Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell 变量卡在“a”中;至于;动力壳_Powershell_Variables_Scope - Fatal编程技术网

Powershell 变量卡在“a”中;至于;动力壳

Powershell 变量卡在“a”中;至于;动力壳,powershell,variables,scope,Powershell,Variables,Scope,我一直在尝试退出,变量$groupUsers(数组)卡在了一个数组中。 我一直在研究变量的作用域,我发现一个变量必须是全局的,如果我想它在所有函数中都可用的话 我曾尝试将数组放在foreach之外,如果用于,甚至将其转换为$global:groupUsers,但它不起作用 function get-localadmins{ [cmdletbinding()] param( [string[]] $machineslist)

我一直在尝试退出,变量
$groupUsers
(数组)卡在了一个数组中。 我一直在研究变量的作用域,我发现一个变量必须是全局的,如果我想它在所有函数中都可用的话

我曾尝试将数组放在
foreach
之外,如果用于,甚至将其转换为
$global:groupUsers
,但它不起作用

        function get-localadmins{
          [cmdletbinding()]
         param( [string[]] $machineslist)


        #split computers list
        $separator = ","
        $option = [System.StringSplitOptions]::RemoveEmptyEntries
        $computerSplit = $machineslist.Split($separator, 20000 ,$option)

        #input credentials 
        $credential = Get-Credential kimo\admin0987

        ######## Create empty array to use in the for #####
        $groupUsers = @()

        foreach ($computer in $computerSplit)
        {

        $session = new-pssession -computername $computer -Credential $credential

        If ($? -eq $false )


                {
            $alert = "La conexion remota de ""PSSRemoting""no pude ser establecida con este equipo porfavor verifica WinRM has been updated to receive requests. WinRM service type changed successfully. WinRM service started. WinRM has been updated for remote management. WinRM firewall exception enabled. que la politica de este activada o corre el commando asdfjsfda para habilitar powershell remoto "
        } 
        else
        {


        new-pssession -computername $computer -Credential $credential
        $admingroup2 = Invoke-Command -ComputerName $computer -Credential $credential -ScriptBlock {
        $group = get-wmiobject win32_group -ComputerName localhost -Filter "LocalAccount=True AND SID='S-1-5-32-544'"
        $query = "GroupComponent = `"Win32_Group.Domain='$($group.domain)'`,Name='$($group.name)'`""
        $list = Get-WmiObject win32_groupuser -ComputerName localhost -Filter $query
        $admingroup = $list | select PartComponent # | % {$_.substring($_.lastindexof("Domain=") + 7).replace("`",Name=`"","\")}

        $clean = $admingroup

         for($i = 0; $i -lt $clean.length; $i += 1)
                    {
                        $string = $clean[$i] | Out-String


                        $domainPosition = $string.lastindexof('Domain=') + 7
                        $nextComma = $string.substring($domainPosition).indexOf(',')
                        $domain = $string.substring($domainPosition,$nextComma-1)

                        $namePosition = $string.lastindexof('Name="') + 6
                        $name = $string.substring($namePosition)


    #####this is the variable that I can get outside the for ######


                          $groupUsers += $domain + "\" + $name

                        Write-Host $groupUsers "inside for"
                    }

          Write-Host $groupUsers "outside for"
        }




        }



        }

############### I need to get the variable here ###################
        $groupUsers| ConvertTo-Html 


         Write-Host $groupUsers "out side the for-each "
        }

        $222 = get-localadmins localhost,machine1,machine2 

这里唯一的问题似乎是您没有从函数返回
$groupUsers
。那是因为你永远不会还它

Write Host
直接写入主机应用程序,作为显示数据的一种方式,而不将数据包含在管道中

要返回数据,请使用
Write Object
,或者根本不限定它:

                      $groupUsers += $domain + "\" + $name

                    Write-Host $groupUsers "inside for"
                }

      Write-Host $groupUsers "outside for"
      $groupUsers # this will return it
    }
然后,
$222
变量将包含该值


这里不需要
$Global
作用域。

这里唯一的问题似乎是没有从函数返回
$groupUsers
。那是因为你永远不会还它

Write Host
直接写入主机应用程序,作为显示数据的一种方式,而不将数据包含在管道中

要返回数据,请使用
Write Object
,或者根本不限定它:

                      $groupUsers += $domain + "\" + $name

                    Write-Host $groupUsers "inside for"
                }

      Write-Host $groupUsers "outside for"
      $groupUsers # this will return it
    }
然后,
$222
变量将包含该值


这里不需要
$Global
作用域。

您的
写主机
调用有效吗?他们是否正确显示了
$groupUsers
的值?是的,写入主机$groupUsers“inside for”具有我需要的信息,并显示了管理员用户。您的
写入主机
调用是否有效?他们是否正确显示了
$groupUsers
的值?是的,将主机$groupUsers“inside for”写入具有我需要的信息并显示管理员用户。我不想返回$groupUser变量。因为功能接收器将计算机列表作为参数。如果计算机满足else的要求,则执行将停止,并且不会处理计算机列表。我的想法是用处理过的数据创建一个对象($groupUsers),然后用$groupUsers | converto创建一个html报告-html@kimopryvt:briantst所说的是返回
$groupUsers
的正确方法。这就是你的问题所在。如果您需要其他帮助,请打开一个新问题。我不想返回$groupUser变量。因为功能接收器将计算机列表作为参数。如果计算机满足else的要求,则执行将停止,并且不会处理计算机列表。我的想法是用处理过的数据创建一个对象($groupUsers),然后用$groupUsers | converto创建一个html报告-html@kimopryvt:briantst所说的是返回
$groupUsers
的正确方法。这就是你的问题所在。如果你在其他方面需要帮助,请提出一个新问题。