首页 > 解决方案 > 如何用“”包裹 & 以转义 & 符号

问题描述

这里提到:

The ampersand (&) character is not allowed. The & operator is reserved for future use; wrap an ampersand in double quotation marks ("&") to pass it as part of a string.

我们如何对这个字符串执行此操作?如果我们用“&”包裹它,那么它会将字符串分成两部分。

$saskey = "ssdsdsdsd&dsdsdwwwe1212" #auto-generated

这根本不符合逻辑:

$sasKey = $sasKey.replace("&",""&"")

“表达式或语句错误中出现意外的标记 '&'”是我们得到的

我想将它传递给 Azure 自动化中的一个函数,并且由于与号,我只得到第一部分“ssdsdsdsd”。这是自动化运行手册。

function MyFunc {
    [CmdletBinding()]
    param (
    [Parameter(Position=0)]
    [string]$sasKey
    )


    cd c:\upload
    $Env:AZCOPY_AUTO_LOGIN_TYPE = "MSI"
    
    $sourcePath = "c:\upload"
    $destPath="https://server.blob.core.windows.net/vmuploads" + $sasKey
    azcopy login --identity
    azcopy copy  $sourcePath $destPath --recursive
    
    #just to view the content in the VM
    $coreSitePath = "c:\upload\log.txt"
    $destPath | Out-File -FilePath $coreSitePath -Force 

}
...
...

    $context = (Get-AzStorageAccount -ResourceGroupName $storageRg -Name $storageAccountName).context
    $sasKey = New-AzStorageAccountSASToken -Context $context -Service Blob -ResourceType Service,Container,Object -Permission "racwdlup"

    $ScriptToRun = Get-Content Function:\MyFunc
    Out-File -InputObject $ScriptToRun -FilePath ScriptToRun.ps1

    # Run Func in VM
    $params = @{"sasKey" = $sasKey}
    
    $result=Invoke-AzVMRunCommand -ResourceGroupName $VMResourceGroupName -VMName $VMname -ScriptPath "ScriptToRun.ps1" -Parameter  $params  -CommandId 'RunPowerShellScript' # -Parameter $params

标签: powershell

解决方案


推荐阅读