首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用powershell将zip文件发送和解压缩到azure VM

使用powershell将zip文件发送和解压缩到azure VM
EN

Stack Overflow用户
提问于 2020-02-19 18:55:26
回答 2查看 936关注 0票数 0

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

代码语言:javascript
运行
复制
$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

误差

代码语言:javascript
运行
复制
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

下面的是在蔚蓝VM上运行'winrm‘命令时的输出

代码语言:javascript
运行
复制
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‘

代码语言:javascript
运行
复制
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
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-02-20 06:28:57

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

这对我来说很管用。

代码语言:javascript
运行
复制
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/

票数 1
EN

Stack Overflow用户

发布于 2020-02-20 05:49:43

这实际上不是风险管理/安全方面的最佳实践。

代码语言:javascript
运行
复制
<#
$username = 'qa-admin'
$pass = ConvertTo-SecureString -string 'xxxxxxxx' -AsPlainText -Force
#>

这..。

代码语言:javascript
运行
复制
<#
$cred = New-Object -typename System.Management.Automation.PSCredential -argumentlist $username, $pass
#>

..。这里有一个内置的cmdlet。

不要在脚本中传递明文密码。以下任一项:

  1. 提示他们
  2. 从预先创建的安全文件中读取它们。

快速安全地存储凭据- PowerShell 在Windows PowerShell中使用密码、安全字符串和凭据

  1. 来自Windows凭据管理器

CredentialManager 2.0 从PowerShell访问Windows凭据管理器 如何使用CredentialManager和PowerShell管理秘密和密码

代码语言:javascript
运行
复制
$cred = Get-Credential -Credential $env:USERNAME

这..。

代码语言:javascript
运行
复制
$session = New-PSSession -ConnectionUri 'http://xx.xx.xxx.xxx:3389' -Credential $cred -SessionOption (New-PSSessionOption -SkipCACheck -SkipCNCheck -SkipRevocationCheck)

...is不正确。你不能这么做。您需要上述结果才能传递给-SessionOption参数。

代码语言:javascript
运行
复制
Get-Help -Name New-PSSessionOption -Examples

<#
NAME
    New-PSSessionOption

SYNOPSIS
    Creates an object that contains advanced options for a PSSession.


Example 1: Create a default session option

    PS C:\>New-PSSessionOption
...

This command creates a session option object that has all of the default values.
Example 2: Configure a session by using a session option object

    PS C:\>$pso = New-PSSessionOption -Culture "fr-fr" -MaximumReceivedObjectSize 10MB
    PS C:\>New-PSSession -ComputerName Server01 -SessionOption $pso

...

Example 3: Start an interactive session

    PS C:\>Enter-PSSession -ComputerName Server01 -SessionOption (New-PSSessionOption -NoEncryption -NoCompression)

...
Example 4: Modify a session option object

    PS C:\>$a = New-PSSessionOption
...

PS C:\> $a.UICulture = (Get-UICulture)
PS C:\> $a.OpenTimeout = (New-Timespan -Minutes 4)
PS C:\> $a.MaximumConnectionRedirectionCount = 1
PS C:\> $a

...

Example 5: Create a preference variable

    PS C:\>$PSSessionOption = New-PSSessionOption -OpenTimeOut 120000

...

Example 6: Fulfill the requirements for a remote session configuration

    PS C:\>$skipCN = New-PSSessionOption -SkipCNCheck
    PS C:\>New-PSSession -ComputerName 171.09.21.207 -UseSSL -Credential Domain01\User01 -SessionOption $SkipCN

...

Example 7: Make arguments available to a remote session

    PS C:\>$team = @{Team="IT"; Use="Testing"}
    PS C:\>$TeamOption = New-PSSessionOption -ApplicationArguments $team
    PS C:\>$s = New-PSSession -ComputerName Server01 -SessionOption $TeamOption
    PS C:\>Invoke-Command -Session $s {$PSSenderInfo.SpplicationArguments}

...

    PS C:\>Invoke-Command -Session $s {if ($PSSenderInfo.ApplicationArguments.Use -ne "Testing") {.\logFiles.ps1} else {"Just testing."}}
...
#>

所以你的是..。

代码语言:javascript
运行
复制
$SO       = New-PSSessionOption -SkipCACheck -SkipCNCheck -SkipRevocationCheck
$session  = New-PSSession -ConnectionUri 'http://xx.xx.xxx.xxx:3389' -Credential $cred -SessionOption $SO

# Process other actions
Send-File -Path C:\testArchive.zip -Destination C:\ -Session $session
Expand-Archive -Path C:\testArchive.zip -DestinationPath C:\ -Session $session
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60307385

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档