SOLVED

powershell new-team does not return object in time

%3CLINGO-SUB%20id%3D%22lingo-sub-2502409%22%20slang%3D%22en-US%22%3Epowershell%20new-team%20does%20not%20return%20object%20in%20time%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2502409%22%20slang%3D%22en-US%22%3E%3CP%3EHello%2C%20i%20am%20having%20a%20problem%20using%20powershell%20to%20add%20new%20Teams.%20let%20me%20show%20you%20some%20example%20code%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3Eforeach(%24item%20in%20%24list)%7B%0A%20%20%20%20%20%20%20%20%23some%20stuff%20happens%20above%20thats%20working...%0A%0A%0A%20%20%20%20%20%20%20%20if%20(%24autoteam%20-eq%20%24null)%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20continue%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%24teamid%20%3D%20Get-team%20-DisplayName%20%24autoteam%0A%20%20%20%20%20%20%20%20if%20(%24teamid%20-eq%20%24null)%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%24teamid%20%3D%20new-team%20-displayname%20%24autoteam%20-owner%20%22john.doe%40contoso.com%22%20-template%20%22EDU_Class%22%20%7Cout-null%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%23this%20is%20my%20workaround%20%0A%20%20%20%20%20%20%20%20while%20(%24teamid%20-eq%20%24null)%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20sleep%20-Seconds%203%0A%20%20%20%20%20%20%20%20%20%20%20%20%24teamid%20%3D%20Get-team%20-DisplayName%20%24autoteam%20%0A%20%20%20%20%20%20%20%20%7D%0A%0A%20%20%20%20%20%20%20%20add-teamuser%20-groupid%20%24teamid.groupid%20-user%20%24upn%20%7Cout-null%0A%0A%20%20%20%20%7D%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThe%20Problem%20i%20cannot%20wrap%20my%20head%20around%20is%20as%20follows%3A%20when%20i%20use%20%22new-team%22%20in%20an%20interactive%20PS-Session%20it%20returns%20the%20groupid%20as%20it%20should%20(checked%20the%20manual).%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EWhen%20using%20it%20inside%20my%20script%20it%20returns%20nothing.%3C%2FP%3E%3CP%3Ei%20found%20a%20workaround%20in%20which%20i%20let%20the%20script%20wait%20some%20time%2C%20and%20after%20aprox.%203-5%20seconds%20the%20team%20is%20created%20and%20get-team%20returns%20what%20it%20should.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIt%20seems%20to%20me%20that%20the%20new-team%20cmdlet%20only%20waits%20till%20finished%20when%20in%20interactive%20shell%2C%20could%20that%20be%20the%20problem%3F%20and%20if%20so%2C%20how%20do%20you%20guys%20do%20this%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2502409%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EAdministrator%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EBest%20Practices%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EMicrosoft%20Teams%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2502858%22%20slang%3D%22en-US%22%3ERe%3A%20powershell%20new-team%20does%20not%20return%20object%20in%20time%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2502858%22%20slang%3D%22en-US%22%3EHello%2C%20Isn't%20the%20out-null%20causing%20that%3F%3C%2FLINGO-BODY%3E
New Contributor

Hello, i am having a problem using powershell to add new Teams. let me show you some example code:

 

 

 

 

foreach($item in $list){
        #some stuff happens above thats working...


        if ($autoteam -eq $null){
            continue
        }
        $teamid = Get-team -DisplayName $autoteam
        if ($teamid -eq $null){
            $teamid = new-team -displayname $autoteam -owner "john.doe@contoso.com" -template "EDU_Class" |out-null
        }
        #this is my workaround 
        while ($teamid -eq $null){
            sleep -Seconds 3
            $teamid = Get-team -DisplayName $autoteam 
        }

        add-teamuser -groupid $teamid.groupid -user $upn |out-null

    }

 

 

 

The Problem i cannot wrap my head around is as follows: when i use "new-team" in an interactive PS-Session it returns the groupid as it should (checked the manual).

 

When using it inside my script it returns nothing.

i found a workaround in which i let the script wait some time, and after aprox. 3-5 seconds the team is created and get-team returns what it should.

 

It seems to me that the new-team cmdlet only waits till finished when in interactive shell, could that be the problem? and if so, how do you guys do this?

 

2 Replies
best response confirmed by newboerg83 (New Contributor)
Solution
Hello, Isn't the out-null causing that?

@Andres Gorzelany 

 

OMG I'm feeling stupid, you're right. Its obvious.

 

Thanks Man.