使用powershell创建具有多个分隔符的csv

时间:2010-11-17 06:02:55

标签: powershell

我承认我是一个PowerShell n00b,但需要根据来自novell目录的ldap信息在广告中创建12k个已启用邮件的联系人。我一直试图操纵下面的代码以满足需要,但我试图拆分用户对象的'mail'属性:

$gwise = "GWISE"
$comma = ","
$dot = "."
$space = " "
$gwcontacts = "C`:\Windows\System32\adfind.exe `-hh nds.groupwise.local `-simple `-s subtree `-nodn `-csv empty `-csvnoq `-b `"OU`=people,OU`=GroupWise,O`=local`" `-f mail=* mail givenName sn | findstr /i /c`:`"@`" | sort" | cmd | ConvertFrom-Csv -UseCulture -Header $header

alisdair向我展示了如何在powershell中进行替换,但是现在我很难弄清楚如何解析mail属性以便我只能使用@字符左边的字符串:

$gwcontacts | ForEach-Object { New-MailContact -Name ($_.mail.split('@') + $dot + $gwise) -Alias ($_.mail.split('@')) -DisplayName ($_.sn + $comma + $space+ $_.givenName) -ExternalEmailAddress ($_.mail.Replace('GroupWise.local','example.com')) -FirstName $_.givenName -LastName $_.sn -OrganizationalUnit 'exchange.local/contacts' -PrimarySmtpAddress ($_.mail.replace('GroupWise.local','example.com')) }
  

Invoke-Command:无法将参数'Alias'绑定到目标。异常设置“别名”:“属性表达式”TESTUSER GroupWise.local“无效。有效值为:字符串形成的字符从A到Z(大写或小写),数字从0到9,!,#,$, %,&amp;,',*,+, - ,/,=,?,^,_,, {, |, } or ~. One or more periods may be embedded in an alias, but each period should be preceded and followed by at least one of the other characters. Unicode characters from U+00A1 to U+00FF are also valid in an alias, but they will be mapped to a best-fit US-ASCII string in the e-mail address, which is generated from such an alias." At C:\Users\Administrator\AppData\Roaming\Microsoft\Exchange\RemotePowerShell\ems.exchange.local\ems.exchange.local.psm1:28521 char:29 + $scriptCmd = { & <<<< $script:InvokeCommand       + CategoryInfo:WriteError:(:) [New-MailContact],ParameterBindingException       + FullyQualifiedErrorId:ParameterBindingFailed,Microsoft.Exchange.Management.RecipientTasks.NewMailContact

我知道我的分割语法不正确......但是,我迷路了。任何帮助表示赞赏!

1 个答案:

答案 0 :(得分:0)

我不完全确定我读过这个权利,但你想要做的是采用GroupWise地址,并为每个用户用适当的互联网域替换地址的本地Groupwise部分?

以单行完成所有操作(假设$ gwaddress是一个分组地址,如user @ local):

$internetaddress = $gwaddress.remove($gwaddress.indexof('@')).insert($gwaddress.indexof('@'),'@mydomain.com')

上面的代码采用分组地址,从@开始删除所有内容,然后在@原始字符串中附加您的域名。

或者,如果您只想将分组地址拆分为用户和域,并以另一种方式构建Internet地址:

$gwuser,$gwdomain=$gwaddress.split('@')
相关问题