繁体   English   中英

如何从 powershell 输出中获取特定单词?

[英]How to get specific word from powershell output?

我想参与这个角色

REGISTRATION_ELI_20221222_071008.csv

从此 PowerShell 输出

名称 REGISTRATION_ELI_20221222_071008.csv,长度 0,用户 ID 1142,组 ID 1220,访问时间 12/21/2022 下午 3:00:47,修改时间 12/21/2022 下午 3:00:47

我怎么才能得到它?

 #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

您可以深入研究 Regex 库并使用 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

$文件路径 | 选择字符串 -Pattern 'Name\s(.*?),' -AllMatches| Foreach 对象 {$ .Matches} | Foreach-Object {$ .Groups[1].Value}

不要从文件对象的字符串转储中解析名称。

Posh-SSH Get-SFTPChildItem返回 SSH.NET SftpFile ,它具有Name属性:

$FilePath.Name

(尽管$FilePath实际上是一个令人困惑的变量名,因为它是一个对象,而不仅仅是路径字符串)

完成,我使用 $FilePath 的子字符串

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM