powershell:if语句并在Import-CSV中使用空白单元格
我很难使用CSV文件中的空白单元格使用IF语句。我正在尝试编写一个入职脚本,并将其从XLSX HR填充中提取信息(它将行需要的行复制到脚本中使用的CSV中)。为了获取OU路径,我使用部门名称。但是,有些用户有一个子部门,而另一些用户则没有,这些字段在HR发送的XLSX中留为空白。我可以通过在这些字段中输入N/A来使用它,但是如果其他技术不知道在CSV文件中执行此操作,则脚本将失败。因此,我想将其与空白字段一起使用。
这就是我在CSV字段中不使用N/A时尝试的
foreach ($obj in $onboardcsv){
$dep = "$($obj.department)"
$sdep = "$($obj.subDepartment)"
if ($null -eq $obj.subDepartment){
$ou = Get-ADOrganizationalUnit -Filter {name -like $dep} -SearchBase "OU=User,OU=OU,DC=DOMAIN,DC=com"
}
else{
$ou = Get-ADOrganizationalUnit -Filter {name -like $sdep} -SearchBase "OU=User,OU=OU,DC=DOMAIN,DC=com"
}
任何帮助!
I am having difficulty using an if statement with blank cells in my CSV file. I'm attempting to write an onboarding script and have it pull info from an xlsx HR fills out (IT copies needed rows into CSV that is used in script). In order to get the OU path, I use the department names. However, some users have a sub-department and others do not, these fields are left blank in the xlsx that HR sends. I have it working by inputting a N/A in those fields however if another tech doesn't know to do that in the CSV file the script will fail. So I would like to get it working with the blank fields.
This is what im trying when not using the N/A in the CSV field
foreach ($obj in $onboardcsv){
$dep = "$($obj.department)"
$sdep = "$($obj.subDepartment)"
if ($null -eq $obj.subDepartment){
$ou = Get-ADOrganizationalUnit -Filter {name -like $dep} -SearchBase "OU=User,OU=OU,DC=DOMAIN,DC=com"
}
else{
$ou = Get-ADOrganizationalUnit -Filter {name -like $sdep} -SearchBase "OU=User,OU=OU,DC=DOMAIN,DC=com"
}
Any help would be appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要重新提出您的问题,您只想搜索 subdepartment 不是空的?
如果不修改太多代码,则可以使用
:: isnullorWhitespace()
在[string]
类中提供的静态方法来评估空虚:-not
逆转[String] :: IsNullorWhitespace($ obj.subdepartment)
的结果。因此,如果
$ obj.subdepartment
不是 null ,请针对子部门进行测试。 。这将允许仅在两个属性中使用一个变量,并且无需复制代码。感谢 @santiago 用于理智检查。
To rephrase your question, you just want to search where SubDepartment isn't empty?
Without modifying too much of your code, you can make use of the static method of
::IsNullOrWhiteSpace()
provided in the[string]
class to evaluate against the emptiness:-Not
reverses the result of[string]::IsNullOrWhiteSpace($obj.subDepartment)
.So, testing against the subDepartment first, if
$obj.subDepartment
is not null, assign it to$department
. This will allow the use of just one variable for both properties, and no code copying necessary.Thanks to @Santiago for a sanity check.
这样的事情会起作用。
Something like this would work.