如何防止 Powershell 在输出上打印警告?

时间:2021-02-19 13:21:49

标签: powershell

我正在执行 PS 脚本以显示 Azure 中的一些内容。但是,对于某些 cmdlet,警告会写入输出。

像这样:

WARNING: AzureRM.Profile already loaded. Az and AzureRM modules cannot be imported in the same session or used in the same script or runbook. If you are running PowerShell i n an environment you control you can use the 'Uninstall-AzureRm' cmdlet to remove all AzureRm modules from your machine. If you are running in Azure Automation, take care th at none of your runbooks import both Az and AzureRM modules. More information can be found here: https://aka.ms/azps-migration-guide.

我使用了 -ErrorAction SilentlyContinue 来防止打印错误。但是有没有办法在执行命令时避免输出中出现警告。

P.S:我很清楚如何摆脱上述警告。只是好奇是否有任何方法可以防止显示警告。

1 个答案:

答案 0 :(得分:3)

您正在尝试对警告使用 -ErrorAction,这不是错误。

要使警告静音,您需要使用 -WarningAction SilentlyContinue