Search code examples
arrayspowershellpowershell-remoting

Array removed after function?


I am making a script that goes into all servers we're hosting and gets all members of a specific group and the domain name, and then exports it to a file. I'm saving the users and the domain names into two arrays AA (user array) and DA (domain array) AA stands for användararray, and "användare" is users in swedish so it makes sense to me.

I noticed that the export step didn't work, no users or domain names were exported, so I tried to print them in the function. But it doesn't print anything, so I tried to print it in a different location (didn't work). After some experimenting I came to the conlusion that the only place the arrays actually contains any information is inside the foreach loop where I save the users that I find??!

Here is the code

unction GetData([int]$p) {

Write-Host("B") 

for ($row = 1; $row -le $UsernamesArray.Length; $row++) 
{

    if($CloudArray[$row] -eq 1)
    {

        .

        $secstr = New-Object -TypeName System.Security.SecureString 
        $PasswordsArray[$row].ToCharArray() | ForEach-Object {$secstr.AppendChar($_)}
        $cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $UsernamesArray[$row], $secstr



        $output = Invoke-Command -computername $AddressArray[$row] -credential $cred -ScriptBlock {

            Import-Module Activedirectory                

            foreach ($Anvandare in (Get-ADGroupMember fjärrskrivbordsanvändare)) 
            {

                $AA = @($Anvandare.Name) 
                $DA = gc env:UserDomain
                #$DA + ";" + $Anvandare.Name
                $DA + ";" + $AA
            }
        }
    $output
    }
}
$DA
$AA
}

function Export {

Write-Host("C")


$filsökväg = "C:\Users\322sien\Desktop\Coolkids.csv"
$ColForetag = "Företag"
$ColAnvandare = "Användare"
$Emptyline = "`n"
$delimiter = ";"

for ($p = 1; $p -le $DomainArray.Length; $p++) {


    $ColForetag + $delimiter + $ColAnvandare | Out-File $filsökväg




    $DA + $delimiter + $AA | Out-File $filsökväg -Append

    }
}

ReadInfo
GetData
Export

Can anyone help me with this? I've sat down with this all day and i cant find a solution.


Solution

  • Your variables $DA and $AA are bound to GetData function, so they live only there. You could make them available inside your script by changing it's scope.

    Change this:

                $AA = @($Anvandare.Name) 
                $DA = gc env:UserDomain
    

    To this:

                $script:AA = @($Anvandare.Name) 
                $script:DA = gc env:UserDomain
    

    So they will now be available for other functions inside the script.

    Also I found the ways to improve your script, hope you can see the logic:

    function GetData([int]$p) {
    
    Write-Host("B") 
    
        for ($row = 1; $row -le $UsernamesArray.Length; $row++) 
        {
    
            if($CloudArray[$row] -eq 1)
            {
    
                .
    
                $secstr = New-Object -TypeName System.Security.SecureString 
                $PasswordsArray[$row].ToCharArray() | ForEach-Object {$secstr.AppendChar($_)}
                $cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $UsernamesArray[$row], $secstr
    
    
    
                [array]$output = Invoke-Command -computername $AddressArray[$row] -credential $cred -ScriptBlock {
    
                    Import-Module Activedirectory  
                    $array = @()            
                    foreach ($Anvandare in (Get-ADGroupMember fjärrskrivbordsanvändare)) 
                    {
                        $object = New-Object PSObject
                        $object | Add-Member -MemberType NoteProperty -Name AA -Value @($Anvandare.Name) 
                        $object | Add-Member -MemberType NoteProperty -Name DA -Value (gc env:UserDomain)
                        $object | Add-Member -MemberType NoteProperty -Name Something -Value $DA + ";" + $AA
                        $array += $object
                    }
                    Write-Output $array
                }
            Write-Output $output
            }
        }
    }
    

    Your function will now output some data.