2017-07-12 70 views
0

我正在創建用於爲業務用戶帳戶創建Skype的PowerShell腳本。 我正試圖從CSV文件中查找狀態,業務單位和位置以查找要使用的正確LineUri的可用編號。根據條件從CSV中搜索和查找數據

我正在使用以下代碼,它始終使用第一個備用號碼,並且不驗證位置和業務單位以查找行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這是備用的,其位置是WA,其BusinessUnit是支持。

我應該得到電話:61396176103; EXT = 6103作爲LineURI

+0

您的預期產出是? – gms0ulman

+0

@ gms0ulman我已經在問題本身更新了我的輸出 –

回答

0

我返工你的邏輯有點。我覺得你可以在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的調試功能。步進將停止,未來的突破點將被忽略。我不知道這是爲什麼,但它超出了你的目標。

+0

這是完美的,但是如何更新CSV文件以便狀態可以從SPARE更改爲用於該特定LineUri .. –

+0

更新回答附加要求 –

+0

它不更新CSV .. –

相關問題