- 1 year ago
PowerShell script to stop service and backup a folder in remote path
You can use the following PowerShell script to stop a service and backup a folder on a remote path:
powershell# Remote computer name
$computerName = "REMOTE_COMPUTER_NAME"
# Service name
$serviceName = "SERVICE_NAME"
# Remote folder path
$remoteFolderPath = "\\REMOTE_COMPUTER_NAME\SHARE_FOLDER"
# Backup folder path
$backupFolderPath = "C:\Backup"
# Stop the service
Write-Host "Stopping service $serviceName on $computerName..."
$service = Get-Service -ComputerName $computerName -Name $serviceName
$service.Stop()
# Wait for the service to stop
Write-Host "Waiting for the service to stop..."
$service.WaitForStatus('Stopped')
# Copy the folder to the backup location
Write-Host "Copying folder $remoteFolderPath to $backupFolderPath..."
Copy-Item -Path $remoteFolderPath -Destination $backupFolderPath -Recurse -Force
# Start the service
Write-Host "Starting service $serviceName on $computerName..."
$service.Start()
Replace "REMOTE_COMPUTER_NAME"
with the name of the remote computer where the service and folder are located. Replace "SERVICE_NAME"
with the name of the service you want to stop. Modify the $remoteFolderPath
variable to specify the remote folder path you want to back up. Set the $backupFolderPath
variable to the destination path where you want to store the backup.
Save the script with a .ps1
extension and run it using PowerShell on the local machine. Ensure that you have appropriate permissions and network access to the remote computer and folders.
Note: The script assumes that you have administrative access to the remote computer and appropriate permissions to stop/start the service and access the folders.