{"id":6898,"date":"2023-05-24T08:21:49","date_gmt":"2023-05-24T08:21:49","guid":{"rendered":"https:\/\/pariswells.com\/blog\/?p=6898"},"modified":"2023-05-24T08:21:52","modified_gmt":"2023-05-24T08:21:52","slug":"vb-net-script-to-upload-files-to-sftp-via-cert-based-auth","status":"publish","type":"post","link":"https:\/\/pariswells.com\/blog\/research\/vb-net-script-to-upload-files-to-sftp-via-cert-based-auth","title":{"rendered":"VB.net script to Upload files to SFTP via Cert based Auth"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code class=\"\">Dim fileName as string\r\n\r\nDim uploadFolder as string\r\n\r\nfilename = \u201cc:\\temp\\*\u201d \u2018 files to be loaded from this folder. wilcards can be used\r\n\r\nuploadFolder = \u201c\/upload\/\u201d \u2018 Default folder where files will be loaded\r\n\r\nTry\r\n\r\nDim sessionOptions as new WinSCP.SessionOptions\r\n\r\nwith sessionOptions\r\n\r\n.Protocol = WinSCP.Protocol.Sftp\r\n\r\n.HostName = GBL_SFTP_HOST_NAME\r\n\r\n.UserName = GBL_SFTP_USERNAME\r\n\r\n.SshPrivateKeyPath = GBL_SFTP_PRIVATEKEY_PATH\r\n\r\n.PortNumber = GBL_SFTP_PORT\r\n\r\n.SshHostKeyFingerprint = GBL_SFTP_HOST_SSH_KEY\r\n\r\nend with\r\n\r\nusing session as new WinSCP.Session\r\n\r\nsession.Open(sessionOptions)\r\n\r\n\r\nDim transferOptions as new WinSCP.TransferOptions\r\n\r\ntransferOptions.FilePermissions = $Null\r\n\r\ntransferOptions.PreserveTimestamp = $False\r\n\r\nDim transferResult as WinSCP.TransferOperationResult\r\n\r\ntransferResult = session.PutFiles(Filename, uploadFolder, false, transferOptions)\r\n\r\ntransferResult.Check() \u2018THrows the first error if not successful\r\n\r\nFor Each transfer as WinSCP.TransferEventArgs in transferResult.Transfers\r\n\r\n\u2018Write results to Event Viewer or SQL table or Log Table\r\n\r\n\u2018This will display a message box with the results\r\n\r\nMessageBox.Show(\u201cUpload of file \u201c &amp; transfer.FileName &amp; \u201d successful.\u201d)\r\n\r\nNext\r\n\r\n\r\nend using\r\n\r\nCatch ex as System.Exception\r\n\r\nMessageBox.Show(ex.ToString()) \u2018Display any Exceptions\r\n\r\nFinally\r\n\r\nEnd Try\r\n\r\nreturn true<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-6898","post","type-post","status-publish","format-standard","hentry","category-research"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/posts\/6898","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/comments?post=6898"}],"version-history":[{"count":1,"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/posts\/6898\/revisions"}],"predecessor-version":[{"id":6899,"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/posts\/6898\/revisions\/6899"}],"wp:attachment":[{"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/media?parent=6898"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/categories?post=6898"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/tags?post=6898"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}