【发布时间】:2019-01-01 02:07:51
【问题描述】:
所以我完全被这个难住了。
问题
我有一个 powershell 脚本(一个 .ps1 文件),它有时会执行以下操作:
Import-Module AzureRM
Login-AzureRmAccount -Credential $azureCredentials | out-null
Select-AzureRmSubscription -SubscriptionId $azureSubscriptionId -TenantId $azureTenantId | out-null
Write-Host (ConvertTo-Json((Get-AzureRmResource -Tag @{ "env"="dev"}) | Select Name, Location))
我通过执行以下操作在 .NET 控制台应用程序中运行脚本:
Dim process = New Process() With {.StartInfo = New ProcessStartInfo With {
.FileName = "powershell.exe",
.Arguments = "-ExecutionPolicy ByPass -file """ & _powershellScriptsPath & "\get-webapps.ps1",
.UseShellExecute = False,
.RedirectStandardOutput = True,
.RedirectStandardError = True
}}
脚本在我的笔记本电脑上运行良好,无论是从 ISE 还是从控制台应用程序运行。
然后在我的桌面上,它在 ISE 中运行良好,但在控制台应用程序中失败,并显示以下错误消息:
Get-AzureRmResource : A parameter cannot be found that matches parameter name 'Tag'.
我想不通。
一些见解
我的$PSVersionTable 在两台电脑上都是一样的:
Name Value
---- -----
PSVersion 5.1.17134.165
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.17134.165
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
我的笔记本电脑有 6.1.0 版的 AzureRm 模块:
PS C:\WINDOWS\system32> Get-InstalledModule -Name AzureRm
Version Name Repository Description
------- ---- ---------- -----------
6.1.0 AzureRM PSGallery Azure Resource Manager Module
虽然我的桌面版本为 6.5.0:
PS C:\WINDOWS\system32> Get-InstalledModule -Name AzureRm
Version Name Repository Description
------- ---- ---------- -----------
6.5.0 AzureRM PSGallery Azure Resource Manager Module
所以这里有不同的版本,但 -Tag 确实存在于 6.1.0 中,因为在 Powershell ISE 中脚本确实有效。而且它必须使用 6.1.0,因为它似乎是唯一安装的版本(?)。
此外,控制台应用程序在两个 Visual Studio(笔记本电脑/台式机)中运行相同的配置(至少据我所知)。值得注意的是,它编译为 AnyCPU 并在两台计算机上以 32 位运行。
【问题讨论】:
-
是什么阻止您将 azurerm 更新到 6.5.0 只是为了确定?此外,您可以将 get-command get-azurermresource -full 添加到控制台应用程序并检查输出
-
您的命令指出了我认为的问题 - 从 ISE 运行时显示为 6.2.0(当脚本工作时),但从控制台应用程序运行时显示为 5.5.2(当失败时)。我假设 -Tag 参数在 5.5.2 中不存在。现在的问题是 - 它从哪里获得 5.5.2 以及为什么?
标签: .net visual-studio azure powershell azure-powershell