VMware Cloud Community
internetrush
Contributor
Contributor

Uploading FTP for VMhosts without console intervention

Want to partially automate uploading support dumps for specific vmware hosts for tickets, not sure if there is a way to do this but here is what i have so far:

function get-VMhostSupportDump {

    param(

    [string]$vmhostname,

    [System.Automation.PScredential]$credential,

    [string]$FullyQualifiedFilePath='c:\temp\test2.tgz'

   

    )

    $uri = "https://$vmhostname/cgi-bin/vm-support.cgi"

    $supportRequest = Invoke-WebRequest -Uri $uri -credential $credential

    if($supportRequest.StatusCode -eq 200){

        write-host "Success, writing file to $FullyQualifiedFilePath"

    }else{

        throw "Cannot get HTTP data from $uri"

    }

    try{

        [io.file]::WriteAllBytes($FullyQualifiedFilePath,($supportRequest.content))

        write-host "Sucessfully wrote TGZ to $FullyQualifiedFilePath"

    }catch{

        throw "Data not written to $FullyQualifiedFilePath : $_"

    }

}

The above function writes out a VMhost support dump in the TGZ format, the dump then needs to be uploaded to an FTPsite, FTPsite.vmware.com specifically:

function upload-FTPsite{

    param(

        $inboundCredential=(Get-Credential),

        $ftpSite='ftp://ftpsite.vmware.com',

        $FullyQualifiedFilePath

       

    )

    # create the FtpWebRequest and configure it

    $ftp = [System.Net.FtpWebRequest]::Create($ftpSite)

    $ftp = [System.Net.FtpWebRequest]$ftp

    $ftp.Method = [System.Net.WebRequestMethods+Ftp]::UploadFile

    $ftp.Credentials = new-object System.Net.NetworkCredential(($inboundCred.GetNetworkCredential().username),($inboundCred.GetNetworkCredential().password))

    $ftp.UseBinary = $true

    $ftp.UsePassive = $true

    # read in the file to upload as a byte array

    $content = [System.IO.File]::ReadAllBytes($FullyQualifiedFilePath)

    $ftp.ContentLength = $content.Length

    # get the request stream, and write the bytes into it

    $rs = $ftp.GetRequestStream()

    $rs.Write($content, 0, $content.Length)

    # be sure to clean up after ourselves

    $rs.Close()

    $rs.Dispose()

}

The above function more or less works, but i cannot get the URI correct even for an existing directory. Anyone got anything on this that they can share / know?

Would like to basically take the one function and foreach into another so i can auto-upload to the site. Anyway to do this?

Tags (2)
Reply
0 Kudos
1 Reply
LucD
Leadership
Leadership

Are you getting any error messages?


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

Reply
0 Kudos