您好,我正在运行以下代码来更新AD中的用户信息
import-module activedirectory
$Users=Import-CSV C:\UserFeed.csv
foreach($_ in $Users)
{
if ($_."FAX" -ne $null){
$fax = $_."FAX"
}
else {
$fax = $null
}
if ($_."MOBILETELEPHONE" -ne $null){
$mobile = $_."MOBILETELEPHONE"
}
else {
$mobile = $null
}
if ($_."COUNTRY" -ne $null){
$country = $_."COUNTRY"
}
else {
$country = $null
}
$Mgr = Get-ADUser -Filter "SamAccountName -like '$($_."REPORTSTOEMAIL")'" |
Select -ExpandProperty DistinguishedName
Get-ADUser -filter "EmailAddress -like '$($_.EMAILPRIMARY)'" |
Set-ADUser -Replace @{physicalDeliveryOfficeName=($_."ACTUALOFFICE");Title=
($_."PERSONALJOBTITLE");department=($_."DeptName");company=
($_."COMPANYNAME");facsimileTelephoneNumber="$fax";StreetAddress=
($_."STREET");l=($_."TOWNCITY");postalCode=($_."POSTZIPCODE");Co=$country;C=
($_."COUNTRY2");CountryCode=($_."COUNTRY3");givenName=($_."KNOWNAS");sn=
($_."LASTNAME");telephoneNumber=($_."WORKTELEPHONE");mobile="$mobile
";manager="$Mgr"}
}问题出在CSV中,用户说没有传真号,字段为空,而不是在传真中什么也不放,而是放入空格字符。如何让它不为该字段设置任何内容。
示例请参见图片http://s22.postimg.org/nsf66ngc1/mobile.jpg
下面是CSV的样子
提前感谢
发布于 2015-05-29 08:18:28
在这种情况下,测试字符串的更好方法是使用string::IsNullOrWhiteSpace()
试试这个:
if ([string]::IsNullOrWhiteSpace($_."FAX"){
$fax = $null
} else {
$fax = $_."FAX"
}发布于 2015-05-29 05:22:33
if ($_."FAX" -ne $null -and $_."FAX" -ne " "){
$fax = $_."FAX"
}
else {
$fax = $null
}https://stackoverflow.com/questions/30517249
复制相似问题