Creating user policy Powershell MicrosoftTeams session error

Viewed 214

When I try to create a user policy for my active directory I get this error:

Invoke-Command : Cannot validate argument on parameter 'Session'. The argument is null or empty. Provide an argument that is
 not null or empty, and then try the command again.
At C:\Program Files\WindowsPowerShell\Modules\MicrosoftTeams\2.3.1\net472\SfBORemotePowershellModule.psm1:22959 char:38
+ ...    -Session (Get-PSImplicitRemotingSession -CommandName 'New-CsApplic ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Invoke-Command], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.InvokeCommandCommand

The code that I'm using is this:

Import-Module MicrosoftTeams
# Get the credentials    
$password = ConvertTo-SecureString -AsPlainText -Force -String "password"
$credentials = New-Object System.Management.Automation.PsCredential("email", $password) 
    
# Connect to Microsoft Teams  

Connect-MicrosoftTeams -Credential $credentials
New-CsApplicationAccessPolicy -Identity Random -AppIds "appid" -Description "Users"
Grant-CsApplicationAccessPolicy -PolicyName Random -Identity "userObjectId"

I know that the command New-CsApplicationAccessPolicy is creating the error but my guess is that it's caused by the command Connect-MicrosoftTeams because from what I can understand is that Connect-MicrosoftTeams creates a session. Is there a way to set the session via a parameter or is this something you need to do outside this method?

0 Answers
Related