Powershell - object return

Lewandowski, Bartłomiej 0 Reputation points
2023-04-12T08:30:18.3733333+00:00
Hello, I made a simple program that print specified computers in our Domain, sadly there is a problem with returning an object from a function after call. First call does not work, 2nd print same output 2 times.  Do you have any idea how to display it in a first run? As far as i know it is a problem that get-ad makes object and objects are not so eazy to return from function. 

function MyFunctionOne{

    $Computers=(Get-ADComputer -Filter * -Properties MemberOf,description | Where-Object {[STRING]$_.MemberOf -notlike "*GroupName*"} | Select name, Description )

    return $Computers 

}
function main{

$choice = 0

Import-Module -Global -Name ActiveDirectory

Import-Module -Global -Name Microsoft.PowerShell.Utility   // I though It was problem with module so I import it before execute command

    do{

        $choice = Read-Host

        switch($choice){

        1 {MyFunctionOne break}

        2 {$choice = 0}

        }

    }while($choice -ne 0)

}

main
Windows for business Windows Client for IT Pros Directory services Active Directory
Windows for business Windows Server User experience PowerShell
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. dashanan13 930 Reputation points
    2023-04-12T11:05:16.3533333+00:00

    Hei Lewandowski, Bartłomiej, Thank you for posting to Microsoft Q&A. I understand that you want the names and descriptions of the computers in your domain that match particular criteria. The criteria is, the property MemberOf for the resulting computer should not have a value that contains the string "GroupName". In addition, you are also trying to make a system with a switch case such that you can call multiple functions with the choice of user input. Currently, this system of switch case calls one function if the user enters 1, and on the second choice it exists. I tried the code, and i don't see much problem except that there is a bracket extra when you select name and description in the first line inside FunctionOne. The code I tried is listed below. A better approach maybe, if you want to create your own custom functions and use them later in scripts etc, try to create a module to house all those functions together. You can import all functions just by referencing that module file, more information: PowerShell Modules It makes it easier to maintain. If this solves your problem, do mark it as a response. If you have questions, do post a response with an error screenshot or message for reference. Sometimes the response gets mushed up in a giant paragraph, so I am posting the formatted text as a picture too

    function MyFunctionOne {
        $mycomps = Get-ADComputer -Filter * -Properties MemberOf, Description | Where-Object {[string]$_.MemberOf -notlike '*DKHOS-WSUS*'} | Select-Object -Property Name, Description 
        $mycomps.count()
    }
    
    function MyFunctionTwo {
        Write-Host "Function 2"
    }
    
    function main {
        
        $choice = 0
        do {
            $choice = Read-Host
            switch($choice){
            1 {MyFunctionOne break}
            2 {MyFunctionTwo}
            3 {$choice = 0}
    
            }
        
        }while($choice -ne 0)
    }
    
    main
    

  2. dashanan13 930 Reputation points
    2023-04-12T11:20:46.68+00:00

    Sometimes the response gets mushed up in a giant paragraph, so I am posting the formatted text as a picture too1

    0 comments No comments

  3. Rich Matheisen 47,901 Reputation points
    2023-04-12T18:57:07.53+00:00

    Your problem arises from your treating the "memberof" property a string. It's actually a collection (array) of the distinguishedNames of the groups in which an object (a computer, in this case) is a member.

    A safer way is to ignore the computers that are members of that group (using a NOT condition is often confusing, or at least not clear about your intention).

    Because the output from your function (FYI, the "main" function isn't necessary) is inside a "do" statement it won't be displayed until you exit that loop.

    Give this a try:

    function MyFunctionOne{
        $select = $true
        Get-ADComputer -Filter * -Properties MemberOf,description |
            ForEach-Object{
                $select = $true
                ForEach ($dn in $_.memberof){
                    if ($dn -like "*GroupName*"){
                        $select = $false
                        break
                    }
                }
                if ($select){
                    $_ | Select-Object name, Description
                }
        }
    }
    
    $choice = 0
    Import-Module -Global -Name Microsoft.PowerShell.Utility   # I though It was problem with module so I import it before execute command
        do{
            $choice = Read-Host "1 to run, 2 to exit"
            switch($choice){
                1 {MyFunctionOne; break}
                2 {$choice = 0}
            }
        }while($choice -ne 0)
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.