networking - Powershell script to copy file to network runs manually, but not from Task Scheduler -


i running script copy file local network location. script works if call myself via powershell, not task scheduler. have looked @ this , many other articles, cannot work. right can @ task scheduler, "last run result" , says task running couple minutes before stop it. when end task don't exception (code in try/catch)

i have serverlocation , serverfullpath broken because trying few different examples.

the code follows (with domain/username/password substituted):

set-variable servercredentials -option constant -value "<domain>/<username>  <password>" set-variable serverlocation -option constant -value "\\<domain>" set-variable serverfullpath -option constant -value $serverlocation"\path\path"  $latest = get-childitem -path .\ | sort-object lastwritetime -descending | select-object -first 1 net use $serverfullpath /user:$servercredentials  copy-item .\$latest $serverfullpath -force 

i new powershell, thoughts on how troubleshoot? need more information?

the answer lies in how "net use" works. typing "net use" cmd showed had connection, , seems having connection make net use want prompt, why seemed script hanging. tried deleting connection "net use * /delete /y", above original net use, , allowed script continue correctly. looked like:

net use * /delete /y net use $serverfullpath /user:$servercredentialuser $servercredentialpassword 

the key here \y on end, silently answer yes prompt.

as aside this link shows how use /delete param. chose use * here, seems have deleted specific connection.

however sparked maybe net use prompting me in way connection there, changed original line to:

net use $serverfullpath /user:$servercredentialuser $servercredentialpassword /y 

with /y on end works fine, , :)


Comments

Popular posts from this blog

php - Wordpress: Link a post to posts of another category manually -

delphi xe7 upload/download dropbox file using the rest client and show progress -

java - Get more than One value and display it -