powershell - 使用 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
解决方案
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://mohitgoyal.co/2016/11/10/enable-powershell-remoting-on-azure-rm-virtual-machines/
推荐阅读
- laravel - 使用重定向从另一个控制器方法调用 Laravel 控制器方法
- amazon-web-services - 如何使用 AWS 设置 no-www?
- java - IntelliJ 配置中的命名策略映射标记是什么意思?
- java - 如何使用 Jackson 将 JSON 键值数组动态映射到子对象?
- c# - 防止扫描仪冻结程序
- powerbi - 将选项卡导出为单独的报告
- spring-boot - 多模块 Gradle 项目 - 从 Spring-Boot 1.5 迁移到 2.1
- c - 如何为C中的链表头分配空间?
- angular - Karma 在测试结果上渲染 html
- reactjs - 使用 React 和 Redux 连接在一起的父子组件中的值未定义。应用程序崩溃