1

I have search for similar answers to this and still I am going round in a circle(s).

I am new to any form of scripting so this is a bastardised script. The script is basically copying log files and data from locations to a remote server and making an append log each time it does it but for the life in me I cant get it to work over the network only local, by changing the $dirname = "D:\${env:computername}".

I would appreciate any feed back and help. This came about from a batch file I created and thought to try and progress in the dark arts.

The script is going to be scheduled to run task when a machines connects to the network.

thanks in advance

update

I get no output or error message from the log file at all no txt or data of any type, As for error messages I am trying to copy from local to server in a vm scenario and will not run, but if I apply this on the local machine it will copy c to d no problem. as I said complete novice

missing function body in function declaration
at line:2 char1
<<<<c:script\copy_log.ps1
+categoryinfo    : parser error: (:) []. ParentContainsErrorRecordException
+FullyQualifiedErrorId : MissingFunctionBody

Apologies for format had to type it as I can c+p from the unit

UPDATE

figured out that the share to the other server was not shared correctly fixed this but the script still does not create a log file

function CopyLogFiles ($sourcePackage) { #used this syntax as I couldn't get anything else to work and took it from here
    $dirName = "\\server\$sourcePackage" #server it is going to
    if (!(Test-Path $dirName)) { mkdir $dirName }
    Copy-Item -Path "C:\Program Files (x86)\ESS-T\$sourcePackage\Logs" -Destination $dirName -Recurse -Force
}

CopyLogFiles AppLauncher_V2.0.0.7
CopyLogFiles MMA_V2.0.0.12
CopyLogFiles MML_V2.0.0.4
CopyLogFiles SerialDataReader_V2.0.0.5 

function Log-Write {
    Param ([string]$LogString)
    Add-Content $LogFile -value $LogString
}

$LogFile = "C:\Program Files (x86)\ESS-T\.log"
1
  • 1
    Hi, can you please edit your question and include the error message you get when using a remote location ? Commented Dec 7, 2015 at 11:58

1 Answer 1

1

Don't reinvent the wheel. Copy-Item is convenient for small cases, but Windows has had robocopy included with every install since Windows 7 and it's faster, more robust, and has logging built in with the /log:FILENAME switch.

https://technet.microsoft.com/en-us/library/cc733145.aspx

Go ahead and test for the existence of your destination & create it manually in your PowerShell script, but leave the logging of the copy operation to robocopy.

Edit: You aren't creating the logfile because you don't define the logfile name until after the rest of your code runs.

Sign up to request clarification or add additional context in comments.

2 Comments

Hi thanks for the replay, I had the robocopy one working as a bat fine that's what got me thinking (if you knew me you would know that's bad) about trying to use powershell to do similar things luckily for me I think I will end up using the batch file but just wanted to know how logging works ,
You can still use PowerShell - just use it to wrap Robocopy.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.