1

I want to take this part

REGISTRATION_ELI_20221222_071008.csv

from this PowerShell output of Posh-SSH Get-SFTPChildItem

Name REGISTRATION_ELI_20221222_071008.csv, Length 0, User ID 1142, Group ID 1220, Accessed 12/21/2022 3:00:47 PM, Modified 12/21/2022 3:00:47 PM

How can I get it?

#Date
$time = (Get-Date).ToString("yyyyMMdd") 

#Setting credentials for the user account
$password = ConvertTo-SecureString "password" -AsPlainText -Force
$creds = New-Object System.Management.Automation.PSCredential ("gomgom", $password)

$SFTPSession = New-SFTPSession -ComputerName 172.16.xxx.xxx -Credential $Credential -AcceptKey

# Set local file path and SFTP path
$LocalPath = "D:\WORK\Task - Script\20221010 - AJK - ITPRODIS380 - upload file csv ke sql server\csvfile"

$FilePath = Get-SFTPChildItem -SessionId $SFTPSession.SessionId "/Home Credit/Upload/" | Select-String -Pattern "REGISTRATION_ELI_$([datetime]::Now.toString('yyyyMMdd'))" 
$FilePath
Remove-SFTPSession $SFTPSession -Verbose
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
TITANIXX
  • 35
  • 6

3 Answers3

1

Do not parse the name from the string dump of the file object.

The Posh-SSH Get-SFTPChildItem returns SSH.NET SftpFile, which has Name attribute:

$FilePath.Name

(though then the $FilePath is actually a confusing variable name, as it's an object, not a mere path string)

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
-1

You can dig into the Regex library and extract it out using Regex

$test = "Name REGISTRATION_ELI_20221222_071008.csv, Length 0, User ID 1142, Group ID 1220, Accessed 12/21/2022 3:00:47 PM, Modified 12/21/2022 3:00:47 PM"
$matches = [regex]::Match($test,"REGISTRATION.+\.csv")
$Matches.Value
Shadowzee
  • 537
  • 3
  • 15
  • it didn't work, no results came up – TITANIXX Dec 22 '22 at 04:43
  • @Titanixx When I run those 3 lines of code, I get the desired output. Are you sure the get-sftpchilditem command is actually returning the output? – Shadowzee Dec 22 '22 at 04:49
  • Yes, I am very sure and have tried it – TITANIXX Dec 22 '22 at 05:42
  • @Titanixx Can you run the following commend and let me know the output? `$FilePath = get-sftpchilditem -SessionId $SFTPSession.SessionId "/Home Credit/Upload/"; write-output "Debug"; write-output $FilePath;` If there is nothing after "Debug", then your command is writing it directly to the console and everything becomes trickier. Otherwise, the object type might not be a string and require some extra processing – Shadowzee Dec 22 '22 at 05:46
  • This is the output FullName : /Home Credit/Upload/REGISTRATION_ELI_20191006_071047.csv LastAccessTime : 12/21/2022 11:00:28 AM LastWriteTime : 10/30/2019 11:30:38 AM Length : 2464584 UserId : 1021 FullName : /Home Credit/Upload/REGISTRATION_ELI_20210419_071056.csv LastAccessTime : 12/21/2022 11:00:29 AM LastWriteTime : 4/19/2021 12:37:14 PM Length : 1828502 UserId : 1021 – TITANIXX Dec 22 '22 at 06:19
  • and there is some output with different file names again @Shadowzee – TITANIXX Dec 22 '22 at 06:20
  • and i just need **/Home Credit/Upload/REGISTRATION_ELI_20191006_071047.csv** – TITANIXX Dec 22 '22 at 06:21
-1

$FilePath | Select-String -Pattern 'Name\s(.*?),' -AllMatches| Foreach-Object {$.Matches} | Foreach-Object {$.Groups[1].Value}

Iggy Zofrin
  • 515
  • 3
  • 10