问题描述
我具有以下Azure Automation Runbook脚本,其目标是从REST API调用中进行转储/导出,该调用必须从能够访问REST API设备的目标设备上运行。因此,Azure自动化运行手册的目标是“代理服务器”,然后从中进行REST API备份。
该方法一直有效,但一旦'cm.vm.run_command'出现输出大小限制并正在截断备份,便无法从目标服务器复制此备份文件。我们为此找到的解决方法是将备份文件从“目标/代理服务器”直接复制到安装在目标/代理服务器上的存储帐户文件共享中。我现在的问题是,从Azure自动化运行时,它无法访问其他用户安装的驱动器和/或无法加载设备或直接访问设备(如以下错误消息)。有人对此有其他选择吗?我能够检查Runbook在t的存储帐户端口443/445上是否具有连接。这是此处https://docs.microsoft.com/en-us/azure/storage/files/storage-troubleshoot-windows-file-connection-problems
所述的可能原因之一下面是我收到的命令和错误以及所使用的整个脚本。
copy-item -Path C:\Devicebackup.txt -Destination \\storage_account_name.file.core.windows.net\configdatafileshare\orchestration
net use w: \\storage_account_name.file.core.windows.net\configdatafileshare\orchestration `'/yBapkthow==`' /user:Azure\storage_account_name
copy-item : The network path was not found
At C:\Packages\Plugins\Microsoft.CPlat.Core.runcommandWindows\1.1.5\Downloads\s
cript9.ps1:15 char:1
+ copy-item -Path C:\Devicebackup.txt -Destination \\storage_account_name. ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [copy-Item],IOException
+ FullyQualifiedErrorId : System.IO.IOException,Microsoft.PowerShell.Comma
nds.copyitemcommand
The option /DL2D2QKD1OU2ZKEOJVRK4LGPIRTJKAJBZ+EDKNHWVYYEJDDYSL9CPB5T8F/9VWQBMBWC37B1njs4YBAPKTHOW== is unkNown.
The Syntax of this command is:
NET USE
[devicename | *] [\\computername\sharename[\volume] [password | *]]
[/USER:[domainname\]username]
[/USER:[dotted domain name\]username]
[/USER:[username@dotted domain name]
[/SMARTCARD]
[/SAVECRED]
[[/DELETE] | [/PERSISTENT:{YES | NO}]]
NET USE {devicename | *} [password | *] /HOME
NET USE [/PERSISTENT:{YES | NO}]
Param (
[Parameter(Mandatory=$false)][string] $rgName,[Parameter(Mandatory=$false)][string] $ProxyServerName
)
function CreatePSCommandFile {
Param(
[parameter(Mandatory=$true)][String[]]$DeviceName,[parameter(Mandatory=$true)][String[]]$DeviceIP,[parameter(Mandatory=$true)][String[]]$ApiToken
)
$remoteCommand =
@"
add-type @`"
using System.Net;
using System.Security.Cryptography.X509Certificates;
public class TrustAllCertsPolicy : ICertificatePolicy {
public bool CheckValidationResult(
ServicePoint srvPoint,X509Certificate certificate,WebRequest request,int certificateProblem) {
return true;
}
}
`"@
[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
Invoke-WebRequest -Uri 'www.mydownload.com' -UseBasicParsing -Headers @{ Authorization="Bearer $($ApiToken)" } | Out-file C:\Devicebackup.txt
net use w: \\storage_account_name.file.core.windows.net\configdatafileshare\orchestration `'/STORAGE_KEY+EDknHWvyyeJDDYsL9cPB5T8F/9VwqBmbwc37B1njs4yBapkthow==`' /user:Azure\storage_account_name
copy-item -Path C:\Devicebackup.txt -Destination \\storage_account_name.file.core.windows.net\configdatafileshare\orchestration
"@
Set-Content -Path .\InvokeCommand.ps1 -Value $remoteCommand
}
$connectionName = "AzureRunAsConnection"
try {
# Get the connection "AzureRunAsConnection "
$servicePrincipalConnection = Get-AutomationConnection -Name $connectionName
Write-Host "Logging in to Azure..."
$connectionResult = Connect-AzAccount `
-ServicePrincipal `
-Tenant $servicePrincipalConnection.TenantID `
-ApplicationId $servicePrincipalConnection.ApplicationID `
-CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint
}
catch {
if (!$servicePrincipalConnection)
{
$ErrorMessage = "Connection $connectionName not found."
throw $ErrorMessage
} else{
Write-Error -Message $_.Exception
throw $_.Exception
}
}
function Backup-Device {
Param (
[Parameter(Mandatory=$false)][string] $DeviceName,[Parameter(Mandatory=$false)][string] $DeviceIP,[Parameter(Mandatory=$false)][string] $ApiToken
)
# Execute Backup on Fortigate Rest API
CreatePSCommandFile -DeviceName $DeviceName -DeviceIP $DeviceIP -ApiToken $ApiToken
$Output = Invoke-AzvmrunCommand -ResourceGroupName $rgName -VMName $ProxyServerName -CommandId 'RunPowerShellScript' -Scriptpath ".\InvokeCommand.ps1" -Parameter @{'api_url' = "10.29.255.212"; 'api_token' = "0p6h1rmspjf37kp80bc6ny88jw"}
($Output).Value.Message
}
Backup-Device -DeviceName "DeviceName" -DeviceIP '10.29.255.212' -ApiToken 'Api_Token'
解决方法
分享由一位幸运的同事提出的解决方案:)
使用New-SmbMapping,我们能够从Azure Automation PS脚本成功挂载存储帐户文件共享。
if (!(Test-Path `$MapDrive)) {
New-SmbMapping -LocalPath `$MapDrive -RemotePath `$RemotePath -UserName `$UserName -Password `$Key
}
Copy-Item .\Devicebackup.txt `$MapDrive