我目前正在運行以下行將代理安裝到我的虛擬機
& "$env:ProgramFiles\AzureConnectedMachineAgent\azcmagent.exe" connect
當安裝順利通過它輸出以下
time="2022-06-08T09:26:41 03:00" level=info msg="Loading AgentConfig file from: C:\\ProgramData\\AzureConnectedMachineAgent\\Config\\agentconfig.json"
time="2022-06-08T09:26:41 03:00" level=info msg="Onboarding Machine. It usually takes a few minutes to complete. Sometimes it may take longer depending on network and server load status."
time="2022-06-08T09:26:41 03:00" level=info msg="Check network connectivity to all endpoints..."
time="2022-06-08T09:26:42 03:00" level=info msg="All endpoints are available... continue onboarding"
time="2022-06-08T09:26:48 03:00" level=info msg="Successfully Onboarded Resource to Azure" VM Id=0000-0000-xxxx-xxxx
現在我想創建一些陳述句(使用 IF),以便更好地處理錯誤。我可以將輸出保存到 $installation 等變數
$installation = & "$env:ProgramFiles\AzureConnectedMachineAgent\azcmagent.exe" connect
但我不知道如何使用 $installation 所以如果訊息輸出等于或包含“成功將資源加載到 Azure”就可以了,就像這樣(這不起作用)。
if($installation -contains "Successfully Onboarded Resource to Azure"){
Write-Output "Everything ok"
} else {
Write-Output "Installation failed"
}
我現在應該如何進行?我可以使用 IF 陳述句我們應該嘗試切換到可能的功能嗎?我還不是 Powershell 向導。
uj5u.com熱心網友回復:
操作員尋找一個精確的-Contains(盡管不區分大小寫)完整的字串匹配。
可行的是像這樣使用-like通配符運算子:
if ($installation -like '*Successfully Onboarded Resource to Azure*') {
Write-Host "Everything ok" -ForegroundColor Green
}
else {
Write-Host "Installation failed" -ForegroundColor Red
}
或正則運算式-match運算子:
if ($installation -match 'Successfully Onboarded Resource to Azure') {
Write-Host "Everything ok" -ForegroundColor Green
}
else {
Write-Host "Installation failed" -ForegroundColor Red
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/487514.html
標籤:电源外壳
上一篇:獲取未壓縮的zip大小
下一篇:Powershell簡單腳本
