2015-05-06 78 views
0

我正在嘗試構建一個腳本來創建AD用戶,併爲其分配一個o365許可證。我目前有創建一個用戶交換和AD沒有問題的腳本。但服務器每15分鐘同步一次以將用戶憑據帶到o365客戶端。這是我創建do的語句,它應該等待,直到它在o365中找到郵箱而不是分配許可證。但是,即使在我能夠在o365 GUI中找到用戶後,該腳本仍然會掛起並且從未找到用戶。難道我做錯了什麼?儘管Powershell問題

$ identAlias設置爲「名字」+「。」+「姓氏」。所以它會像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 
+0

嗯,首先,你不應該使用'格式Wide'(使用人ias'fw'),您應該將其更改爲'Select -Expand'(Select-Object -ExpandProperty'的縮寫。這將準確地將'$ mailboxexists'設置爲'IsValid'屬性的值,而不是字符串表示。除此之外,您是否可以手動運行「get-mailboxpermission」命令並獲得有效的結果? – TheMadTechnician

+0

nope ...當我運行獲取郵箱權限時,它無法找到對象。不知道爲什麼 –

回答

1

的主要問題是,Get-MailboxPermission沒有-Alias參數。
改爲使用-Identity

-Identity <MailboxIdParameter>
Identity參數標識郵箱。您可以使用下列值之一:
* GUID
* ADO對象
*分辨名稱(DN)
*域\帳戶
*用戶主體名稱(UPN)
*舊版Exchange
* Smtp地址
*別名

+0

我能夠使用userprincipalname? –

+0

nvm剛剛發現我不能 –

+0

'-Identity'應該取別名值,如果它真的是郵箱上的別名。另外,我只需要'| fw IsValid'完全關閉。這是不必要的,但它不會以任何方式改變功能。 –

0
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;}