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

PowerShell script to stop service and backup a folder in remote path

如何

  1. 停止服务
  2. 根据系统日期
  3. 在远程路径中备份 folder/contents
  4. 删除原文件夹的内容
  5. 终于开始服务了
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

以上代码出错。

这应该可以解决问题,很确定您可能遇到的问题是围绕字符串中间的 (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