Pages

Wednesday, December 9, 2015

Powershell, Infile search and file copy between the servers in same domain

Find a keyword within multiple files in a folder.

Cls
$PATH = "D:\DSUK\Daiitchi Sankyo Activity Instance\Warehouse\Data\Staging\Veeva Activity\WE_00DA0000000Ci0mMAC_123\"
$FILES = Get-ChildItem -Path $path  # -Name user.csv*

Foreach ($FILE IN $FILES )
{
Get-Content $PATH$FILE -First 1   | Where-Object { $_ -like '*lm_Presentation_Version_vod*' }

$FILE
}


File copy between the servers in the same domain

Cls
$SourcePath = "\\Server1\PharmaAnalytics\Takeda Instance\Data Input Area"
$DestinationPath  = "\\Server2\PharmaAnalytics\Takeda Instance\Data Input Area"


$Folders = Get-ChildItem -Path $SourcePath  -Name Aver*
FOREACH ($Folder in $Folders)
{
#$DestinationPath + "\"+ $Folder
#Get-ChildItem -Path $SourcePath\$Folder | Where-Object{!($_.PSIsContainer)}
Copy -Path $SourcePath\$Folder\* -Destination $DestinationPath\$Folder | Where-Object{!($_.PSIsContainer)} # Exclude Folders
}



Monday, October 19, 2015

Identify and disconnect / log off remote RDP sessions


Run QWINSTA to extract the RDP session information
  • QWINSTA /SERVER:servername

If the session exists, read the username and session ID.
  • To disconnect user 
    TSDISCON /SERVER:servername sessionID
  • To log-off user / kill the session
    RWINSTA /SERVER:servername sessionID


Ref : http://discoposse.com/2012/10/20/finding-rdp-sessions-on-servers-using-powershell/

Saturday, February 21, 2015

Function to Get Folder Sizes in Powershell

Following Powershell function can be used to get the sizes of given folders.
Folder names hould be given within double quotes seperated by commas.
 
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
 
 function Get-FolderSize {
    param(
        $FolderPaths = $null
        )
    [array]$FolderLists = $null
    foreach ( $FolderPath in $FolderPaths)
    {
        $FolderLists += 
        Get-ChildItem $FolderPath -Force -Recurse | 
        Measure-Object -property Length -Sum |
        select @{n="Path";e={"""" +$FolderPath + """"}} ,
            @{n="Size GB";e={[math]::ROUND($_.Sum / 1GB -as [Float],2)}} , 
            @{n="Size MB";e={[math]::ROUND($_.Sum / 1MB -as [Float],2)}} , 
            @{n="Size KB";e={[math]::ROUND($_.Sum / 1KB -as [Float],2)}}  
    }
    $FolderLists | Format-Table -AutoSize 
 }

The function rutuns the sizes of given folders in kilobyte (KB), megabyte (MB), gigabyte(GB).

Saturday, January 31, 2015

Connect to a SFTP site using Windows Powershell

This post explains how to access  a SFTP server using Windows Powershell commands. There are many file transfer utilities which can be used for this purpose. Here, I use WinSCP  in order to access SFTP server from windows powershell.

1. WinSCP PowerShell Wrapper can be downloaded from https://github.com/dotps1/WinSCP.
2. Import WinSCP Powershell module. $WinSCPFile path variable needs to be set to the WinSCP.psd1 file inside the downloaded folder.

$WinSCPFile = "C:\....\WinSCP-master\WinSCP.psd1"
Import-Module -Name $WinSCPFile -Verbose -ErrorAction Inquire -WarningAction Inquire | Out-null
3. Splat New-WinSCPSessionOptions.
$sessionOptions = @{
HostName = "SFTP-HostName"
UserName = "Username"
Password = "Password"
SshHostKeyFingerprint = "HostKey"
}


To get the SshHostKeyFingerprint, go to \WinSCP-master\NeededAssemblies\ folder and run WinSCP.exe.Then enter HostName, Username, & Password and try connecting to the SFTP site. You will get the SshHostKeyFingerprint when you log in for the first time.

4. Open new WinSCPSession using the splatted parameters.
$session = Open-WinSCPSession -SessionOptions (New-WinSCPSessionOptions @sessionOptions)
 

5. Send a file to SFTP server
Send-WinSCPItem -WinSCPSession $session -LocalPath "C:\localFile.txt" -RemotePath "./remoteDirectory/"
 

6. Get a file from FTP server
Receive-WinSCPItem -WinSCPSession $session -RemotePath "./remoteDirectory/rFile.txt" -LocalPath "C:\localFile.txt"
7. Close SFTP session
Close-WinSCPSession -WinSCPSession $session

Referance: 

https://github.com/dotps1/WinSCP
http://winscp.net/eng/docs/ssh_verifying_the_host_key