Tyler S
Tyler S

Reputation: 877

Do while Powershell issue

I am trying to build a script that creates the AD user and than assigns it an o365 license. I currently have the script that creates a user in exchange and AD no problem. however the server syncs up every 15 minutes to bring over the users credentials to the o365 client. This is where I have created the do while statement that is supposed to wait till it finds the mailbox in o365 than assign the license. However the script hangs and never finds the user even after i am able to locate the user in the o365 GUI. Am i doing something wrong?

$identAlias is set to being the "first name"+"."+"lastname". so it would be like Steve.richard

$Cred = New-Object System.Management.Automation.PsCredential($User,(ConvertTo-SecureString $Pass -AsPlainText -Force))
Import-Module MSOnline
Connect-MsolService -Credential $Cred
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell/ -Credential $Cred -Authentication Basic -AllowRedirection
Import-PSSession $Session

do {
    sleep -seconds 1
    $mailboxExists = get-mailboxpermission -Alias $identAlias -ErrorAction SilentlyContinue |fw IsValid
    write-host "." -nonewline
} while (!$mailboxExists)

Set-MsolUserLicense -UserPrincipalName ($OnMicro + ".onmicrosoft.com") -AddLicenses company:ENTERPRISEPACK

Upvotes: 0

Views: 229

Answers (2)

Booga Roo
Booga Roo

Reputation: 1781

The main issue is that Get-MailboxPermission does not have the -Alias parameter.
Use -Identity instead.

-Identity <MailboxIdParameter>
The Identity parameter identifies the mailbox. You can use one of the following values:
* GUID
* ADObjectID
* Distinguished name (DN)
* Domain\Account
* User principal name (UPN)
* LegacyExchangeDN
* SmtpAddress
* Alias

Upvotes: 1

Tyler S
Tyler S

Reputation: 877

tyler.test

PS C:\WINDOWS\system32> C:\Scripts\if.ps1


   TypeName: System.String

Name             MemberType            Definition                                                                                                                   
----             ----------            ----------                                                                                                                   
Clone            Method                System.Object Clone(), System.Object ICloneable.Clone()                                                                      
CompareTo        Method                int CompareTo(System.Object value), int CompareTo(string strB), int IComparable.CompareTo(System.Object obj), int ICompara...
Contains         Method                bool Contains(string value)                                                                                                  
CopyTo           Method                void CopyTo(int sourceIndex, char[] destination, int destinationIndex, int count)                                            
EndsWith         Method                bool EndsWith(string value), bool EndsWith(string value, System.StringComparison comparisonType), bool EndsWith(string val...
Equals           Method                bool Equals(System.Object obj), bool Equals(string value), bool Equals(string value, System.StringComparison comparisonTyp...
GetEnumerator    Method                System.CharEnumerator GetEnumerator(), System.Collections.Generic.IEnumerator[char] IEnumerable[char].GetEnumerator(), Sys...
GetHashCode      Method                int GetHashCode()                                                                                                            
GetType          Method                type GetType()                                                                                                               
GetTypeCode      Method                System.TypeCode GetTypeCode(), System.TypeCode IConvertible.GetTypeCode()                                                    
IndexOf          Method                int IndexOf(char value), int IndexOf(char value, int startIndex), int IndexOf(char value, int startIndex, int count), int ...
IndexOfAny       Method                int IndexOfAny(char[] anyOf), int IndexOfAny(char[] anyOf, int startIndex), int IndexOfAny(char[] anyOf, int startIndex, i...
Insert           Method                string Insert(int startIndex, string value)                                                                                  
IsNormalized     Method                bool IsNormalized(), bool IsNormalized(System.Text.NormalizationForm normalizationForm)                                      
LastIndexOf      Method                int LastIndexOf(char value), int LastIndexOf(char value, int startIndex), int LastIndexOf(char value, int startIndex, int ...
LastIndexOfAny   Method                int LastIndexOfAny(char[] anyOf), int LastIndexOfAny(char[] anyOf, int startIndex), int LastIndexOfAny(char[] anyOf, int s...
Normalize        Method                string Normalize(), string Normalize(System.Text.NormalizationForm normalizationForm)                                        
PadLeft          Method                string PadLeft(int totalWidth), string PadLeft(int totalWidth, char paddingChar)                                             
PadRight         Method                string PadRight(int totalWidth), string PadRight(int totalWidth, char paddingChar)                                           
Remove           Method                string Remove(int startIndex, int count), string Remove(int startIndex)                                                      
Replace          Method                string Replace(char oldChar, char newChar), string Replace(string oldValue, string newValue)                                 
Split            Method                string[] Split(Params char[] separator), string[] Split(char[] separator, int count), string[] Split(char[] separator, Sys...
StartsWith       Method                bool StartsWith(string value), bool StartsWith(string value, System.StringComparison comparisonType), bool StartsWith(stri...
Substring        Method                string Substring(int startIndex), string Substring(int startIndex, int length)                                               
ToBoolean        Method                bool IConvertible.ToBoolean(System.IFormatProvider provider)                                                                 
ToByte           Method                byte IConvertible.ToByte(System.IFormatProvider provider)                                                                    
ToChar           Method                char IConvertible.ToChar(System.IFormatProvider provider)                                                                    
ToCharArray      Method                char[] ToCharArray(), char[] ToCharArray(int startIndex, int length)                                                         
ToDateTime       Method                datetime IConvertible.ToDateTime(System.IFormatProvider provider)                                                            
ToDecimal        Method                decimal IConvertible.ToDecimal(System.IFormatProvider provider)                                                              
ToDouble         Method                double IConvertible.ToDouble(System.IFormatProvider provider)                                                                
ToInt16          Method                int16 IConvertible.ToInt16(System.IFormatProvider provider)                                                                  
ToInt32          Method                int IConvertible.ToInt32(System.IFormatProvider provider)                                                                    
ToInt64          Method                long IConvertible.ToInt64(System.IFormatProvider provider)                                                                   
ToLower          Method                string ToLower(), string ToLower(cultureinfo culture)                                                                        
ToLowerInvariant Method                string ToLowerInvariant()                                                                                                    
ToSByte          Method                sbyte IConvertible.ToSByte(System.IFormatProvider provider)                                                                  
ToSingle         Method                float IConvertible.ToSingle(System.IFormatProvider provider)                                                                 
ToString         Method                string ToString(), string ToString(System.IFormatProvider provider), string IConvertible.ToString(System.IFormatProvider p...
ToType           Method                System.Object IConvertible.ToType(type conversionType, System.IFormatProvider provider)                                      
ToUInt16         Method                uint16 IConvertible.ToUInt16(System.IFormatProvider provider)                                                                
ToUInt32         Method                uint32 IConvertible.ToUInt32(System.IFormatProvider provider)                                                                
ToUInt64         Method                uint64 IConvertible.ToUInt64(System.IFormatProvider provider)                                                                
ToUpper          Method                string ToUpper(), string ToUpper(cultureinfo culture)                                                                        
ToUpperInvariant Method                string ToUpperInvariant()                                                                                                    
Trim             Method                string Trim(Params char[] trimChars), string Trim()                                                                          
TrimEnd          Method                string TrimEnd(Params char[] trimChars)                                                                                      
TrimStart        Method                string TrimStart(Params char[] trimChars)                                                                                    
Chars            ParameterizedProperty char Chars(int index) {get;}                                                                                                 
Length           Property              int Length {get;}                                                                                                            

Upvotes: 0

Related Questions