Thanks Micky.
I'd still go back to permissions - the only way I've been able to get the same error is if the user doesn't have the rights it needs. I think it needs access to each database but only the public role inside each (i.e. the sys.database_files view, but that's unconfirmed, I'm not sure what SMO uses to get the data).
I notice you're setting the user/password in the script so it'll be that rather than your own credentials.
Thought it might be a server not using mixed mode but that gives a different error.
As far as ISE/agent difference goes, I haven't been able to reproduce & I can't explain it.
Only guess I have is that the ISE can reuse variables' contents between executions but I'm not sure that applies here.
Cheers
Gaz