%20%E3%81%AB%E3%83%95%E3%82%A1%E3%82%A4%E3%83%AB%E3%82%92%E3%82%B3%E3%83%94%E3%83%BC%E3%81%99%E3%82%8B%E6%96%B9%E6%B3%95.png)
私は以下のPowerShellコマンドを使用して、サーバー「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 に設定しているため、これを指定するのは冗長です。私の例では、通常のパラメータとスプラッティングに使用されるハッシュテーブルを組み合わせて使用できることを示しています。