根据条件从CSV中搜索和查找数据

时间:2017-07-12 12:56:34

标签: powershell scripting powershell-v2.0 powershell-v3.0

我正在创建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"

所以我需要找到SPAR的LineURI,它的位置是WA,其BusinessUnit是支持。

我应该获得电话:+61396176103; ext = 6103为LineURI

1 个答案:

答案 0 :(得分:0)

我稍微改写了你的逻辑。我觉得你可以在foreach循环中完成所有这些工作。为清楚起见,这确实收集了循环中的数据,然后为您提供了一个可以使用的数组。你可以直接在循环中嵌入你的动作并突破,或者你可以像这样保持它,然后在数组项后执行你的工作。

Dim servicebase As ServiceBase = _oModule.GetService(ServiceType.DSPBASE)
If servicebase IsNot Nothing Then
    ' handle data received from the dispatch base component
    servicebase.Register(EventHandler_DSPBASE)  ' <- error here - red squiggly line in parenthesis
    servicebase.SetDispatchBaseOption(12500, DispatchBase.ROUTE_DATA_RECEIVED)
End If

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

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