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

Eldon Li 0 Reputation points Microsoft Employee
2023-04-30T11:31:49.97+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 for business | Windows Server | Devices and deployment | Set up, install, or upgrade
Windows for business | Windows Server | User experience | PowerShell
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Limitless Technology 44,766 Reputation points
    2023-05-02T15:13:14.2066667+00:00

    Hi,

    I'd be happy to help you out with your question. Sorry for the inconvenience caused.

    I think the issue may be related to the scope of the PowerShell session you are using to run the backup script. When you create a new PSSession, the session starts with a default set of modules loaded, and any changes you make to the module loading or environment variables are only available within the scope of that session.

    To resolve the issue, I recommend modifying your PowerShell session creation command to include the -InitializationScript parameter, which allows you to specify a script that runs when the session is created. This script can load the WindowsServerBackup module into the new PSSession, making it available for use in your backup script.

    Refer to following example of how you can modify your PowerShell session creation command to load the WindowsServerBackup module:

    $session = New-PSSession -HostName 2019EL1 -UserName administrator -InitializationScript {Import-Module WindowsServerBackup}

    This command includes an initialization script that imports the WindowsServerBackup module into the new PSSession. Once you have established the session with this command, any subsequent commands you run within that session should be able to access the WindowsServerBackup module.

    Also, please ensure that the PowerShell version on your Windows Server 2019 machine is also 7.3.4.0 or later and that the OpenSSH server version is compatible with PowerShell 7. You can check the compatibility matrix for PowerShell and OpenSSH server versions to ensure they are compatible.

    If you have any other questions or need assistance with anything, please don't hesitate to let me know. I'm here to help.

    If the reply was helpful, please don’t forget to upvote or accept as answer, thank you.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.