首页 > 解决方案 > 使用 powershell 将 zip 文件发送并提取到 azure VM

问题描述

我正在尝试将 zip 文件发送并解压缩到 azure VM,但无法连接到远程 Azure VM。

代码

$cred = Get-Credential
$SO = New-PSSessionOption -SkipCACheck -SkipCNCheck -SkipRevocationCheck
$session = New-PSSession -ConnectionUri 'http://xx.xx.xxx.xxx:3389' -Credential $cred -SessionOption $SO

Send-File -Path C:\testArchive.zip -Destination C:\ -Session $session
Expand-Archive -Path C:\testArchive.zip -DestinationPath C:\ -Session $session

错误

New-PSSession : [xx.xx.xxx.xxx] Connecting to remote server xx.xx.xxx.xxx 
failed with the following error message : The client cannot connect to the 
destination specified in the request. Verify that the service on the 
destination is running and is accepting requests. Consult the logs and 
documentation for the WS-Management service running on the destination, most 
commonly IIS or WinRM. If the destination is the WinRM service, run the 
following command on the destination to analyze and configure the WinRM 
service: "winrm quickconfig". For more information, see the 
about_Remote_Troubleshooting Help topic.
At line:4 char:12
+ $session = New-PSSession -ConnectionUri 'http://xx.xx.xxx.xxx:3389' - ...
+            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OpenError: (System.Manageme....RemoteRunspace:Re 
   moteRunspace) [New-PSSession], PSRemotingTransportException
    + FullyQualifiedErrorId : CannotConnect,PSSessionOpenFailed

以下是我在 azure VM 上运行“winrm quickconfig”命令时的输出

WinRM service is already running on this machine.
WinRM is already set up for remote management on this computer.

当我运行“Enter-PSSession -ComputerName LoadTestVm -Port 3389 -Credential qa-admin”时

Enter-PSSession : Connecting to remote server LoadTestVm failed with the following error 
message : The WinRM client cannot process the request because the server name cannot be 
resolved. For more information, see the about_Remote_Troubleshooting Help topic.
At line:1 char:1
+ Enter-PSSession -ComputerName LoadTestVm -Port 3389 -Credential qa-ad ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (LoadTestVm:String) [Enter-PSSession], PSRem 
   otingTransportException
    + FullyQualifiedErrorId : CreateRemoteRunspaceFailed

标签: powershellvirtual-machinerdpwinrmazure-vm

解决方案


WINRM 将在端口 5985 和 5986 上运行。端口 5985 用于 HTTP,5986 用于 HTTPS。默认情况下,如果您没有指定它,它会使用端口 5985 -port。您应该指定端口 5985 而不是 3389,如果有,也应在 NSG 中启用它。这样你就可以跑了Enter-PSSession -ComputerName "PublicIPaddress of VM" -Port 5985 -Credential $cred

这对我有用。

Copy-Item -Path D:\nancy\4.zip -Destination C:\ –ToSession $session

Invoke-Command -Session $session -ScriptBlock { Expand-Archive -Path C:\4.zip -DestinationPath C:\ }

更多参考:

https://www.assistanz.com/access-azure-windows-vm-through-powershell/

https://geekdudes.wordpress.com/2016/11/16/enabling-remote-powershell-connection-to-azure-virtual-machine/

https://mohitgoyal.co/2016/11/10/enable-powershell-remoting-on-azure-rm-virtual-machines/


推荐阅读