StUffz
StUffz

Reputation: 123

Check if FTP Username and Password are correct in PowerShell

I'd like to check if the provided username and password is correct before doing anything else on the FTP server.

As of now, I establish a connection via:

$cred = Get-Credential

$FTPlog = New-Object System.Net.WebClient
$FTPlog = [system.net.ftpwebrequest][system.net.webrequest]::create($server)
$FTPlog.Credentials = $cred

$FTPlog | Get-Member doesn't show anything useful I could use to check if the login was successful or not.

I could probably try a directory listing and check this one for errors, but I'd prefer a solution which doesn't add unnecessary lines to the code. Are there any other options?

Upvotes: 1

Views: 945

Answers (1)

Kory Gill
Kory Gill

Reputation: 7161

My understanding is that until you make a Method call, I do not believe you can gather any status from StatusCode or StatusDescription. I would think that PrintWorkingDirectory is the most lightweight call you can make. Although why not just write the code you want and handle errors centrally regardless of when they happen? If authentication fails, that will surely be the first thing you need to handle.

Upvotes: 1

Related Questions