[英]Windows Azure Powershell Copying file to VM
我正在嘗試使用 Windows Azure PowerShell 將 zip 文件復制到 VM。 我已經按照文檔設法連接到 VM。
但是,我找不到任何教程可以將 zip 文件上傳/復制/傳輸到 VM 磁盤,比如 C 驅動器。
任何人都可以幫我提供教程的任何鏈接或任何想法我如何復制它?
這是我在此處記錄的另一種方法。 它涉及
下面是一個例子:
#Create and mount a new local VHD
$volume = new-vhd -Path test.vhd -SizeBytes 50MB | `
Mount-VHD -PassThru | `
Initialize-Disk -PartitionStyle mbr -Confirm:$false -PassThru | `
New-Partition -UseMaximumSize -AssignDriveLetter -MbrType IFS | `
Format-Volume -NewFileSystemLabel "VHD" -Confirm:$false
#Copy my files
Copy-Item C:\dev\boxstarter "$($volume.DriveLetter):\" -Recurse
Dismount-VHD test.vhd
#upload the Vhd to azure
Add-AzureVhd -Destination http://mystorageacct.blob.core.windows.net/vhdstore/test.vhd `
-LocalFilePath test.vhd
#mount the VHD to my VM
Get-AzureVM MyCloudService MyVMName | `
Add-AzureDataDisk -ImportFrom `
-MediaLocation "http://mystorageacct.blob.core.windows.net/vhdstore/test.vhd" `
-DiskLabel "boxstarter" -LUN 0 | `
Update-AzureVM
這是我從一些 powershell 示例中獲得並修改的一些代碼。 它適用於使用New-PSSession
創建的會話。 下面還包括一個很酷的包裝器。 最后,我需要發送一個完整的文件夾,所以它也在這里..
將它們捆綁在一起的一些示例用法
# open remote session
$session = Get-Session -uri $uri -credentials $credential
# copy installer to VM
Write-Verbose "Checking if file $installerDest needs to be uploaded"
Send-File -Source $installerSrc -Destination $installerDest -Session $session -onlyCopyNew $true
<#
.SYNOPSIS
Returns a session given the URL
.DESCRIPTION
http://michaelcollier.wordpress.com/2013/06/23/using-remote-powershell-with-windows-azure-vms/
#>
function Get-Session($uri, $credentials)
{
for($retry = 0; $retry -le 5; $retry++)
{
try
{
$session = New-PSSession -ComputerName $uri[0].DnsSafeHost -Credential $credentials -Port $uri[0].Port -UseSSL
if ($session -ne $null)
{
return $session
}
Write-Output "Unable to create a PowerShell session . . . sleeping and trying again in 30 seconds."
Start-Sleep -Seconds 30
}
catch
{
Write-Output "Unable to create a PowerShell session . . . sleeping and trying again in 30 seconds."
Start-Sleep -Seconds 30
}
}
}
<#
.SYNOPSIS
Sends a file to a remote session.
NOTE: will delete the destination before uploading
.EXAMPLE
$remoteSession = New-PSSession -ConnectionUri $remoteWinRmUri.AbsoluteUri -Credential $credential
Send-File -Source "c:\temp\myappdata.xml" -Destination "c:\temp\myappdata.xml" $remoteSession
Copy the required files to the remote server
$remoteSession = New-PSSession -ConnectionUri $frontEndwinRmUri.AbsoluteUri -Credential $credential
$sourcePath = "$PSScriptRoot\$remoteScriptFileName"
$remoteScriptFilePath = "$remoteScriptsDirectory\$remoteScriptFileName"
Send-File $sourcePath $remoteScriptFilePath $remoteSession
$answerFileName = Split-Path -Leaf $WebPIApplicationAnswerFile
$answerFilePath = "$remoteScriptsDirectory\$answerFileName"
Send-File $WebPIApplicationAnswerFile $answerFilePath $remoteSession
Remove-PSSession -InstanceId $remoteSession.InstanceId
#>
function Send-File
{
param (
## The path on the local computer
[Parameter(Mandatory = $true)]
[string]
$Source,
## The target path on the remote computer
[Parameter(Mandatory = $true)]
[string]
$Destination,
## The session that represents the remote computer
[Parameter(Mandatory = $true)]
[System.Management.Automation.Runspaces.PSSession]
$Session,
## should we quit if file already exists?
[bool]
$onlyCopyNew = $false
)
$remoteScript =
{
param ($destination, $bytes)
# Convert the destination path to a full filesystem path (to supportrelative paths)
$Destination = $ExecutionContext.SessionState.`
Path.GetUnresolvedProviderPathFromPSPath($Destination)
# Write the content to the new file
$file = [IO.File]::Open($Destination, "OpenOrCreate")
$null = $file.Seek(0, "End")
$null = $file.Write($bytes, 0, $bytes.Length)
$file.Close()
}
# Get the source file, and then start reading its content
$sourceFile = Get-Item $Source
# Delete the previously-existing file if it exists
$abort = Invoke-Command -Session $Session {
param ([String] $dest, [bool]$onlyCopyNew)
if (Test-Path $dest)
{
if ($onlyCopyNew -eq $true)
{
return $true
}
Remove-Item $dest
}
$destinationDirectory = Split-Path -Path $dest -Parent
if (!(Test-Path $destinationDirectory))
{
New-Item -ItemType Directory -Force -Path $destinationDirectory
}
return $false
} -ArgumentList $Destination, $onlyCopyNew
if ($abort -eq $true)
{
Write-Host 'Ignored file transfer - already exists'
return
}
# Now break it into chunks to stream
Write-Progress -Activity "Sending $Source" -Status "Preparing file"
$streamSize = 1MB
$position = 0
$rawBytes = New-Object byte[] $streamSize
$file = [IO.File]::OpenRead($sourceFile.FullName)
while (($read = $file.Read($rawBytes, 0, $streamSize)) -gt 0)
{
Write-Progress -Activity "Writing $Destination" -Status "Sending file" `
-PercentComplete ($position / $sourceFile.Length * 100)
# Ensure that our array is the same size as what we read from disk
if ($read -ne $rawBytes.Length)
{
[Array]::Resize( [ref] $rawBytes, $read)
}
# And send that array to the remote system
Invoke-Command -Session $session $remoteScript -ArgumentList $destination, $rawBytes
# Ensure that our array is the same size as what we read from disk
if ($rawBytes.Length -ne $streamSize)
{
[Array]::Resize( [ref] $rawBytes, $streamSize)
}
[GC]::Collect()
$position += $read
}
$file.Close()
# Show the result
Invoke-Command -Session $session { Get-Item $args[0] } -ArgumentList $Destination
}
<#
.SYNOPSIS
Sends all files in a folder to a remote session.
NOTE: will delete any destination files before uploading
.EXAMPLE
$remoteSession = New-PSSession -ConnectionUri $remoteWinRmUri.AbsoluteUri -Credential $credential
Send-Folder -Source 'c:\temp\' -Destination 'c:\temp\' $remoteSession
#>
function Send-Folder
{
param (
## The path on the local computer
[Parameter(Mandatory = $true)]
[string]
$Source,
## The target path on the remote computer
[Parameter(Mandatory = $true)]
[string]
$Destination,
## The session that represents the remote computer
# [Parameter(Mandatory = $true)]
[System.Management.Automation.Runspaces.PSSession]
$Session,
## should we quit if files already exist?
[bool]
$onlyCopyNew = $false
)
foreach ($item in Get-ChildItem $Source)
{
if (Test-Path $item.FullName -PathType Container) {
Send-Folder $item.FullName "$Destination\$item" $Session $onlyCopyNew
} else {
Send-File -Source $item.FullName -Destination "$destination\$item" -Session $Session -onlyCopyNew $onlyCopyNew
}
}
}
您不能使用 PowerShell 將文件直接復制到虛擬機的操作系統磁盤(或什至其連接的磁盤之一)。 沒有用於直接與虛擬機內部通信的 API(您需要為此創建自己的自定義服務。
可以使用 PowerShell 通過Set-AzureStorageBlobContent
文件上傳到 Blob。 那時,您可以在虛擬機上通知正在運行的應用程序(可能帶有隊列消息?),有一個文件等待它處理。 處理過程可能就像將文件復制到 VM 的本地磁盤一樣簡單。
.\AzCopy /Source:C:\myfolder /Dest:https://myaccount.blob.core.windows.net/mycontainer/myfolder/ /DestKey:key /Pattern:abc.txt
登錄遠程虛擬機
.\AzCopy /Source:https://myaccount.file.core.windows.net/myfileshare/myfolder/ /Dest:C:\myfolder /SourceKey:key /Pattern:abc.txt
另一種解決方案是使用自定義擴展腳本。
使用自定義擴展腳本允許您將文件復制到 VM,即使 VM 沒有公共 ip(專用網絡)。 所以你不需要配置 winRm 或任何東西。
我過去曾使用自定義擴展腳本進行部署后,例如在 VM 或規模集上安裝應用程序。 基本上,您將文件上傳到 blob 存儲,自定義擴展腳本將在 VM 上下載這些文件。
我在我的 blob 存儲帳戶上創建了一個test-container
並上傳了兩個文件:
deploy.ps1
:在 VM 上執行的腳本。test.txt
:帶有“Hello world from VM”的文本文件這是deploy.ps1
文件的代碼:
Param(
[string] [Parameter(Mandatory=$true)] $filename,
[string] [Parameter(Mandatory=$true)] $destinationPath
)
# Getting the full path of the downloaded file
$filePath = $PSScriptRoot + "\" + $filename
Write-Host "Checking the destination folder..." -Verbose
if(!(Test-Path $destinationPath -Verbose)){
Write-Host "Creating the destination folder..." -Verbose
New-Item -ItemType directory -Path $destinationPath -Force -Verbose
}
Copy-Item $filePath -Destination $destinationPath -Force -Verbose
這是向虛擬機添加自定義腳本擴展的代碼。
Login-AzureRMAccount
$resourceGroupName = "resourcegroupname"
$storageAccountName = "storageaccountname"
$containerName = "test-container"
$location = "Australia East"
$vmName = "TestVM"
$extensionName = "copy-file-to-vm"
$filename = "test.txt"
$deploymentScript = "deploy.ps1"
$destintionPath = "C:\MyTempFolder\"
$storageAccountKeys = (Get-AzureRmStorageAccountKey -ResourceGroupName $resourceGroupName -Name $storageAccountName).Value
$storageAccountKey = $storageAccountKeys[0]
Set-AzureRmVMCustomScriptExtension -ResourceGroupName $resourceGroupName -VMName $vmName -Name $extensionName -Location $location -TypeHandlerVersion "1.9" -StorageAccountName $storageAccountName -StorageAccountKey $storageAccountKey -ContainerName $containerName -FileName $deploymentScript, $filename -Run $deploymentScript -Argument "$filename $destintionPath" -ForceRerun "1"
您可以在復制文件后刪除擴展名:
Remove-AzureRmVMCustomScriptExtension -ResourceGroupName $resourceGroupName -VMName $vmName -Name $extensionName -Force
在我的場景中,我有一個邏輯應用程序,每次將新文件添加到容器時都會觸發該應用程序。 邏輯應用調用 Runbook(需要一個azure 自動化帳戶),添加自定義腳本擴展然后將其刪除。
我能夠在目標服務器上復制二進制文件但無法安裝,我在底部的deploy.ps1
中使用以下語法
powershell.exe Start-Process -Wait -PassThru msiexec -ArgumentList '/qn /i "c:\MyTempFolder\ddagent.msi" APIKEY="8532473174"'
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.