gpt4 book ai didi

powershell - CSV导入Powershell-AD

转载 作者:行者123 更新时间:2023-12-03 00:00:47 26 4
gpt4 key购买 nike

嗨,我正在运行下面的代码以更新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的样子

标题,KNOWNAS,姓氏,姓名首字母,部门,部门代码,公司REFNO,WORKTELEPHONE,移动电话,WORKPHONE短代码,电子邮件主,传真,街道,市镇, zip ,国家,国家/地区,公司,名称,报告办事处Y3 Peter,Smith,P,Executive,IT,PL,+ 44(0)1234567890,,user1 @ testdomain.co.uk,+ 44(0)1234567890,路,伦敦,QWER,美国Kingdom,COMPANY,j.smith,OfficeName,Computers,City,GB,826

提前致谢

最佳答案

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

试试这个:

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

关于powershell - CSV导入Powershell-AD,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30517249/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com