'PowerShell New-PSSession is not working in VSCode Jupyter Notebook

The code below execute correctly when:

  • Copy pasting in PowerShell command prompt 5.1
  • Copy pasting in command prompt 7.2
  • Executed as a ps1 file in VScode
$ExchangeServer = "server1"
$ServiceAccount = "[email protected]"
$token = get-mytoken -name $ServiceAccount 
$password = ConvertTo-SecureString $token -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential ($ServiceAccount, $password)

New-PSSession -ConfigurationName Microsoft.Exchange `
                         -ConnectionUri http://$ExchangeServer/powershell `
                         -Authentication Kerberos `
                         -Credential $credential `
                         -ErrorAction continue 

But it triggers the error below while running in a VSCode ".Net Interactive Notebook" from a .ipynd file:

[server1] An error has occurred which PowerShell cannot handle. 
A remote session might have ended. Cannot validate argument on parameter 'Session'. The argument is null. 
Provide a valid value for the argument, and then try running the command again. [server1l] 
An error has occurred which PowerShell cannot handle. A remote session might have ended. 
Cannot validate argument on parameter 'Session'. The argument is null. Provide a valid value for the argument, and then try running the command again. 
Could not load file or assembly 'System.Composition.AttributedModel, Version=1.0.32.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. Could not find or load a specific file. (0x80131621)

Other PowerShell command run fine but I still get this error with New-PSSession.

Thanks for your support.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source