我的迁移上的预置型Exchange环境到Office 365和目前正在设法脚本一些迁移任务。一个特别的任务是组和组成员从的预置型环境导出为CSV,然后导入CSV数据到Office 365。
我创建了下面的脚本来尝试并导入组到Office 365:
# Import distribution groups from CSV to Office 365
Import-Csv c:\admin\exchange-migration\exports\distribution-groups.csv | foreach {
New-DistributionGroup -Name $_.name -Alias $_.alias -PrimarySmtpAddress $_.primarysmtpaddress -Type Distribution -RequireSenderAuthenticationEnabled ([System.Convert]::ToBoolean($_.requiresenderauthenticationenable))
}
我有与“requiresenderauthenticationenabled”部分作祟我不能一个字符串转换为一个布尔值,但一些研究后,我想出了[System.Convert]
选项。但是现在每个值被视为FALSE。
Mathias R. Jessen提供的重要指针:
你必须在属性名称的拼写错误,你传递给[System.Convert]::ToBoolean()
,这引起了属性引用始终计算$null
,其.NET方法始终转换$false
。
为了防止这样的错别字在未来,你可以设置Set-StrictMode -Version 2
或更高,这将报告发表声明终止错误,如果未初始化的变量或不存在的属性被引用:
Set-StrictMode -Version 2
# Try to reference a nonexistent property on an input object.
[pscustomobject] @{ one = 1; two = 2; three = 3 } | % { $_.four }
# -> Error "The property 'four' cannot be found on this object.
# Verify that the property exists"
然而不幸的是,这种严格的模式有可能有害的副作用:
.Count
/ .Length
properties on scalars,这是集合和标量的统一处理很重要。
如果你还不满意,还有所描述的行为,让您的声音在那里。除了错字,更简单的方法来强制一个字符串值,要么是true
或false
(在任何情况下的变化)为相应的布尔值是使用-eq 'true'
;例如。:
... -RequireSenderAuthenticationEnabled ($_.requiresenderauthenticationenabled -eq 'true')
这就是说,这仅仅是强大的,如果你能依靠的属性值仅限于字符串'true'
和'false'
- 你不会赶上意外值这种方式,如'yes'
。
顺便说一句,使用New-DistributionGroup
类型的参数,而不是使用[bool]
类型参数的[switch]
是PowerShell的约定一个不幸的偏差。
如果-RequireSenderAuthenticationEnabled
进行了适当类型为[switch]
:
代替
-RequireSenderAuthenticationEnabled $true
你只是通过开关名称本身:
-RequireSenderAuthenticationEnabled
和代替
-RequireSenderAuthenticationEnabled $false
你:
-RequireSenderAuthenticationEnabled:$var
- 注意开关名称和布尔值之间所需的:
。