Home > Enterprise >  Making phone calls with PowerShell and ADB for Android via looping through csv
Making phone calls with PowerShell and ADB for Android via looping through csv

Time:10-06

I'm trying to write a PowerShell script which uses ADB and intents to make some phone calls. My script works but it's not elegant or efficient. Currently I have to store the numbers and duration in variables then make the calls then write these details plus the date and time the call is made to a csv file. I also have to hard code in the number of calls. What I would like to do is have the numbers and durations in the csv file and then loop through leach line of the file making the calls and then writing the time back to the csv.

Can anyone suggest how I can amend my current script to do this. Thanks

$Number1 = "12345678"
$Duration1 = "2"
$Number2 = "23456789"
$Durationl2 = "5"

$fullname = "c:\temp\calls.csv"

Add-Content -Path $fullname -Value '"Dialled Number","Date/Time","Duration"'

adb shell am start -a android.intent.action.CALL -d tel:$Number1

$d = Get-Date -Format "dd/MM/yyyy HH:mm:ss"

Start-Sleep -Seconds $Duration1

adb shell input keyevent KEYCODE_ENDCALL

Add-Content -Path $fullname -Value "$Number1,$d,$Duration1"

Start-Sleep -Seconds 5

adb shell am start -a android.intent.action.CALL -d tel:$Number2

$d = Get-Date -Format "dd/MM/yyyy HH:mm:ss"

Start-Sleep -Seconds $Duration2

adb shell input keyevent KEYCODE_ENDCALL

CodePudding user response:

you can do:

#Store result in Array
$result = @(
    #Loop through csv
    foreach ($c in $csv){
        $measure = measure-command {
            #Call number
            adb shell am start -a android.intent.action.CALL -d tel:$($c.number)
            #Wait 
            start-sleep $c.duration
            #End call
            adb shell input keyevent KEYCODE_ENDCALL
        }
        $attrsHt = [ordered]@{
            number=$c.number
            duration=$c.duration
            measure=$measure.totalSeconds
        }
        new-object -typename psobject -property $attrsht
    }
)

#Output Data
$result | export-csv C:\result.csv -delimiter ";" -noclobber -notypeinformation -encoding:utf8

But I guess the measured time is pretty close to the specified duration.

  • Related