Can't run remote Windows Server backup using Powershell 7 thru SSH

Eldon Li 0 Reputation points Microsoft Employee
2023-04-30T11:33:47.22+00:00

Task: Run Windows Server backup (2019) using Powershell 7 SSH remote session (Win11)

Environment:

Windows Server 2019 , install Powershell 7.3.4.0, install OpenSSH

Windows 11 , install Powershell 7.3.4.0

Step

In Window 11, run PowerShell 7

1.enter a remote session under SSH

$session = New-PSSession -HostName 2019EL1 -UserName administrator

Enter-PSSession $session

  1. Find no WindowsServerBackup module avaliable, and add a WindowsServerBackup maually

import-module -Name WindowsServerBackup -SkipEditionCheck

get-module -ListAvailable

then below item added

Script 0.0 windowsserverbackup

Script 1.0 windowsServerBackup {Add-WBBackupTarget, Add-WBBareMetalRecovery, Add…

Binary 1.0.0.0 windowsServerBackup {Add-WBBackupTarget, Add-WBBareMetalRecovery, Add…

3.run Backup script (below script can run locally or thry WinRM PS session, but not in PS7 SSH remote session

============

$Policy = New-WBPolicy

Add-WBSystemState $Policy

Add-WBBareMetalRecovery $Policy

$BackupLocation = New-WBBackupTarget -VolumePath "E:"

Add-WBBackupTarget -Policy $Policy -Target $BackupLocation

Set-WBVssBackupOptions -Policy $Policy -VssCopyBackup

Start-WBBackup -Policy $Policy

============

error

$Policy = Get-WBPolicy

Add-WBSystemState $Policy

Add-WBSystemState: Cannot validate argument on parameter 'Policy'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.

Anyone has any recommendation, how to resolve this error ?

Thanks

Windows Server 2019
Windows Server 2019
A Microsoft server operating system that supports enterprise-level management updated to data storage.
3,531 questions
Windows Server Backup
Windows Server Backup
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.Backup: A duplicate copy of a program, a disk, or data, made either for archiving purposes or for safeguarding valuable files from loss should the active copy be damaged or destroyed.
458 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,424 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Khaled El-Sayed Mohamed 1,165 Reputation points
    2023-06-12T10:23:35.6033333+00:00

    Hi YF

    kindly try the sample PowerShell script that demonstrates how to run a Windows Server 2019 backup using PowerShell 7 and an SSH remote session from a Windows 11 machine:

    # Install the SSH module if not already installed
    if (-not (Get-Module -Name SSHSession -ListAvailable)) {
        Install-Module -Name SSHSession -Force
    }
    
    # Import the SSH module
    Import-Module -Name SSHSession -Force
    
    # Set the remote server details
    $remoteServer = "ServerIP"
    $remoteUsername = "Username"
    $remotePassword = "Password"
    
    # Create a new SSH session
    $session = New-SSHSession -ComputerName $remoteServer -Credential (Get-Credential -UserName $remoteUsername -Password $remotePassword)
    
    # Define the backup target location
    $backupTarget = "D:\"
    
    # Run the backup command remotely
    Invoke-SSHCommand -SessionId $session.SessionId -Command "wbadmin start backup -backupTarget:$backupTarget -allCritical -quiet"
    
    # Close the SSH session
    Remove-SSHSession -SessionId $session.SessionId
    

    Make sure to replace "ServerIP", "Username", and "Password" with the appropriate values for your remote server.

    This script first checks if the SSH module is installed and installs it if necessary. Then, it imports the SSH module and sets the remote server details.

    Next, it creates an SSH session using the New-SSHSession cmdlet, providing the remote server IP, username, and password.

    The backup target location is defined using the $backupTarget variable.

    Finally, the script uses the Invoke-SSHCommand cmdlet to run the backup command remotely on the Windows Server 2019 machine. After the backup command completes, the SSH session is closed using Remove-SSHSession.

    Please note that this script assumes you have PowerShell 7 and the SSH module installed on your Windows 11 machine. Also, ensure that you have proper permissions and follow best practices for secure remote management.

    0 comments No comments

  2. Eldon Li 0 Reputation points Microsoft Employee
    2023-06-13T10:45:37.8066667+00:00
    1. The Invoke-SSHCommand is not valid

    Invoke-SSHCommand: The term 'Invoke-SSHCommand' is not recognized as a name of a cmdlet, function, script file, or executable program.

    Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

    2 I know I can workaround it using wbadmin.exe in the remote SSH, but the main problem is I can't run Windows backup PS cmdlet in PS7 remote SSH

    0 comments No comments