Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell 删除远程SFTP目录中具有模式的文件_Powershell_Sftp_Remote Server_Filepattern - Fatal编程技术网

Powershell 删除远程SFTP目录中具有模式的文件

Powershell 删除远程SFTP目录中具有模式的文件,powershell,sftp,remote-server,filepattern,Powershell,Sftp,Remote Server,Filepattern,我需要使用PS从SFTP服务器上的远程目录中删除与某些模式(名称包含特定字符串)匹配的文件。PowerShell中不支持SFTP。您必须使用第三方SFTP库 例如,使用,可以执行以下操作: Add-Type -Path "WinSCPnet.dll" $sessionOptions = New-Object WinSCP.SessionOptions -Property @{ Protocol = [WinSCP.Protocol]::Sftp HostName = "exam

我需要使用PS从SFTP服务器上的远程目录中删除与某些模式(名称包含特定字符串)匹配的文件。

PowerShell中不支持SFTP。您必须使用第三方SFTP库

例如,使用,可以执行以下操作:

Add-Type -Path "WinSCPnet.dll"

$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
    Protocol = [WinSCP.Protocol]::Sftp
    HostName = "example.com"
    UserName = "username"
    Password = "password"
    SshHostKeyFingerprint = "ssh-rsa 2048 ..."
}

$session = New-Object WinSCP.Session

$session.Open($sessionOptions)

$session.RemoveFiles("/remote/path/*string*").Check()

$session.Dispose()
给你


(我是WinSCP的作者)

您也可以尝试ssh.net库(它非常轻量级)
构建之后,基本语法将如下所示

Add-Type -Path "path\to\Renci.SshNet.dll"
$conn = New-Object Renci.SshNet.SftpClient -ArgumentList @($HostName, $PortNumber, $UserName, $Password)
$conn.connect()
$files = $conn.ListDirectory("DirName").FullName | where { $_ -like "*.csv"}
$files | foreach { $conn.Delete($_) }

您还可以安装GitBash,然后您的终端中就可以使用ssh命令。

请阅读。更新了我的回复。本机不支持通配符,但使用where也可以轻松实现这一点