powershell:if语句并在Import-CSV中使用空白单元格

发布于 2025-01-25 11:46:15 字数 649 浏览 2 评论 0原文

我很难使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ぇ气 2025-02-01 11:46:15

要重新提出您的问题,您只想搜索 subdepartment 不是空的?

如果不修改太多代码,则可以使用:: isnullorWhitespace()[string]类中提供的静态方法来评估空虚:

  • < em>使用-not逆转[String] :: IsNullorWhitespace($ obj.subdepartment) 的结果。
foreach ($obj in $onboardcsv)
{
    $department = if (-not [string]::IsNullOrWhiteSpace($obj.subDepartment)) {
        $obj.subDepartment
    }
    else {
        $obj.department
    }
    Get-ADOrganizationalUnit -Filter "Name -like '$department'" -SearchBase "OU=User,OU=OU,DC=DOMAIN,DC=com" 
}

因此,如果$ 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:

  • Using -Not reverses the result of [string]::IsNullOrWhiteSpace($obj.subDepartment).
foreach ($obj in $onboardcsv)
{
    $department = if (-not [string]::IsNullOrWhiteSpace($obj.subDepartment)) {
        $obj.subDepartment
    }
    else {
        $obj.department
    }
    Get-ADOrganizationalUnit -Filter "Name -like '$department'" -SearchBase "OU=User,OU=OU,DC=DOMAIN,DC=com" 
}

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.

空城旧梦 2025-02-01 11:46:15

这样的事情会起作用。

$ou = "searching by sub department"

$department = if (!($user.subDepartment)) {
        #subdepartment is blank
        #searching by department
        $ou = "searching by department"
    }

$ou

Something like this would work.

$ou = "searching by sub department"

$department = if (!($user.subDepartment)) {
        #subdepartment is blank
        #searching by department
        $ou = "searching by department"
    }

$ou
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文