程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了无法在 Azure DevOps 中运行 Powershell 脚本大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决无法在 Azure DevOps 中运行 PowersHell 脚本?

开发过程中遇到无法在 Azure DevOps 中运行 PowersHell 脚本的问题如何解决?下面主要结合日常开发的经验,给出你关于无法在 Azure DevOps 中运行 PowersHell 脚本的解决方法建议,希望对你解决无法在 Azure DevOps 中运行 PowersHell 脚本有所启发或帮助;

我有一个脚本,它创建连接到 VM 的磁盘的快照,然后复制到另一个区域并从中创建一个磁盘,这作为内联脚本工作,但是当我在 Azure DevOps 中作为 Azure PowersHell 脚本运行它时从回购,它失败

At D:\a\r1\a\_repo\drop\snapshot-copy.ps1:108 char:38
+         } until ($status -eq "success")
+                                      ~~
The String is missing the terminator: ".

At D:\a\r1\a\_repo\drop\snapshot-copy.ps1:59 char:35
+ foreach ($snapshot in $snapshots) {
+                                   ~
Missing closing '}' in statement block or type deFinition.
PowerSHell exited with code '1'.

脚本有两部分,首先创建有效的快照(下)

$resourceGroupname = 'sourcerg' 

$LOCATIOn = 'Westeurope' 

$ManageddiskresourceGroup = "dstdisk-rg"

$dstLOCATIOn = "north Europe"
$storageaccountdr = "strgdestvhd2021"
$storageaccountdrblob = "vhd"
$SnapshotresourceGroup = "desTination-rg"

$vm = Get-AzVM -resourceGroupname $resourceGroupname

foreach ($v in $vm) {

    $snapshot =  New-AzSnapshotConfig `
    -sourceUri $v.StorageProfile.osdisk.Manageddisk.ID `
    -LOCATIOn $LOCATIOn `
    -CreateOption copy -Verbose

    $ss = $v.name 
    $ssname = "$ss-osdisk"

    New-AzSnapshot `
    -Snapshot $snapshot `
    -Snapshotname $ssname `
    -resourceGroupname $resourceGroupname -Verbose

     $datadisks = ($v.StorageProfile.Datadisks).name
 
     foreach ($datadisk in $datadisks) {
 
         $datadisk = Get-Azdisk `
         -resourceGroupname $resourceGroupname `
         -diskname $datadisk
 
         Write-Output "VM $($vm.Name) data disk $($datadisk.Name) Snapshot Begin"
 
         $DatadiskSnapshotConfig = New-AzSnapshotConfig `
         -sourceUri $datadisk.ID `
         -CreateOption copy `
         -LOCATIOn $LOCATIOn
          $snapshotnameData = "$($datadisk.Name)_snapshot_$(Get-Date -Format ddMMyy)"
 
         New-AzSnapshot `
         -resourceGroupname $resourceGroupname `
         -Snapshotname $snapshotnameData `
         -Snapshot $DatadiskSnapshotConfig `
         -ErrOraction Stop
          
         Write-Output "VM $($vm.Name) data disk $($datadisk.Name) Snapshot End"   
        }
 
 }

以下部分失败

$storageaccountdrkey = (Get-AzStorageAccountKey `
-name $storageaccountdr `
-resourceGroupname $snapshotresourcegroup).value[0]
$snapshots = Get-AzSnapshot `
-resourceGroupname $resourceGroupname | ?{($_.TimeCreated) -gt ([datetiR_475_11845@e]::UtcNow.Addhours(-12))}
 
foreach ($snapshot in $snapshots) {
  
        Write-Output "GranTing $($snapshot.Name) access"
        $snapshotaccess = Grant-AzSnapshotAccess `
        -resourceGroupname $resourceGroupname `
        -Snapshotname $snapshot.name -DurationInSecond 1800 `
        -Access Read -ErrOraction stop -Verbose
        
        Write-Output "$($snapshot.Name) access granted"
         
        $DestStorageContext = New-AzStorageContext `
        –StorageAccountname $storageaccountdr `
        -StorageAccountKey $storageaccountdrkey `
        -ErrOraction stop
        
        $vhdname = $($snapshot.Name).SubString(0,$($snapshot.Name).Length-9)
 
        Write-Output "Begin snapshot: ($($snapshot.Name)) copy to $vhdname.vhd"
        Start-AzStorageBlobcopy `
        -absoluteUri $snapshotaccess.AccessSAS `
        -DestContainer $storageaccountdrblob `
        -DestContext $DestStorageContext `
        -DestBlob "$($vhdName).vhd" `
        -Force -ErrOraction stop
        
        Write-Output "snapshot: ($($snapshot.Name)) copy to $vhdname.vhd completed" 
       
        $storageaccountID = Get-AzStorageAccount `
        -name $storageaccountdr `
        -resourceGroupname $snapshotresourcegroup

        $diskuri = "https://$($storageaccountdr).blob.core.windows.net/$($storageaccountdrblob)/$($vhdName).vhd"
        $storageType = 'Premium_lrs'
        
        Write-Output "CreaTing managed disk: $vhdname"
        
        $diskConfig = New-AzdiskConfig `
        -AccountType $storageType `
        -LOCATIOn $dstLOCATIOn `
        -CreateOption import `
        -StorageAccountID $storageaccountID.ID `
        -sourceUri $diskuri `
        -OsType windows `
        -Verbose
       
        $time = Get-Date

        do 
        {
            $status = (Get-AzStorageBlobcopyState `
            -Blob "$vhdname.vhd" `
            -Container "vhd-cont" `
            -Context $DestStorageContext).Status 
            
            sleep -Seconds 120
        } until ($status -eq "success")
        
        New-Azdisk -disk $diskConfig `
        -resourceGroupname $ManageddiskresourceGroup `
        -diskname $vhdname `
        -ErrOraction stop -Verbose
        
        Write-Output "managed disk: $vhdname created"
            
        } 

我看不出我做错了什么,因为它作为内联脚本工作,这个问题似乎与

    sleep -Seconds 120
} until ($status -eq "success")

谁能看出我做错了什么?我在 Azure DevOps 中使用最新版本的 PowersHell。

提前致谢

编辑:

内联脚本部署失败,错误:

TFs.WebAPI.Exception: 503: error
    at g (https://cdn.vsassets.io/bundles/vss-bundle-common-vKfxZuj32xwrvhB4ZfuMmqcJoxJ1nxtCCBRlgV94N2ZY=:79:623)
    at https://cdn.vsassets.io/bundles/vss-bundle-common-vKfxZuj32xwrvhB4ZfuMmqcJoxJ1nxtCCBRlgV94N2ZY=:79:3718
    at nt (https://cdn.vsassets.io/bundles/vss-bundle-common-vKfxZuj32xwrvhB4ZfuMmqcJoxJ1nxtCCBRlgV94N2ZY=:79:887)
    at https://cdn.vsassets.io/bundles/vss-bundle-common-vKfxZuj32xwrvhB4ZfuMmqcJoxJ1nxtCCBRlgV94N2ZY=:79:3650
    at l (https://cdn.vsassets.io/bundles/vss-bundle-common-vKfxZuj32xwrvhB4ZfuMmqcJoxJ1nxtCCBRlgV94N2ZY=:33:8126)
    at https://cdn.vsassets.io/bundles/vss-bundle-common-vKfxZuj32xwrvhB4ZfuMmqcJoxJ1nxtCCBRlgV94N2ZY=:33:8352
    at t.when (https://cdn.vsassets.io/bundles/vss-bundle-common-vKfxZuj32xwrvhB4ZfuMmqcJoxJ1nxtCCBRlgV94N2ZY=:33:3784)
    at t.u.promisedispatch (https://cdn.vsassets.io/bundles/vss-bundle-common-vKfxZuj32xwrvhB4ZfuMmqcJoxJ1nxtCCBRlgV94N2ZY=:33:2828)
    at https://cdn.vsassets.io/bundles/vss-bundle-common-vKfxZuj32xwrvhB4ZfuMmqcJoxJ1nxtCCBRlgV94N2ZY=:33:1653
    at messagePort.t (https://cdn.vsassets.io/bundles/vss-bundle-common-vKfxZuj32xwrvhB4ZfuMmqcJoxJ1nxtCCBRlgV94N2ZY=:33:5777)

有什么想法吗? :(

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

大佬总结

以上是大佬教程为你收集整理的无法在 Azure DevOps 中运行 Powershell 脚本全部内容,希望文章能够帮你解决无法在 Azure DevOps 中运行 Powershell 脚本所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。