powershell - 如何用“”包裹 & 以转义 & 符号
问题描述
这里提到:
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
解决方案
推荐阅读
- python - 基于 2d 数组填充 3d 数组的有效方法是什么?
- ffmpeg - 如何从视频流中删除 PES 标头
- javascript - Vuetify 项目组如何预选项目?
- javascript - 获取请求 -> 错误:SyntaxError: Unexpected token < in JSON at position 4
- c - linux内核中的ptrace在哪里?
- excel - 索引匹配中的嵌套 IF 以查找值
- sql - 在加入非常大的表进行优化之前,我是否需要过滤所有子查询
- python - 如何通过 IF 语句简化变量赋值
- javascript - 如何在尊重代码顺序的情况下执行 javascript 代码
- swift - Xcode11 中的 Kingfisher 问题 - 找不到目标“armv7-apple-ios”的模块“Kingfisher”;找到:arm64,arm64-apple-ios