使用PowerShell导入电子邮件地址时,如何跳过csv中的空白单元格? [英] How to skip empty cells in a csv when using PowerShell to import Email Addresses?

查看:93
本文介绍了使用PowerShell导入电子邮件地址时,如何跳过csv中的空白单元格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试运行以下脚本以在Powershell中导入电子邮件地址:

I am trying to run the following script to import email addresses in powershell:

Import-CSV "C:\AliasesTest.csv" | foreach-object {
    Set-Mailbox -Display Name $_.Name -EmailAddresses @{add=$_.Alias1,$_.Alias2,$_Alias3}}

它工作正常,除非csv的Alias列之一下有一个空单元格,此时会产生以下错误:

It works fine, unless the csv has an empty cell under one of the Alias columns, at which point the following error is produced:

"The address '' is invalid: "" isn't a valid SMTP address..:

我该如何构造我的脚本以在遇到空单元格时忽略它们?

How can I construct my script to just ignore empty cells when it comes across them?

推荐答案

检查每个属性(别名)以查看其是否为空,并仅将具有值的属性添加到哈希表中的数组中:

Check each property (alias) to see if it is empty, and only add the ones with values to the array inside your hash table:

Import-CSV "c:\AliasesTest.csv" | ForEach-Object {
    #Save the CSV row for use in another loop later
    $CSV = $_

    Set-Mailbox -DisplayName $_.Name -EmailAddresses @{add = ("Alias1","Alias2","Alias3" | ForEach-Object { $Csv.$_ } | Where-Object{$_}) }
}

这种疯狂的行为是,创建一个新的哈希表,并使用键"add"创建一个具有子表达式值的哈希表.子表达式具有要检查的属性名称数组,以将其迭代,将每个名称转换为该属性的值,然后过滤掉空的属性名称.

What that craziness does is, create a new hashtable with a key "add" that has a value of a sub expression. The sub expression has an array of property names that you want to check that it iterates over, converting each name to the value of that property, then filters out the empty ones.

这篇关于使用PowerShell导入电子邮件地址时,如何跳过csv中的空白单元格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆