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 -AppendStop-Process -Name hMailAdmin*Stop-Process -Name IQWebFTPServerGUI*Stop-Service -DisplayName hMailServerStop-Service -DisplayName "Fastream IQ Web/FTP Server"Stop-Service -DisplayName MySQL -ForceGet-Job | Wait-JobCopy-Item -Path W:\MySQL -Recurse -Destination $destfile Copy-Item -Path W:\hMailServer -Recurse -Destination $destfileCopy-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 hMailServerGet-Job | Wait-JobStart-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
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!
Thank you, that's been very helpful.