Replacing Values in array from another array.

Mike 246 Reputation points
2021-03-10T20:27:01.767+00:00
function RemoveSvcAccount
{
    $Terminalusers2 = [System.collections.arraylist]$Script:Terminalusers
    foreach($svcAccount in $script:svcmap){
    foreach($terminaluser in $Script:TerminalUsers){
    if($terminaluser.samaccountname -eq $svcaccount.serviceaccountname){
    $Terminalusers2.remove($_)
    $Terminalusers3=$Terminalusers2}}
    $Terminalusers1 >> users1.txt 
    }
    {
    else
    write-host "false"}                                                         
}

This is the code that I have. I can't seem to remove the value in Terminalusers that exists in $script:svcmap.samaccountname. ANd how do I pass the value from this function to a variable outside this function?

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,360 questions
0 comments No comments
{count} votes

Accepted answer
  1. Ian Xue (Shanghai Wicresoft Co., Ltd.) 29,571 Reputation points Microsoft Vendor
    2021-03-11T02:45:52.773+00:00

    Hi Mike,

    To pass a value to a variable outside the function the scope must be specified. Please see if this works for you.

    function RemoveSvcAccount  
    {  
        $Terminalusers2 = [System.collections.arraylist]$Script:Terminalusers  
        foreach($svcAccount in $script:svcmap){  
           foreach($terminaluser in $Script:TerminalUsers){  
               if($terminaluser.samaccountname -eq $svcaccount.serviceaccountname)  
               {  
                   $Terminalusers2.remove($terminaluser)  
               }      
           }             
        }  
        $Terminalusers2 | Out-File users1.txt                                                          
    }  
    RemoveSvcAccount  
    

    Best Regards,
    Ian Xue

    ============================================

    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful