【问题标题】:Search and find data from CSV based on Criteria根据 Criteria 从 CSV 中搜索和查找数据
【发布时间】:2017-12-16 22:01:47
【问题描述】:

我正在创建 powershell 脚本来为企业用户帐户创建 Skype。 我正在尝试从 CSV 文件中查找可用编号,该文件查找状态、业务单位和位置以找到要使用的正确 LineUri。

我正在使用以下代码,它始终使用第一个 SPARE 编号,并且不验证位置和业务单位来查找线路 uri。

$path     = Split-Path -parent $MyInvocation.MyCommand.Definition
$newpath  = $path + "\PhoneData.csv"
$LineUri = @()
$Status = @()
$BusinessUnit = @()
$Location = @()

$csv = Import-Csv $newpath -Delimiter ","

$csv |`
    ForEach-Object {
        $LineUri += $_.LineUri
        $Status += $_.Status
        $BusinessUnit +=$_.BusinessUnit
        $Location +=$_.Location
    }

$currentStatus = "Spare"
$userBusinessUnit = "Support"
$userLocation = "WA"


if ($Status -eq $currentStatus -And $BusinessUnit -eq $userBusinessUnit -And $Location -eq $userLocation )
    {    
    $Where = [array]::IndexOf($Status, $currentStatus)
    $AvailableURI = $LineUri[$Where]    
    #Write-Host "Next Uri Available: " $LineUri[$Where]

    $changeWhere = [array]::IndexOf($LineUri, $AvailableURI)
    #Write-Host "Next Uri Available: " $Status[$changeWhere]


    Try
    {
        Enable-CsUser -identity sudip.sapkota -RegistrarPool "s4b-fe01.tapes.com" -SipAddressType SamAccountName -sipdomain "tapes.com"
        Set-CsUser -Identity sudip.sapkota -EnterpriseVoiceEnabled $true -LineURI $AvailableURI
        Grant-CsDialPlan -Identity sudip.sapkota -PolicyName 'DialPlan'
        Grant-CsVoicePolicy -Identity sudip.sapkota -PolicyName 'My VoicePolicy'

        Write-Host "[INFO]`t Lync Enterprise account for user sudip.sapkota has been created with sip : $AvailableURI" -ForegroundColor "Green"
              "[INFO]`t Lync Enterprise account for user sudip.sapkota has been created with sip : $AvailableURI" | Out-File $log -append

        $i = $changeWhere    
        $csv[$i].Status = 'Used'

        $csv | Export-Csv -Path $newpath -NoTypeInformation

        Write-Host "[INFO]`t PhoneData CSV has been updated" -ForegroundColor "Green"
              "[INFO]`t PhoneData CSV has been updated" | Out-File $log -append
    }

    catch
    {
        Write-Host "[ERROR]`t Oops, something went wrong: $($_.Exception.Message)`r`n" -ForegroundColor "Red" 
                   "[WARNING]`t Oops, something went wrong: $($_.Exception.Message)" | Out-File $log -append
    }

    }

else
{

  Enable-CsUser -identity sudip.sapkota -RegistrarPool "s4b-fe01.tapes.net" -SipAddressType SamAccountName -sipdomain "tapes.net"
  Write-Host "[INFO]`t No Spare LineURI, user has been created as PC-to-PC only" -ForegroundColor "Yellow"
          "[INFO]`t No Spare LineURI, user has been created as PC-to-PC only" | Out-File $log -append
}

我的 CSV 看起来像这样。

Name        LineUri                     Status  BusinessUnit    Location
Test 1      tel:+61396176100;ext=6100   Spare   Sales           VIC
Test 2      tel:+61396176101;ext=6101   Spare   Sales           VIC
Test 2      tel:+61396176102;ext=6102   Used    Sales           NSW
Test 2      tel:+61396176103;ext=6103   Spare   Support         WA
Test 2      tel:+61396176104;ext=6104   Spare   Support         WA
Test 2      tel:+61396176105;ext=6105   Used    Action          VIC
Test 2      tel:+61396176106;ext=6106   Spare   Suppot          VIC
Test 2      tel:+61396176107;ext=6107   Spare   Action          VIC

有人可以帮忙找出我的错误吗?

当我手动输入输入时,测试输入是

$currentStatus = "Spare"
$userBusinessUnit = "Support"
$userLocation = "WA"

所以我需要找到 LineURI,它是 SPARE,它的位置是 WA,它的 BusinessUnit 是 Support。

我应该得到 tel:+61396176103;ext=6103 作为 LineURI

【问题讨论】:

  • 你的预期输出是什么?
  • @gms0ulman 我已经更新了问题本身的输出

标签: powershell scripting powershell-2.0 powershell-3.0


【解决方案1】:

我稍微修改了你的逻辑。我觉得你可以在 foreach 循环中完成所有这些工作。为清楚起见,这确实会在循环中收集数据,然后为您留下一个可以使用的数组。您可以直接将您的操作嵌套在循环中并中断,或者您可以保持它这样,然后对数组项执行您的工作。

# Array of spare numbers that meet your criteria
$firstAvailableSpare

# criteria
$currentStatus = "Spare"
$userBusinessUnit = "Support"
$userLocation = "WA"


$csv |`
    ForEach-Object {
        $LineUri += $_.LineUri
        $Status += $_.Status
        $BusinessUnit +=$_.BusinessUnit
        $Location +=$_.Location
    $currentStatus = "Spare"
    $userBusinessUnit = "Support"
    $userLocation = "WA"


    if ( ($_.Status -eq $currentStatus) -And ($_.BusinessUnit -eq $userBusinessUnit) -And ($_.Location -eq $userLocation) ) { 
        $firstAvailableSpare = $_ # Assign the first
        $_.Status = "Used"

        continue # break the loop so we don't get additional hits

    }
}

$csv | Export-Csv -Path $newpath -NoTypeInformation
$firstAvailableSpare # <-- your first available spare
$firstAvailableSpare.LineUri # <-- the URI you want

编辑 1:针对 CSV 导出要求进行了更新

编辑 2:更改中断以继续。警告:由于某种原因,这会破坏 ISE 的调试功能。步进将停止,未来的断点将被忽略。我不知道为什么会这样,但它超出了您的目标范围。

【讨论】:

  • 这是完美的,但我如何更新 CSV 文件,以便状态可以从 SPARE 更改为 Used 用于特定 LineUri..
  • 更新了答案,增加了要求
  • 我在 foreach-object 中没有遇到过 break 与 continue 的有趣行为。我已经更新了脚本,继续解决这个问题。感兴趣的可以看这里:stackoverflow.com/questions/7760013/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-08-10
  • 1970-01-01
  • 1970-01-01
  • 2022-10-21
  • 2017-10-14
  • 1970-01-01
  • 2012-07-19
相关资源
最近更新 更多