Thursday 17 May 2018

WinScp - Directory Syncing between linux and windows

Import-Module WinScp
$sessionOptions = New-Object WinSCP.SessionOptions
$sessionOptions.Protocol = [WinSCP.Protocol]::Scp
$sessionOptions.HostName = "nash.duckdns.org"
$sessionOptions.UserName = "xyz"
$sessionOptions.Password = "abc"

$sessionOptions.GiveUpSecurityAndAcceptAnySshHostKey = $true
$sessionOptions.SshPrivateKeyPath = "C:\Users\NashAIO\Downloads\Nisa.ppk"

$session = New-Object WinSCP.Session

$session.Open($sessionOptions)

$SourcePath = "C:\Source\"
$DestinationPath = "/home/ubuntu/html/"

$session.SynchronizeDirectories( [WinSCP.SynchronizationMode]::Remote, "$SourcePath", "$DestinationPath", $False)

$session.Dispose()

Some Powershell Stuff

Script1

$cred = Get-Credential

$Server = "192.168.109.135"

Invoke-Command -ComputerName $server -ScriptBlock {

Set-ExecutionPolicy Unrestricted -Force
Sleep 5

Install-PackageProvider -Name NuGet -Force

Enable-WindowsOptionalFeature -Online -FeatureName "IIS-WebServer" -All
Enable-WindowsOptionalFeature -Online -FeatureName "IIS-WebServerManagementTools" -All
Enable-WindowsOptionalFeature -Online -FeatureName "IIS-WebServerRole" -All
#Install-WindowsFeature -Name Web-Server -IncludeAllSubFeature -IncludeManagementTools

Import-Module ServerManager -Force
Import-Module WebAdministration -Force

Stop-Website "Default Web Site"

Md $Env:systemdrive\Website1
Md $Env:systemdrive\MyApp
Md $Env:systemdrive\Backup

New-Website -Name "Website1" -Port 80 -IPAddress "*" -HostHeader "" -PhysicalPath "C:\Website1"

New-WebVirtualDirectory -Site "Website1" -Name "MyApp" -PhysicalPath "$Env:systemdrive\MyApp"
ConvertTo-WebApplication -PSPath "IIS:\Sites\Website1\MyApp"

Restart-WebAppPool DefaultAppPool

} -Credential $cred

New-PSDrive -Name J -PSProvider FileSystem -Root \\$Server\C$ -Credential $cred

Copy-Item -Path "J:\Website1" -Destination "C:\ServerCopy" -Recurse -Force
Copy-Item -Path "J:\MyApp" -Destination "C:\ServerCopy" -Recurse -Force

Get-PSDrive J | Remove-PSDrive

--------------------------------------------------------------------------------------------------------------------------

Script2

$cred = Get-Credential

$server = "192.168.109.135"

New-PSDrive -Name J -PSProvider FileSystem -Root \\$server\C$ -Credential $cred

Copy-Item -Path "J:\Website1" -Destination "J:\Backup" -Recurse -Force
Copy-Item -Path "J:\MyApp" -Destination "J:\Backup" -Recurse -Force

do {

$Source=  Read-Host "Input The Full file Path "
$Destination = Read-Host "Input The Full file Path "

get-childitem $Source -Recurse | foreach {
    $SrcFile = $_.FullName
    $SrcHash = Get-FileHash -Path $SrcFile -Algorithm MD5

    $DestFile = $_.Fullname -replace [RegEx]::escape($Source),$Destination

    if (Test-Path $DestFile) {
        $DestHash = Get-FileHash -Path $DestFile -Algorithm MD5
        if ($SrcHash.hash -ne $DestHash.hash) {
            Copy-Item $SrcFile -Destination $DestFile -Force
        }
    }
else {
        Copy-Item $SrcFile -Destination $DestFile -Force
    }
}
$response = read-host "Repeat?"
}
while ($response -eq "Y")

Get-PSDrive J | Remove-PSDrive

Invoke-Command -ComputerName $server -ScriptBlock {
Restart-WebAppPool DefaultAppPool
} -Credential $cred

--------------------------------------------------------------------------------------------------------------------------