tag:blogger.com,1999:blog-4895679265618109180.post4230515647483477884..comments2024-03-02T22:32:43.182-07:00Comments on ucomsGeek: PowerShell Script to Backup AudioCodes Gateways (updated for 7.0 and 7.2)Jonathan McKinneyhttp://www.blogger.com/profile/05276135243139498379noreply@blogger.comBlogger22125tag:blogger.com,1999:blog-4895679265618109180.post-58924468594532745712018-05-01T08:30:04.343-06:002018-05-01T08:30:04.343-06:00From what I see there I suspect two problems. One ...From what I see there I suspect two problems. One looks like you might have a firewall or something killing the connection when trying to authenticate/detect the .ini filenane. The second seems like you might not have the directory created that is used in the script, but if it works for 7.2 gateway that might be a red herring and related to not getting the .ini file correctly downloaded. Jonathan McKinneyhttps://www.blogger.com/profile/05276135243139498379noreply@blogger.comtag:blogger.com,1999:blog-4895679265618109180.post-72690363400876791492018-05-01T08:06:49.874-06:002018-05-01T08:06:49.874-06:00Hello,
Have an issue with the script not being abl...Hello,<br />Have an issue with the script not being able to backup MP gateways (v 6.60A.342.003) but the same script is backing up Medians (v 7.20A.158.012). The script finishes with:<br /><br /> % Total % Received % Xferd Average Speed Time Time Time Current<br /> Dload Upload Total Spent Left Speed<br />100 156 0 156 0 0 586 0 --:--:-- --:--:-- --:--:-- 586<br /> % Total % Received % Xferd Average Speed Time Time Time Current<br /> Dload Upload Total Spent Left Speed<br />100 13917 0 13917 0 0 35684 0 --:--:-- --:--:-- --:--:-- 35684<br /><br /> Status Code 203<br /><br /> Forms Authentication<br /><br /> % Total % Received % Xferd Average Speed Time Time Time Current<br /> Dload Upload Total Spent Left Speed<br />100 160 0 122 100 38 389 121 --:--:-- --:--:-- --:--:-- 511<br /> % Total % Received % Xferd Average Speed Time Time Time Current<br /> Dload Upload Total Spent Left Speed<br />100 80 0 0 0 80 0 0 --:--:-- 0:02:00 --:--:-- 0<br />curl: (56) Recv failure: Connection was reset<br /><br /> Detecting .ini name<br /><br /> % Total % Received % Xferd Average Speed Time Time Time Current<br /> Dload Upload Total Spent Left Speed<br />100 13917 0 13917 0 0 37112 0 --:--:-- --:--:-- --:--:-- 37112<br /><br /> Detected Board Filename<br /><br /> Backing up gateway.domain.local<br /><br /> % Total % Received % Xferd Average Speed Time Time Time Current<br /> Dload Upload Total Spent Left Speed<br /> 0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0<br />Warning: Failed to create the file D:\Scripts\AudioCodes\: No<br />Warning: such file or directory<br />curl: (23) Failed writing received data to disk/application<br /><br /> % Total % Received % Xferd Average Speed Time Time Time Current<br /> Dload Upload Total Spent Left Speed<br />100 13917 0 13917 0 0 38766 0 --:--:-- --:--:-- --:--:-- 38766<br /><br /><br />Any help would be greatly appreciated.<br />Kind regards<br />Tobie<br />Tobie Fyshhttps://www.blogger.com/profile/00257880150199305678noreply@blogger.comtag:blogger.com,1999:blog-4895679265618109180.post-2016424300719212532017-08-18T07:32:22.937-06:002017-08-18T07:32:22.937-06:00Yea I had a note about that above... I struggle wi...Yea I had a note about that above... I struggle with it because some people like to add that to their profile insteadJonathan McKinneyhttps://www.blogger.com/profile/05276135243139498379noreply@blogger.comtag:blogger.com,1999:blog-4895679265618109180.post-58606360896497418412017-08-18T07:30:37.978-06:002017-08-18T07:30:37.978-06:00This comment has been removed by the author.Jonathan McKinneyhttps://www.blogger.com/profile/05276135243139498379noreply@blogger.comtag:blogger.com,1999:blog-4895679265618109180.post-16235908092930647582017-08-18T06:16:37.260-06:002017-08-18T06:16:37.260-06:00Added Remove-Item alias:curl to the script.
Now is...Added Remove-Item alias:curl to the script.<br />Now is works! :DJakob Raaskou Skakkehttps://www.linkedin.com/in/skakke/noreply@blogger.comtag:blogger.com,1999:blog-4895679265618109180.post-80804074677608084872017-08-18T06:00:03.378-06:002017-08-18T06:00:03.378-06:00Hi Jonathan,
Awesome script... but I'm having...Hi Jonathan,<br /><br />Awesome script... but I'm having issues executing it from Task Scheduler. Any ideas how make cURL functionality available from within there? This seems to be the issue:<br /><br />Invoke-WebRequest : Parameter cannot be processed because the parameter name 'o' is ambiguous. Possible matches<br />include: -OutFile -OutVariable -OutBuffer.<br />At C:\Scripts\Backup-Audiocodes.ps1:21 char:24<br />+ $statuscode = curl -o null.txt $loginurl -w '%{http_code}'<br />+ ~~<br /> + CategoryInfo : InvalidArgument: (:) [Invoke-WebRequest], ParameterBindingException<br /> + FullyQualifiedErrorId : AmbiguousParameter,Microsoft.PowerShell.Commands.InvokeWebRequestCommandJakob Raaskou Skakkehttps://www.linkedin.com/in/skakke/noreply@blogger.comtag:blogger.com,1999:blog-4895679265618109180.post-65078735400643881052017-08-01T15:55:21.093-06:002017-08-01T15:55:21.093-06:00so right off the bat... your $backuppath has a fil...so right off the bat... your $backuppath has a filename indicated. That should only be a directory like "d:\scripts\backup\"Jonathan McKinneyhttps://www.blogger.com/profile/05276135243139498379noreply@blogger.comtag:blogger.com,1999:blog-4895679265618109180.post-26050337573429731042017-08-01T14:39:58.446-06:002017-08-01T14:39:58.446-06:00function BackupGatewayAPI ($un, $pw, $ad, $path)
{...function BackupGatewayAPI ($un, $pw, $ad, $path)<br />{<br /> $backupurlsc = $null<br /> $backupurlini = $null<br /> $backupfilename = $null<br /> $securepw = $null<br /> $cred = $null<br /><br /> $backupurlsc = "https://" + $ad + "/api/v1/actions/saveConfiguration"<br /> $backupurlini = "https://" + $ad + "/api/v1/files/ini"<br /> $addressfilename = $ad.Replace(“.”,”-”)<br /> $backupfilename = $path + "BOARD.ini"<br /> Write-Host $backupfilename<br /> $securepw = ConvertTo-SecureString -String $pw -AsPlainText -Force<br /> $cred = new-object System.Management.Automation.PSCredential ($un, $securepw)<br /> Invoke-RestMethod $backupurlsc -Method post -Credential $cred<br /> Invoke-RestMethod $backupurlini -Method get -Credential $cred -OutFile $backupfilename -verbose<br /> <br /> Write-Host `r<br />}<br /><br /> <br /> # Function to Logoff Audiocodes with Forms Auth after finishing work<br />function LogoffForms ($ad)<br />{<br /> $logoffurl = $null<br /> $logoffwebpage = $null<br /> <br /> $logoffurl = "https://" + $ad + "/PressLogOff"<br /> $logoffwebpage = curl $logoffurl -b cookiejar.txt<br /> Remove-Item cookiejar.txt<br />}<br /><br /> <br /> # Main Script<br /><br /> $formsbasedauth = DetectAuthType $address<br /><br /> if ($formsbasedauth -eq "forms")<br />{<br /> AuthenticateForms $username $password $address<br /> $filename = DetectFilenameForms $address<br /> BackupGatewayForms $address $backuppath $filename<br /> LogoffForms $address<br /> <br />}<br />ElseIf ($formsbasedauth -eq "basic")<br />{<br /> BackupBasic $username $password $address $backuppath<br /> <br />}<br />ElseIf ($formsbasedauth -eq "api")<br />{<br /> BackupGatewayAPI $username $password $address $backuppath<br /> <br />}<br />Else<br />{<br /> Write-Host "Something went really wrong... couldn't detect Authentication type... Might I suggest Wireshark"<br />}Anonymoushttps://www.blogger.com/profile/14456490538290033748noreply@blogger.comtag:blogger.com,1999:blog-4895679265618109180.post-22734198404174335132017-08-01T14:39:27.315-06:002017-08-01T14:39:27.315-06:00# Function to Backup Gateway WITHOUT Forms Auth
fu...# Function to Backup Gateway WITHOUT Forms Auth<br />function BackupBasic ($un, $pw, $ad, $path)<br />{<br /> $regex = "BOARD.*\.ini"<br /> $webclient = $null<br /> $configurl = $null<br /> $backupurl = $null<br /> $webpage = $null<br /> $pattern = $null<br /> $backupfilename = $null<br /><br /> $webclient = new-object System.Net.WebClient<br /> $webclient.Credentials = New-Object System.Net.NetworkCredential($un, $pw)<br /><br /> Write-Host `r`n 'Detecting .ini name' `r`n<br /> $configurl = "https://" + $ad + "/ConfigurationFile"<br /> $webpage = $webclient.DownloadString($configurl)<br /> $pattern = $webpage -split "`n" | Select-String -pattern $regex -Allmatches | % { $_.Matches | % { $_.Value } }<br /> Write-Host `r`n 'Detected Board Filename ' $pattern<br /><br /> Write-Host `r`n 'Backing up ' $ad `r`n<br /> $backupurl = "https://" + $ad + "/FS/" + $pattern<br /> $backupfilename = $path + $pattern<br /> $webclient.DownloadFile($backupurl,$backupfilename)<br /> Write-Host `r<br />}<br /><br /> <br /># Function to Detect the Backup Filename with Forms Auth for the Audiocodes Configuration File<br />function DetectFilenameForms ($ad)<br />{<br /> $fnregex = "BOARD.*\.ini"<br /> $configurl = $null<br /> $fnpat = $null<br /> $configwebpage = $null<br /><br /> Write-Host `r`n 'Detecting .ini name' `r`n<br /> $configurl = "https://" + $ad + "/ConfigurationFile"<br /> $configwebpage = curl $configurl -b cookiejar.txt<br /> $fnpat = $configwebpage -split "`n" | Select-String -pattern $fnregex -Allmatches | % { $_.Matches | % { $_.Value } }<br /><br /> Write-Host `r`n 'Detected Board Filename ' $fnpat<br /> return $fnpat<br />}<br /><br /> <br /> # Function to Backup Gateway with Forms Auth<br />function BackupGatewayForms ($ad, $path, $fn)<br />{<br /> $backupurl = $null<br /> $backupfilename = $null<br /> <br /> Write-Host `r`n 'Backing up ' $ad `r`n<br /> $backupurl = "https://" + $ad + "/FS/" + $fn<br /> $backupfilename = $path + $fn<br /> curl -o $backupfilename $backupurl -b cookiejar.txt<br /> Write-Host `r<br />}Anonymoushttps://www.blogger.com/profile/14456490538290033748noreply@blogger.comtag:blogger.com,1999:blog-4895679265618109180.post-12568905604448958412017-08-01T14:39:09.121-06:002017-08-01T14:39:09.121-06:00# Function to Authenticate with Forms Auth
functio...# Function to Authenticate with Forms Auth<br />function AuthenticateForms ($un, $pw, $ad)<br />{<br /> $login1sregex = "\(\d*)\<\/s\>"<br /> $login1rregex = "\(.*)\<\/r\>"<br /> $a1 = $null<br /> $loginusername = $null<br /> $loginurl = $null<br /> $login1webpage = $null<br /> $login1spattern = $null<br /> $login1rpattern = $null<br /> $hashByteArray1 = $null<br /> $hashByteArray2 = $null<br /> $passwordhash = $null<br /> $resultwebpage = $null<br /> $formsbasedauth = $null<br /> <br /> Write-Host `r`n 'Forms Authentication' `r`n<br /> $login1username = "c1=" + $un<br /> $loginurl = "https://" + $ad + "/UE/Login"<br /> $login1webpage = curl $loginurl --data-urlencode "t=1" `<br /> --data-urlencode "c0=0" `<br /> --data-urlencode $login1username `<br /> --header "X-Requested-With: XMLHttpRequest" `<br /> -c cookiejar.txt `<br /> --header "Cookie: aclogname=; C2=ct" `<br /> --user-agent "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko"<br /><br /> $login1spattern = $login1webpage -split "`n" | Select-String -pattern $login1sregex -Allmatches | % { $_.Matches | % { $_.groups[1].Value } }<br /><br /> $login1spattern = "s=" + $login1spattern<br /> $login1rpattern = $login1webpage -split "`n" | Select-String -pattern $login1rregex -Allmatches | % { $_.Matches | % { $_.groups[1].Value } }<br /><br /> $cryptoServiceProvider = [System.Security.Cryptography.SHA256CryptoServiceProvider]<br /> $hashAlgorithm = New-Object $cryptoServiceProvider<br /> $hashByteArray1 = $hashAlgorithm.ComputeHash($([Char[]]$password))<br /> foreach ($byte in $hashByteArray1)<br /> {<br /> $a1 += "{0:x2}" -f $byte<br /> }<br /> <br /> $a2 = $username + ":" + $login1rpattern + ":" + $a1<br /> $hashByteArray2 = $hashAlgorithm.ComputeHash($([Char[]]$a2))<br /><br /> foreach ($byte in $hashByteArray2)<br /> {<br /> $passwordhash += "{0:x2}" -f $byte<br /> }<br /> <br /> $passwordhash = "c1=" + $passwordhash<br /> $resultwebpage = curl $loginurl --data-urlencode "t=1" `<br /> --data-urlencode $login1spattern `<br /> --data-urlencode "c0=1" `<br /> --data-urlencode $passwordhash `<br /> --header "X-Requested-With: XMLHttpRequest" `<br /> -c cookiejar.txt `<br /> --header "Cookie: aclogname=; C2=ct" `<br /> --user-agent "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko"<br />}Anonymoushttps://www.blogger.com/profile/14456490538290033748noreply@blogger.comtag:blogger.com,1999:blog-4895679265618109180.post-41469901676123571982017-08-01T14:38:46.778-06:002017-08-01T14:38:46.778-06:00Sure, here it goes.
URL, user and pass have been r...Sure, here it goes.<br />URL, user and pass have been replaced.<br /><br />------------------------------------------<br /><br />Remove-Item alias:curl <br /><br /># User Modifiable Variables<br />$username = "test"<br />$password = "test"<br />$address = "xxxx.net"<br />$backuppath = "d:\scripts.txt"<br /><br /> <br /># Variable Initialization<br />$filename = $null<br />$statuscodeeval = $null<br /><br /> <br />#Function to detect if Forms Auth is present<br />function DetectAuthType ($ad)<br />{<br /> $loginurl = $null<br /> <br /> $loginurl = "https://" + $ad + "/api"<br /> $statuscode = curl -o null.txt $loginurl -w '%{http_code}'<br /> Write-Host "STATUS CODE 1" $statuscode<br /><br /> If ($statuscode -eq "404")<br /> {<br /> $loginurl = "https://" + $ad + "/"<br /> $statuscode = curl -o null.txt $loginurl -w '%{http_code}'<br /> Write-Host "STATUS CODE 2" $statuscode<br /> <br /> If ($statuscode -eq "401")<br /> {<br /> $statuscodeeval = "basic"<br /> Write-Host $statuscodeeval<br /> }<br /> ElseIf ($statuscode -eq "203")<br /> {<br /> $statuscodeeval = "forms"<br /> Write-Host $statuscodeeval<br /> }<br /> Else<br /> {<br /> $statuscodeeval = "neither"<br /> Write-Host $statuscodeeval<br /> }<br /> }<br /> ElseIf ($statuscode -eq "401")<br /> {<br /> $statuscodeeval = "api"<br /> }<br /> Return $statuscodeeval<br /> Remove-Item null.txt<br />}<br />Anonymoushttps://www.blogger.com/profile/14456490538290033748noreply@blogger.comtag:blogger.com,1999:blog-4895679265618109180.post-24033072412341238662017-07-31T09:45:58.900-06:002017-07-31T09:45:58.900-06:00I think I will need to see your script... is it po...I think I will need to see your script... is it possible to post here?Jonathan McKinneyhttps://www.blogger.com/profile/05276135243139498379noreply@blogger.comtag:blogger.com,1999:blog-4895679265618109180.post-43432346662547566912017-07-31T09:44:10.349-06:002017-07-31T09:44:10.349-06:00Sorry I didn´t explain better, I did change http t...Sorry I didn´t explain better, I did change http to https on all instances in the script. I noticed this because I kept getting login errors but after I made the change the script seemed to be working fine.Anonymoushttps://www.blogger.com/profile/14456490538290033748noreply@blogger.comtag:blogger.com,1999:blog-4895679265618109180.post-53697941237451883312017-07-27T09:45:17.875-06:002017-07-27T09:45:17.875-06:00if youu require https, you would need to change al...if youu require https, you would need to change all instances of http to https in the script. the $loginurl only applies to just that one function.Jonathan McKinneyhttps://www.blogger.com/profile/05276135243139498379noreply@blogger.comtag:blogger.com,1999:blog-4895679265618109180.post-71219663980862814382017-07-27T08:34:40.674-06:002017-07-27T08:34:40.674-06:00The script is exactly the same as your except that...The script is exactly the same as your except that I changed http to https in $loginurl.<br />When the script finishes I get the following output:<br /><br />PS C:\Users\60053465> # Main Script<br />PS C:\Users\60053465><br />PS C:\Users\60053465> $formsbasedauth = DetectAuthType $address<br /> % Total % Received % Xferd Average Speed Time Time Time Current<br /> Dload Upload Total Spent Left Speed<br />100 150 0 150 0 0 566 0 --:--:-- --:--:-- --:--:-- 566<br /> % Total % Received % Xferd Average Speed Time Time Time Current<br /> Dload Upload Total Spent Left Speed<br />100 13799 0 13799 0 0 42070 0 --:--:-- --:--:-- --:--:-- 42070<br /><br /> Status Code 203<br /><br /><br />PS C:\Users\60053465> if ($formsbasedauth -eq "forms")<br />>> {<br />>> AuthenticateForms $username $password $address<br />>> $filename = DetectFilenameForms $address<br />>> BackupGatewayForms $address $backuppath $filename<br />>> LogoffForms $address<br />>> }<br />>> ElseIf ($formsbasedauth -eq "basic")<br />>> {<br />>> BackupBasic $username $password $address $backuppath<br />>> }<br />>> ElseIf ($formsbasedauth -eq "api")<br />>> {<br />>> BackupGatewayAPI $username $password $address $backuppath<br />>> }<br />>> Else<br />>> {<br />>> Write-Host "Something went really wrong... couldn't detect Authentication type... Might I suggest Wireshark"<br />>> }<br />>><br /><br /> Forms Authentication<br /><br /> % Total % Received % Xferd Average Speed Time Time Time Current<br /> Dload Upload Total Spent Left Speed<br />100 143 0 122 100 21 326 56 --:--:-- --:--:-- --:--:-- 326<br /> % Total % Received % Xferd Average Speed Time Time Time Current<br /> Dload Upload Total Spent Left Speed<br />100 192 0 112 100 80 265 189 --:--:-- --:--:-- --:--:-- 265<br /><br /> Detecting .ini name<br /><br /> % Total % Received % Xferd Average Speed Time Time Time Current<br /> Dload Upload Total Spent Left Speed<br />100 13799 0 13799 0 0 44227 0 --:--:-- --:--:-- --:--:-- 44227<br /><br /> Detected Board Filename<br /><br /> Backing up xxxx.xxxx.xxxx.net<br /><br /> % Total % Received % Xferd Average Speed Time Time Time Current<br /> Dload Upload Total Spent Left Speed<br /> 0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0<br /><br /> % Total % Received % Xferd Average Speed Time Time Time Current<br /> Dload Upload Total Spent Left Speed<br />100 13799 0 13799 0 0 42070 0 --:--:-- --:--:-- --:--:-- 42070<br />PS C:\Users\60053465><br /><br /><br />Anonymoushttps://www.blogger.com/profile/14456490538290033748noreply@blogger.comtag:blogger.com,1999:blog-4895679265618109180.post-66789861060328419252017-07-25T13:51:01.166-06:002017-07-25T13:51:01.166-06:00Can you post the transcript file here?Can you post the transcript file here?Jonathan McKinneyhttps://www.blogger.com/profile/05276135243139498379noreply@blogger.comtag:blogger.com,1999:blog-4895679265618109180.post-17764169347159568842017-07-25T13:49:11.577-06:002017-07-25T13:49:11.577-06:00Version 6.80AVersion 6.80AAnonymoushttps://www.blogger.com/profile/14456490538290033748noreply@blogger.comtag:blogger.com,1999:blog-4895679265618109180.post-83031140425669487392017-07-20T14:06:06.248-06:002017-07-20T14:06:06.248-06:00which version of GW do you have?which version of GW do you have?Jonathan McKinneyhttps://www.blogger.com/profile/05276135243139498379noreply@blogger.comtag:blogger.com,1999:blog-4895679265618109180.post-75846370181218013542017-07-20T13:32:03.009-06:002017-07-20T13:32:03.009-06:00Hi Jonathan, I have tried to reply twice but for s...Hi Jonathan, I have tried to reply twice but for some reason my responses are not being published.<br />I did remove the curl alias however the errors stopped once I started using standard powershell instead of ISE.<br />Now I do not get any errors but unfortunately I am getting a blank file.<br /><br />Is there anything I should enable on the GW side?Anonymoushttps://www.blogger.com/profile/14456490538290033748noreply@blogger.comtag:blogger.com,1999:blog-4895679265618109180.post-43272876414414114392017-07-07T12:58:59.075-06:002017-07-07T12:58:59.075-06:00are you running within ISE? You might try running ...are you running within ISE? You might try running from standard powershell if that is the caseJonathan McKinneyhttps://www.blogger.com/profile/05276135243139498379noreply@blogger.comtag:blogger.com,1999:blog-4895679265618109180.post-71136406161217817372017-07-07T12:54:12.337-06:002017-07-07T12:54:12.337-06:00did you remove the curl alias that powershell has ...did you remove the curl alias that powershell has by default?Jonathan McKinneyhttps://www.blogger.com/profile/05276135243139498379noreply@blogger.comtag:blogger.com,1999:blog-4895679265618109180.post-67456356040714813572017-07-07T12:52:16.694-06:002017-07-07T12:52:16.694-06:00Hello Jonathan, my name is Veronica.
Thank you ver...Hello Jonathan, my name is Veronica.<br />Thank you very much for posting this!<br />I just tried it out but I am getting an empty file, do you have any idea of what I might be missing?<br /><br />I have never used cURL before and I keep getting a native command error warning for each cURL line in the script.<br /><br />curl : % Total % Received % Xferd Average Speed Time Time Time Current<br />At line:199 char:23<br />+ $logoffwebpage = curl $logoffurl -b cookiejar.txt<br />+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~<br /> + CategoryInfo : NotSpecified: ( % Total % ... Time Current:String) [], RemoteException<br /> + FullyQualifiedErrorId : NativeCommandError<br /> <br /> Dload Upload Total Spent Left Speed<br /> 0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0<br /> 0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0<br />100 4096 0 4096 0 0 2303 0 --:--:-- 0:00:01 --:--:-- 2303<br />100 13799 0 13799 0 0 6858 0 --:--:-- 0:00:02 --:--:-- 6858Anonymousnoreply@blogger.com