PowerShell脚本可停止服务并备份远程路径中的文件夹

时间:2019-07-17 14:06:45

标签: windows powershell copy backup

操作方法

  1. 停止服务
  2. 按照系统日期在远程路径中备份文件夹/内容
  3. 删除原始文件夹的内容
  4. 最后启动服务
Get-Service SERVICENAME
Stop-Service SERVICENAME -Force –PassThru
Start-Sleep -s 20

Copy-Item -Path \\remote path\folderderA -Destination \\Remoate path\folderA(Get-Date) -Recurse -Verbose
Remove-Item -Path \\remote path\folderA -Verbose

Start-Sleep -s 20
Start-Service SERVICENAME -Force –PassThru
Get-Service SERVICENAME

上面的代码抛出错误。

1 个答案:

答案 0 :(得分:1)

这应该可以解决问题,非常确定您可能遇到的问题是字符串中间的(Get-Date),并且事实目录中不能包含特殊字符,例如/或:日期。

就像其他人所说的那样,请以管理员身份进行操作。

Get-Service SERVICENAME | Stop-Service -Force
Start-Sleep -seconds 20

$Date = (Get-Date).ToString().Replace("/","-")
$Date = $Date.Replace(":","-")

Copy-Item "\\remote path\folderderA" -Destination "\\Remoate path\folderA-$Date" -Recurse -Verbose
if(Test-Path("\\Remoate path\folderA-$Date")){Remove-Item -Path \\remote path\folderA -Verbose}
Start-Sleep -seconds 20

Start-Service SERVICENAME
Get-Service SERVICENAME