Differences
This shows you the differences between the selected revisions of the page.
| 2015-12-22 | 2015-12-22 | ||
| Action on match label (martin) | 5.8 Bug 1356 Method to list all files in remote directory tree recursively (martin) | ||
| Line 12: | Line 12: | ||
| You can alter the script for other tasks, instead of grepping the matching files. You can for example [[library_session_removefiles|remove]] or [[library_session_getfiles|download]] the matching files. Just modify the action in the ''Action on match'' block accordingly. | You can alter the script for other tasks, instead of grepping the matching files. You can for example [[library_session_removefiles|remove]] or [[library_session_getfiles|download]] the matching files. Just modify the action in the ''Action on match'' block accordingly. | ||
| + | |||
| + | ===== In the Beta Version ===== | ||
| + | In the latest beta version, you can simplify the implementation by using ''[[library_session_enumerateremotefiles|Session.EnumerateRemoteFiles]]''. &beta | ||
| + | |||
| + | <code powershell> | ||
| + | param ( | ||
| + | [Parameter(Mandatory)] | ||
| + | $path, | ||
| + | [Parameter(Mandatory)] | ||
| + | $text, | ||
| + | $wildcard = "*.*" | ||
| + | ) | ||
| + | |||
| + | try | ||
| + | { | ||
| + | # Load WinSCP .NET assembly | ||
| + | Add-Type -Path "WinSCPnet.dll" | ||
| + | |||
| + | # Setup session options | ||
| + | $sessionOptions = New-Object WinSCP.SessionOptions | ||
| + | $sessionOptions.Protocol = [WinSCP.Protocol]::Sftp | ||
| + | $sessionOptions.HostName = "example.com" | ||
| + | $sessionOptions.UserName = "user" | ||
| + | $sessionOptions.Password = "mypassword" | ||
| + | $sessionOptions.SshHostKeyFingerprint = "ssh-rsa 2048 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx" | ||
| + | |||
| + | $session = New-Object WinSCP.Session | ||
| + | |||
| + | try | ||
| + | { | ||
| + | # Connect | ||
| + | $session.Open($sessionOptions) | ||
| + | |||
| + | # Recursivelly enumerate files to grep | ||
| + | $fileInfos = | ||
| + | $session.EnumerateRemoteFiles( | ||
| + | $path, $wildcard, [WinSCP.EnumerationOptions]::AllDirectories) | ||
| + | |||
| + | foreach ($fileInfo in $fileInfos) | ||
| + | { | ||
| + | # Action on match | ||
| + | |||
| + | # Modify the code below if you want to do another task with | ||
| + | # matching files, instead of grepping their contents | ||
| + | |||
| + | Write-Host ("File {0} matches mask, searching contents..." -f $fileInfo.FullName) | ||
| + | $tempPath = ($env:temp + "\" + $fileInfo.Name) | ||
| + | # Download file to temporary directory | ||
| + | $transferResult = $session.GetFiles($session.EscapeFileMask($fileInfo.FullName), $tempPath) | ||
| + | # Did the download succeeded? | ||
| + | if (!$transferResult.IsSuccess) | ||
| + | { | ||
| + | # Print error (but continue with other files) | ||
| + | Write-Host $transferResult.Failures[0].Message | ||
| + | } | ||
| + | else | ||
| + | { | ||
| + | # Search and print lines containing "text". | ||
| + | # Use -Pattern instead of -SimpleMatch for regex search | ||
| + | $matchInfo = Select-String -Path $tempPath -SimpleMatch $text | ||
| + | # Print the results | ||
| + | foreach ($match in $matchInfo) | ||
| + | { | ||
| + | Write-Host ($fileInfo.FullName + ":" + $match.LineNumber + ":" + $match.Line) | ||
| + | } | ||
| + | # Delete temporary local copy | ||
| + | Remove-Item $tempPath | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | finally | ||
| + | { | ||
| + | # Disconnect, clean up | ||
| + | $session.Dispose() | ||
| + | } | ||
| + | |||
| + | exit 0 | ||
| + | } | ||
| + | catch [Exception] | ||
| + | { | ||
| + | Write-Host $_.Exception.Message | ||
| + | exit 1 | ||
| + | } | ||
| + | </code> | ||
| + | |||
| + | ===== In the Stable Version ===== | ||
| <code powershell> | <code powershell> | ||