VMware Cloud Community
Balaji3110
Contributor
Contributor
Jump to solution

Backing up DVS via powerCLI - Compress and upload to a remote FTP/SFTP server

I have a requirement  to take backup of all DVSwitches and compress them into a ZIP and upload it to a remote FTP/SFTP server on daily basis.

I am struggling with FTP/SFTP upload portion. Do we have simple commandlets to upload the output of Export-CSV command to FTP directly?

$vCenterlist =  "vCenter1", "vCenter 2"

sl -Path "D:\Test"

$datefolder = New-Item -ItemType Directory -Path ".\$((Get-Date).ToString('dd-MM-yyyy_HH_mm_ss'))"

foreach ($vCenter in $vCenterlist)

{

Connect-VIServer $vCenter

$exportpath = New-Item -ItemType Directory -Path "$datefolder\$vCenter"

$switchlist = Get-VDSwitch

Write-Host "List of Switches in $vCenter"

$switchlist

    foreach($switch in $switchlist)

        {

            Get-VDSwitch $switch | Export-VDSwitch -Destination "$exportpath\$switch.zip"

        }

Disconnect-VIServer $vCenter -Confirm:$false

}

$archfile = compress-archive -Path $datefolder -Destination $($datefolder)

# Set the credentials

$Password = ConvertTo-SecureString 'test@123' -AsPlainText -Force

$Credential = New-Object System.Management.Automation.PSCredential ('root', $Password)

# Set local file path, SFTP path, and the backup location path which I assume is an SMB path

$FilePath = $archfile

$SftpPath = '/ftp/dvswitch-backup/'

# Set the IP of the SFTP server

$SftpIp = '10.1.1.1'

# Establish the SFTP connection

$ThisSession = New-SFTPSession -ComputerName $SftpIp -Credential $Credential

# Upload the file to the SFTP path

Set-SFTPFile -SessionId ($ThisSession).SessionId -LocalFile $FilePath -RemotePath $SftpPath

#Disconnect all SFTP Sessions

Get-SFTPSession | % { Remove-SFTPSession -SessionId ($_.SessionId) }

=====================================================================================

I am using POSH-SSH powershell module for SFTP but it is not recognizing the Set-SFTPfile commandlet and no alternate command found. Is anyone using this option without POSH-SSH ?

Please help.

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

The Passthru parameter was added in psv6.
If you are using PSv5.1 or earlier, you will have to provide the full name on the Destination parameter (including the .zip file) or do a Get-ChildItem on the destination folder to find the .zip file.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
5 Replies
LucD
Leadership
Leadership
Jump to solution

The following works for me.
What kind of error do you get on Set-SFTPFile?

$user = 'sftpuser'

$pswd = 'VMware1!'


$pswdSec = ConvertTo-SecureString -String $pswd -AsPlainText -Force

$cred = New-Object System.Management.Automation.PSCredential ($user, $pwsdSec)


# Set local file path, SFTP path, and the backup location path which I assume is an SMB path

$localFile = 'C:\Temp\whatever.data'

$ftpFolder = '/sftpuser'


# Set the IP of the SFTP server

$ftpServer = 'server.domain'


# Establish the SFTP connection

$session = New-SFTPSession -ComputerName $ftpServr -Credential $cred -AcceptKey


# Upload the file to the SFTP path

Set-SFTPFile -SessionId $session.SessionId -LocalFile $localFile -RemotePath $ftpFolder


#Disconnect all SFTP Sessions

Remove-SFTPSession -SessionId $session.SessionId


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
Balaji3110
Contributor
Contributor
Jump to solution

thank you for reply.

I am trying to  move the compressed file to my SFTP file server using the below code and getting error.

Is there a way to solve this please?

==============================================================================================

$archfile = compress-archive -Path $datefolder -Destination $($datefolder)

# Set the credentials

$Password = ConvertTo-SecureString 'password' -AsPlainText -Force

$Credential = New-Object System.Management.Automation.PSCredential ('root', $Password)

# Set local file path, SFTP path, and the backup location path which I assume is an SMB path

$FilePath = $archfile

$SftpPath = '/ftp/dvswitch-backup/'

# Set the IP of the SFTP server

$SftpIp = '10.1.10.1'

# Establish the SFTP connection

$ThisSession = New-SFTPSession -ComputerName $SftpIp -Credential $Credential

# Upload the file to the SFTP path

Set-SFTPFile -SessionId ($ThisSession).SessionId -LocalFile $FilePath -RemotePath $SftpPath

#Disconnect all SFTP Sessions

Get-SFTPSession | % { Remove-SFTPSession -SessionId ($_.SessionId) }

==============================================================================================

Error:

SFTPerror.PNG

0 Kudos
LucD
Leadership
Leadership
Jump to solution

The Compress-Archive cmdlet does not return the path to the archive file unless you use the -Passthru switch.

So in your script $archfile is empty, and hence the error message.

To capture the path, that line should be

$archfile = compress-archive -Path $datefolder -Destination $($datefolder) -PassThru | Select -ExpandProperty FullName


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
Balaji3110
Contributor
Contributor
Jump to solution

Thanks for the reply and sorry for the late response.

Looks like Compress-Archive is not taking -Passthru parameter and getting below error. Could you please help?

=============================================================================

Compress-Archive : A parameter cannot be found that matches parameter name 'PassThru'.

At C:\Users\balaji071\Desktop\dvswitch-backup.ps1:43 char:76

+ ... chive -Path $datefolder -Destination $($datefolder) -PassThru | Selec ...

+                                                         ~~~~~~~~~

    + CategoryInfo          : InvalidArgument: (:) [Compress-Archive], ParameterBindingException

    + FullyQualifiedErrorId : NamedParameterNotFound,Compress-Archive

=============================================================================

0 Kudos
LucD
Leadership
Leadership
Jump to solution

The Passthru parameter was added in psv6.
If you are using PSv5.1 or earlier, you will have to provide the full name on the Destination parameter (including the .zip file) or do a Get-ChildItem on the destination folder to find the .zip file.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos