Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Moving files on network shares

Tags:

fileshare

I want to automate many of my daily tasks via powershell. One process that we have is imaging desktops after hours. Once this operation is complete, the image files need to be moved off the server hard drives to a network drive. I'm writing an app to run as a service on my machine that will call the powershell script and knock everything out, alerting me only when there is a problem.

I set up a directory on the server for scripts. It works fine if I call my script, FileCopy.ps1, from the server:

copy-item C:\scripts\myFile1.txt -destination C:\scripts\myFile2.txt

However, in my .Net app, I call the script from my local machine:

RunScript(LoadScript(@"\\Server\ServerShare\FileCopy.ps1")); 

Doesn't work. That's because it sees C:\scripts as being on the local machine. So, I change it:

copy-item \\Server\ServerShare\myFile1.txt -destination \\Server\ServerShare\myFile2.txt

Doesn't work. I add another line to the script:

copy-item \\Server\ServerShare\myFile1.txt -destination \\Server\ServerShare\myFile2.txt
get-childitem \\Server\ServerShare | format-table name

It still doesn't copy the file, but it does return the contents of the scripts directory on the server.

So I go back to the server and run the script with the UNC paths in place - powershell returns an error

Copy-Item : Access to the path '\\Server\ServerPath\myFile2.txt' is denied.

That seems to be the root of the problem. Any idea how I can get around this? I'm logged onto the server as an admin.

like image 475
deweycooter Avatar asked Nov 28 '25 20:11

deweycooter


1 Answers

Running the script as admin doesn't matter with network shares IIRC. The default share permission allow only reading. Those perms need to be updated to allow writing to the share.

FYI if you're using net share to create the shares on Windows 7 you will need to use the /grant:<users>,CHANGE to grant those permissions to a user.

like image 181
Keith Hill Avatar answered Dec 02 '25 05:12

Keith Hill



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!