0

我对编码和 Powershell 非常陌生。我正在尝试从 csv 输入中获取数据,从每一行中解析组,并根据组是否存在运行一个函数。如果存在多个组,那么我会查找优先级顺序并根据该优先级运行特定功能。

Import-CSV 以创建 $Records。当显示 $Records 的内容时,它看起来像:

$Records
 username1 "groupAll" "groupS"
 username2 "groupAll" "groupS" "groupL"
 username3 "groupAll" "groupS"
 username4 "groupAll" "groupA"
 username5 "groupAll" "groupS" "groupE"
 username6 "groupALL"
 username7 "groupALL" "groupE"
 username8 "groupALL" "groupA" "groupE"
 username9 "groupALL" "groupA" "groupE" "groupS"

需要解析每一行,并根据组的存在和顺序运行特定的函数。

\\[i.e.]//
username1 "groupAll" "groupS"
- Does the list of groups contain "groupD"? \\ [No] //
- Does the list of groups contain "groupL"? \\ [No] //
- Does the list of groups contain "groupS"? \\[Yes]//
-- Do functionD, stop processing username1, and restart loop with username2

username2 "groupAll" "groupS" "groupL"
- Does the list of groups contain "groupD"?  \\ [No] //
- Does the list of groups contain "groupL"?  \\[Yes]//
-- Do functionL, Do FunctionS, stop processing username2, and restart loop with username3

username6 "groupALL"
- Does the list of groups contain "groupD"? \\ [No] //
- Does the list of groups contain "groupL"? \\ [No] //
- Does the list of groups contain "groupS"? \\ [No] //
- Does the list of groups contain "groupE"? \\ [No] //
- Does the list of groups contain "groupA"? \\ [No] //
- Does the list of groups contain "groupNL"? \\ [No] //
- Does the list of groups contain "groupALL"? \\[Yes]//
-- Do PrintingDisabled, stop processing username6, and restart loop with username7

我尝试了 ForEach-Object 循环、For-Each 循环和 Switch 循环,但似乎都没有达到预期的行为。

ForEach-对象

Function Set-Quotas {
    $Records = "Records.csv"
    $Delimiter = "|"
    Write-Log "Importing new user records.";
    Write-Host "Importing new user records.";
    $Roles = @(Import-CSV -Path $Records -Delimiter $Delimiter | Group-Object -Property user_name | Select-Object Name, @{n='Groups';e={$_.group.group_name}})
    $UserN = $($Roles.name)
    :Users $Roles | ForEach-Object {
    if ($Roles.Groups -contains "groupD") {Write-Log "$UserN - D Role"; FunctionD; break Users}
    Elseif ($Roles.Groups -contains "groupL") {Write-Log "$UserN - L Role"; FunctionL; FunctionS; break Users}
    Elseif ($Roles.Groups -contains "groupS") {Write-Log "$UserN - S Role"; FunctionS; break Users}
    Elseif ($Roles.Groups -contains "groupE") {Write-Log "$UserN - E Role"; FunctionE; break Users}
    Elseif ($Roles.Groups -contains "groupA") {Write-Log "$UserN - A Role"; FunctionA; break Users}
    Elseif ($Roles.Groups -contains "groupNL") {Write-Log "$UserN - NL Role"; FunctionNL; break Users}
    Elseif ($Roles.Groups -contains "groupALL") {Write-Log "$UserN - Default Role"; PrintingDisabled; break Users}
    }
    Return $UserN;
}

转变

Function Set-Quotas {
    $Records = "Records.csv"
    $Delimiter = "|"
    Write-Log "Importing new user records.";
    Write-Host "Importing new user records.";
    $Roles = @(Import-CSV -Path $Records -Delimiter $Delimiter | Group-Object -Property user_name | Select-Object Name, @{n='Groups';e={$_.group.group_name}})
    $UserN = $($Roles.name)
    Switch -File $Roles {
        "groupD" {Write-Log "$UserN - D Role"; FunctionD; break}
        "groupL" {Write-Log "$UserN - L Role"; FunctionL; FunctionS; break}
        "groupS" {Write-Log "$UserN - S Role"; FunctionS; break}
        "groupE" {Write-Log "$UserN - E Role"; FunctionE; break}
        "groupA" {Write-Log "$UserN - A Role"; FunctionA; break}
        "groupNL" {Write-Log "$UserN - NL Role"; FunctionNL; break}
        "groupALL" {Write-Log "$UserN - Default Role"; PrintingDisabled; break}
    }
    Return $UserN;
}

当前输出似乎没有单独处理每一行。而不是日志读取类似:

\\[desired log outcome]//
Importing new user records.
username1 - S Role
username2 - L Role
username3 - S Role
username6 - Default Role

我实际上在日志中得到的是这样的:

Importing new user records.
username1 username2 username3 ...

or

Importing new user records.
\\ [ no other lines added to log]//

有可能得到我想要的结果吗?如果是这样,我错过了什么或做错了什么导致它似乎没有按需要解析每一行?我花了很多时间尝试不同的方法来编写这个脚本,但他们每个人都没有按照我的意愿去做。本质上,我希望脚本做我想让它做的事情,而不是我表面上告诉它做的事情。谢谢您的帮助。


现在似乎有效的方法(2020 年 5 月 20 日):

Function Set-Quotas {
    $Records = "Records.csv"
    $Delimiter = "|"
    Write-Log "Importing new user records.";
    Write-Host "Importing new user records.";
    $Roles = @(Import-CSV -Path $Records -Delimiter $Delimiter | select -Skip 1 | Group-Object -Property user_name | Select-Object Name, @{n='Groups';e={$_.group.group_name}})
    :Users Foreach ($Role in $Roles){
    $UserN = $($Role.name)
    if ($Roles.Groups -contains "groupD") {Write-Log "$UserN - D Role"; Write-Host "$UserN - D Role"; FunctionD; Continue Users}
    Elseif ($Role.Groups -contains "groupL") {Write-Log "$UserN - L Role"; Write-Host "$UserN - L Role"; FunctionL; FunctionS; Continue Users}
    Elseif ($Role.Groups -contains "groupS") {Write-Log "$UserN - S Role"; Write-Host "$UserN - S Role"; FunctionS; Continue Users}
    Elseif ($Role.Groups -contains "groupE") {Write-Log "$UserN - E Role"; Write-Host "$UserN - E Role"; FunctionE; Continue Users}
    Elseif ($Role.Groups -contains "groupA") {Write-Log "$UserN - A Role"; Write-Host "$UserN - A Role"; FunctionA; Continue Users}
    Elseif ($Role.Groups -contains "groupNL") {Write-Log "$UserN - NL Role"; Write-Host "$UserN - NL Role"; FunctionNL; Continue Users}
    Elseif ($Role.Groups -contains "groupALL") {Write-Log "$UserN - Default Role"; Write-Host "$UserN - Default Role"; PrintingDisabled; Continue Users}
    }
    Return $UserN;
}

Write-Host仅在我对其进行故障排除时出现在代码中。稍后我可以进行查找/替换并将其注释掉。

现在它似乎以这种方式工作,我将尝试获取它,以便我可以修改 a 的代码,ForEach-Object {...最终目标为ForEach-Object -Parallel {.... 虽然,这对我的日志记录知识造成了影响。

4

0 回答 0