%EB%A1%9C%20%ED%8C%8C%EC%9D%BC%EC%9D%84%20%EB%B3%B5%EC%82%AC%ED%95%98%EB%8A%94%20%EB%B0%A9%EB%B2%95.png)
아래 Power Shell 명령을 사용하여 서버 "SFTP"에서 Windows 서버로 파일을 복사하고 있습니다. 어떤 이유로 스크립트가 작동하지 않습니다. 도와주세요.
# Scriptname.ps1
# send the files to Win-Server server F:\data\in
# Source files are deleted after transfer
# Local Path is the source path
# RemotePath is the flies destination path
Function Scriptname {
Param(
[Parameter(Mandatory=$true)]
[ValidateNotNull()]
[string] $Username = $(throw "Username parameter is required"),
[Parameter(Mandatory=$true)]
[ValidateNotNull()]
[string] $Password = $(throw "Password parameter is required"),
[Parameter(Mandatory=$true)]
[ValidateNotNull()]
[string] $HostName = $(throw "HostName parameter is required"),
[Parameter(Mandatory=$true)]
[ValidateNotNull()]
[string] $RemotePath = $(throw "RemotePath parameter is required"),
[Parameter(Mandatory=$true)]
[ValidateNotNull()]
[string] $LocalPath = $(throw "LocalPath parameter is required"),
[Parameter(Mandatory=$true)]
[ValidateNotNull()]
[string] $SshHostKeyFingerprint = $(throw "SshHostKeyFingerprint parameter is required"),
$Remove=$true
)
if( -not (Test-Path $LocalPath)) {
throw("ERROR: Unable to locate LocalPath (path=${LocalPath})")
}
$Invocation = (Get-Variable MyInvocation -Scope 1).Value
$SftpModuleDirectory = Split-Path $Invocation.MyCommand.Path
[Reflection.Assembly]::LoadFrom("${SftpModuleDirectory}\WinSCPnet.dll") | Out-Null
# Setup session options
$sessionOptions = New-Object WinSCP.SessionOptions
$sessionOptions.Protocol = [WinSCP.Protocol]::Sftp
$sessionOptions.HostName = $HostName
$sessionOptions.UserName = $Username
$sessionOptions.Password = $Password
$sessionOptions.SshHostKeyFingerprint = $SshHostKeyFingerprint #"ssh-rsa 1024 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx"
$session = New-Object WinSCP.Session
# connect to FTP session
try {
$session.Open($sessionOptions)
$session.GetFiles($remotePath, $localPath,$remove).Check()
} catch {
if($_.Exception.ToString().Contains("Host key wasn't verified!")) {
throw("invalid SshHostKeyFingerprint, unable to open session to FTP (host=${HostName}, SshHostKeyFingerprint=${SshHostKeyFingerprint})")
}
elseif($_.Exception.ToString().Contains("No supported authentication methods available")) {
throw("Unable to open session to FTP (host=${HostName}, username=${Username})")
}
}
finally
{
# Disconnect, clean up
$session.Dispose()
}
}
$UserName = GetEnvironmentConfigValue "Scriptname.UserName"
$Password = GetEnvironmentConfigValue "Scriptname.Password"
$HostName = GetEnvironmentConfigValue "Scriptname.HostName"
$RemotePath = GetEnvironmentConfigValue "Scriptname.RemotePath"
$LocalPath = GetEnvironmentConfigValue "Scriptname.LocalPath"
$SshHostKeyFingerprint = GetEnvironmentConfigValue "Scriptname.SshHostKeyFingerprint"
$Remove=$true
Write-host "values: ${Username} ${Password} ${HostName} ${RemotePath} ${LocalPath} ${SshHostKeyFingerprint}"
SFTPUploadFiles $Username $Password $HostName $RemotePath $LocalPath $SshHostKeyFingerprint
답변1
좋아요, 함수 이름이 잘못되어 스크립트가 파일을 복사하지 못하게 되었다는 사실을 알아낸 것 외에도 Powershell 스플래팅을 보여 드리고 싶습니다.
$UserName = GetEnvironmentConfigValue "Scriptname.UserName"
$Password = GetEnvironmentConfigValue "Scriptname.Password"
$HostName = GetEnvironmentConfigValue "Scriptname.HostName"
$RemotePath = GetEnvironmentConfigValue "Scriptname.RemotePath"
$LocalPath = GetEnvironmentConfigValue "Scriptname.LocalPath"
$SshHostKeyFingerprint = GetEnvironmentConfigValue "Scriptname.SshHostKeyFingerprint"
$Remove=$true
Write-host "values: ${Username} ${Password} ${HostName} ${RemotePath} ${LocalPath} ${SshHostKeyFingerprint}"
SFTPUploadFiles $Username $Password $HostName $RemotePath $LocalPath $SshHostKeyFingerprint
다음은 동일하지만 스플래팅이 있습니다. 모든 해시테이블 키(= 기호 앞의 문자열)가 함수 매개변수 이름과 일치하는 한 함수에 전달할 수 있는 해시테이블을 사용합니다.
$Parameters = @{
"UserName" = GetEnvironmentConfigValue "Scriptname.UserName"
"Password" = GetEnvironmentConfigValue "Scriptname.Password"
"HostName" = GetEnvironmentConfigValue "Scriptname.HostName"
"RemotePath" = GetEnvironmentConfigValue "Scriptname.RemotePath"
"LocalPath" = GetEnvironmentConfigValue "Scriptname.LocalPath"
"SshHostKeyFingerprint" = GetEnvironmentConfigValue "Scriptname.SshHostKeyFingerprint"
}
$Parameters
SFTPUploadFiles @Parameters -Remove:$false
귀하의 함수는 기본적으로 Remove를 true로 설정하므로 이를 지정하는 것은 중복됩니다. 내 예에서는 스플래팅에 사용되는 해시테이블과 일반 매개변수를 혼합하고 일치시킬 수 있음을 보여줍니다.