【问题标题】:Do while Powershell issue在 Powershell 问题中执行
【发布时间】:2015-07-17 06:32:03
【问题描述】:

我正在尝试构建一个脚本来创建 AD 用户,然后为其分配 o365 许可证。我目前有创建用户作为交换的脚本,AD 没问题。但是,服务器每 15 分钟同步一次,以将用户凭据传送到 o365 客户端。这是我创建 do while 语句的地方,该语句应该等到它在 o365 中找到邮箱而不是分配许可证。但是,即使我能够在 o365 GUI 中找到用户,脚本也会挂起并且永远找不到用户。我是不是做错了什么?

$identAlias 设置为 "first name"+"."+"lastname"。所以它会像 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

【问题讨论】:

  • 嗯,首先,你不应该使用Format-Wide(你使用别名fw),你应该把它改成Select -ExpandSelect-Object -ExpandProperty的缩写。这将准确地设置@ 987654326@ 到 IsValid 属性的值而不是字符串表示形式。除此之外,您是否能够手动运行 get-mailboxpermission 命令并获得有效结果?
  • 不...当我运行获取邮箱权限时,它无法找到对象。不知道为什么

标签: powershell azure active-directory office365


【解决方案1】:

主要问题是Get-MailboxPermission 没有-Alias 参数。
请改用-Identity

-Identity <MailboxIdParameter>
Identity 参数标识邮箱。您可以使用以下值之一:
* GUID
* ADObjectID
* 专有名称 (DN)
* 域\帐户
* 用户主体名称 (UPN)
* 旧版ExchangeDN
* Smtp地址
* 别名

【讨论】:

  • 我可以改用 userprincipalname 吗?
  • nvm 刚刚发现我不能
  • -Identity 如果确实是邮箱上的别名,则应采用别名值。另外,我会完全关闭| fw IsValid。这是不必要的,但无论哪种方式都不会改变功能。
  • 我的问题是当我运行 get-mailboxpermission -identity 时。它说它找不到对象。它应该寻找什么细节?
  • 如果你运行$identAlias | GM它会返回什么?
【解决方案2】:
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;}                                                                                                            

【讨论】:

    猜你喜欢
    • 2018-02-22
    • 2020-11-07
    • 2019-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-25
    相关资源
    最近更新 更多