first script problems, robust ?

rated by 0 users
Not Answered This post has 0 verified answers | 2 Replies | 1 Follower

Not Ranked
2 Posts
DaveKimble posted on 04-22-2012 11:58 PM

I have just written my first Powershell script, which temporarily stops my web, mail and MySQL servers, and backs up all the dynamic data, before restarting.

It has a couple of wrinkles that I just can't sort out.
1.    The first directory copy (line 10) doesn't create a corresponding directory, but does copy the files and directories in it.
    Using -Verbose shows it doesn't even try to create it.
    If I switch lines 10 to 12 around, it is always line 10 that goes wrong.
2.    Restarting the mail server's GUI (line 18) acts as if it has "-WindowStyle minimized"

On a more general level, the cmdlets don't seem to output anything loggable, so the whole thing feels as if it's not very robust.

Often stopping MySQL with services.msc gets stuck, so I'm concerned about the whole thing hanging.

Any comments appreciated.


$destfile = "U:\W."+(Get-Date -uformat "%Y-%m-%d.%H-%M")
$title = "===========================================`nBacking up W:\ to "+$destfile
$title | Out-File -FilePath U:\backup.log -Append
Stop-Process -Name hMailAdmin*
Stop-Process -Name IQWebFTPServerGUI*
Stop-Service -DisplayName hMailServer
Stop-Service -DisplayName "Fastream IQ Web/FTP Server"
Stop-Service -DisplayName MySQL -Force
Get-Job | Wait-Job
Copy-Item -Path W:\MySQL       -Recurse -Destination $destfile
Copy-Item -Path W:\hMailServer -Recurse -Destination $destfile
Copy-Item -Path W:\webserver   -Recurse -Destination $destfile
# Copy-Item -Path W:\domains   -Recurse -Destination $destfile
Start-Service -DisplayName MySQL
Start-Service -DisplayName "Fastream IQ Web/FTP Server"
Start-Service -DisplayName hMailServer
Get-Job | Wait-Job
Start-Process -FilePath "C:\Program Files (x86)\hMailServer\Bin\hMailAdmin.exe"
Start-Process -FilePath "C:\Program Files (x86)\Fastream IQ Web FTP Server GUI\IQWebFTPServerGUI.exe"
$end = "Finished"
$end | Out-File -FilePath U:\backup.log -Append


All Replies

Top 10 Contributor
640 Posts

I'm afraid I can't help with any feelings of robustness ;). You'd need to take that up with MS or with your application vendors. I'll say this: With PowerShell, 'no news is good news.' Cmdlets will output trappable errors (if their -ErrorAction is set to "Stop"), so if an error occurs you can log that. I don't know that I equate "outputs loggable text" with "robust," but that might just be me - I'm used to working with it after 6 or so years.

First, there's no need to spend cycles creating your $title and $end variables. Just pipe the strings directly to Out-File:

"this" | out-file log.txt

Second, Copy-Item isn't supposed to create that top-level destination folder. It copies to the destination you provide. So you'll need to create that folder as a first step, and specify it as the destination, if that's the goal.

Third, if yo explicitly state "-WindowsStyle Normal" (if that's what you're after), does it still start the application minimized? If so, it's the application that's triggering that, not Start-Process; all the cmdlet can do is tell the application what's requested - it can't enforce it.

Fourth, regarding MySQL hanging on stopping... that's something you'd need to take up with the MySQL folks, unfortunately. PowerShell doesn't have any super powers in that regard. I've not seen Stop-Service permanently hang. What you could do is try Stop-Service, trap for an error, and if you get an error then run Stop-Process -force instead to kill the services' process rather than stopping it cleanly. You could log the fact that you'd done so, since you're creating a log. Pretty much how you'd deal with it if you were using the GUI, I expect.

Hope that helps!

Not Ranked
2 Posts

Thank you, that's been very helpful.

Page 1 of 1 (3 items) | RSS
Copyright 2012 All rights reserved.