Tuesday, 29 May 2018

Downloading File /Files to a remote machine using Powershell

During a desktop application performance testing scenario, I came up with a challenge to move three dll files to 300+ AWS workspaces. I calculated even if I took 5 mins per one workspace, it would cost be 1500 Mins that is 25Hrs. So atleast I need three people do this task in a day or I have to do this in three days or less. So thought of using Powershell to do this job. I wrote my script with the help of google and unable to run it due to some exception.

Here is the script:
param([switch]$Elevated)

function Test-Admin {
  $currentUser = New-Object Security.Principal.WindowsPrincipal $([Security.Principal.WindowsIdentity]::GetCurrent())
  $currentUser.IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)
}

if ((Test-Admin) -eq $false)  {
    if ($elevated)
    {
        # tried to elevate, did not work, aborting
    }
    else {
        Start-Process powershell.exe -Verb RunAs -ArgumentList ('-noprofile -noexit -file "{0}" -elevated' -f ($myinvocation.MyCommand.Definition))
}

exit
}
$Files =Import-Csv D:\Test.csv
import-Module bitstransfer
ForEach($File in $Files)
 {
        $ip=$File.ip
        Write-Output $ip
        $Uname =$File.Uname
        Write-Output $Uname
        $Pwd = $File.Pwd
        $Source ="D:\DLL Files\*.dll"
        $Dest = "\\$($ip)\c$\Program Files (x86)\NetSuite\NetSuite Point of Sale\"
        Write-Output $Dest
        $secpasswd = ConvertTo-SecureString "shoN$p0s" -AsPlainText -Force
        $cred = New-Object System.Management.Automation.PSCredential ($Uname, $secpasswd)
        Write-Output $cred
        $Session = New-PSSession -ComputerName $ip -Credential $cred
        $File = [System.IO.File]::ReadAllBytes("D:\DLL_Files\oAuthRestletCall.dll");
        $File1 = [System.IO.File]::ReadAllBytes("D:\DLL_Files\HtmlAgilityPack.dll");
        $File2 = [System.IO.File]::ReadAllBytes("D:\DLL_Files\MPK_Calendar.dll");
        Invoke-Command -Session $session -ArgumentList $file -ScriptBlock{[System.IO.File]::WriteAllBytes("C:\Program Files (x86)\NetSuite\NetSuite Point of Sale\oAuthRestletCall.dll", $args)};
        Invoke-Command -Session $session -ArgumentList $file1 -ScriptBlock{[System.IO.File]::WriteAllBytes("C:\Program Files (x86)\NetSuite\NetSuite Point of Sale\HtmlAgilityPack.dll", $args)};
        Invoke-Command -Session $session -ArgumentList $file2 -ScriptBlock{[System.IO.File]::WriteAllBytes("C:\Program Files (x86)\NetSuite\NetSuite Point of Sale\MPK_Calendar.dll", $args)};
        #Copy-Item $Source $Dest -Recurse -Force
        #Start-BitsTransfer -Source $Source -Destination $Dest -Credential $cred
        Write_Output "done"
        Remove-PSSession $Session
        }     

Then I took the approach of doing this task with SIKULI + Powershell IDE and succeeded. I spent an hour by StackOverflow and other sites and here is the code :

PS C:\Windows\system32\WindowsPowerShell\v1.0>
[10.9.8.238]: PS D:\Users\PerfReg1172\Documents> $url = "http://10.9.11.192/Logs/NTWSLogs/HtmlAgilityPack.dll";
$output = "c:\Program Files (x86)\NetSuite\NetSuite Point of Sale\HtmlAgilityPack.dll";
Invoke-WebRequest -Uri $url -OutFile $output;
$url = "http://10.9.11.192/Logs/NTWSLogs/MPK_Calendar.dll";
$output = "c:\Program Files (x86)\NetSuite\NetSuite Point of Sale\MPK_Calendar.dll";
Invoke-WebRequest -Uri $url -OutFile $output;
$url = "http://10.9.11.192/Logs/NTWSLogs/oAuthRestletCall.dll";
$output = "c:\Program Files (x86)\NetSuite\NetSuite Point of Sale\oAuthRestletCall.dll";
Invoke-WebRequest -Uri $url -OutFile $output;

I already have an IIS running and copied the files to a directory.I declared this variables to store the file contents in  a variable and writing them as a file in the remote machine.SIKULI will open a new remote Power Shell session in Powershell IDE, then it will paste the above lines and execute them. I made a loop with all 300 machine ip's and passwords to achieve this.

Thank you SIKULI + POWERSHELL.

I did it by 9 Hrs where sikuli ran unattended.  I wasted nerely 5 hours in the task1 .

Attaching a SQL Server Database to DB server through T-SQL

While working to develop a SIKULI script to run on multiple machines, i came to a situation  to attach a database in every machine. As sikuli is not much flexible, i choose to attach the db via T-SQL .

I opened the SQL server  management studio in the remote machine and logged into that with SIKULI script. so finally find the below code to be run on a new query window.

CREATE DATABASE RAPOS_Replication  
    ON (FILENAME = 'C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\RAPOS_Replication.mdf'),  
    (FILENAME = 'C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\RAPOS_Replication_Log.ldf')  
    FOR ATTACH;

With sikuli, its some thing like below:

        type('CREATE DATABASE RAPOS_Replication')
        type(' ON (FILENAME = \'C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\RAPOS_Replication.mdf\'),')
        type(' (FILENAME = \'C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\RAPOS_Replication_Log.ldf\')')
        type('FOR ATTACH;'+Key.ENTER+Key.F5)
        wait(3)

Shrinking the size of Oracle Virtual Box

First, zero fill your virtual disk. Boot the VM and run: sudo dd if=/dev/zero of=/bigemptyfile bs=4096k status=progress sudo rm -f /bigempty...