CSV导入Powershell - AD

时间:2015-05-28 21:17:45

标签: powershell csv import

您好我正在运行以下代码来更新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的样子

TITLE,KNOWNAS,LASTNAME,声母,DIVISION,DEPARTMENTCODE,COMPANYREFNO,WORKTELEPHO NE,MOBILETELEPHONE,WORKPHONESHORTCODE,EMAILPRIMARY,FAX,街道,TOWNCITY,POSTZIPCOD E,COUNTRY,公司名称,REPORTSTOEMAIL,ACTUALOFFICE,DEPTNAME,办公室,COUNTRY2 ,COUNTR Y3 Mr,Peter,Smith,P,Executive,IT,PL,+ 44(0)1234 567 890 ,,, user1 @ testdomain.co.uk,+ 44(0)1234 567 890,Road,London, QWER,英国,COMPANY,j.smith,OfficeName,Computers,City,GB,826

提前致谢

2 个答案:

答案 0 :(得分:1)

在这种情况下测试字符串的更好方法是 [string] :: IsNullOrWhiteSpace()

试试这个:

if ([string]::IsNullOrWhiteSpace($_."FAX"){
    $fax = $null
} else {
    $fax = $_."FAX" 
}

答案 1 :(得分:0)

if ($_."FAX" -ne $null -and $_."FAX" -ne " "){
$fax = $_."FAX"
}
else {
$fax = $null
}