按顺序运行命令

时间:2018-06-06 07:51:47

标签: powershell

我正在尝试按顺序运行powershell。我有以下片段。

$TestdevlandingStorageAccount = Get-AzureRmStorageAccount -ResourceGroupName "Test_Storage" -AccountName "Testdevlanding";
$TestuatlandingStorageAccount = Get-AzureRmStorageAccount -ResourceGroupName "Test_Storage" -AccountName "Testuatlanding";

Set-AzureRmKeyVaultAccessPolicy -VaultName "MyOrgTest" -ObjectId $TestdevlandingStorageAccount.Identity.PrincipalId -PermissionsToKeys wrapkey,unwrapkey,get;
Set-AzureRmKeyVaultAccessPolicy -VaultName "MyOrgTest" -ObjectId $TestuatlandingStorageAccount.Identity.PrincipalId -PermissionsToKeys wrapkey,unwrapkey,get;

我在执行上述命令时遇到错误(第一Set命令成功运行):

Set-AzureRmKeyVaultAccessPolicy : Cannot validate argument on parameter 'ObjectId'. The argument is null or empty. Provide an argument that is not null or empty, and then try the 
command again.
At C:SomePath\PowerShellStorageEncryption.ps1:27 char:71
+ ... " -ObjectId $TestprodpublishedStorageAccount.Identity.PrincipalId -P ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Set-AzureRmKeyVaultAccessPolicy], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.Azure.Commands.KeyVault.SetAzureKeyVaultAccessPolicy

如果我只运行一个组操作,那么它将执行正常:

$TestdevlandingStorageAccount = Get-AzureRmStorageAccount -ResourceGroupName "Test_Storage" -AccountName "Testdevlanding";
Set-AzureRmKeyVaultAccessPolicy -VaultName "HoneywellTest" -ObjectId $TestdevlandingStorageAccount.Identity.PrincipalId -PermissionsToKeys wrapkey,unwrapkey,get;

我尝试在Google上搜索如何按顺序运行powershell命令,大多数答案建议使用我现在使用的;而没有任何运气。

有人可以建议我可能遗失的内容吗?

非常感谢提前。

3 个答案:

答案 0 :(得分:0)

  

无法验证参数' ObjectId'的参数。参数为null或空。提供一个非null或空的参数,然后尝试   命令再次。

表示export class Address { constructor(data: object) { super(); Object.assign(this, data); } ... } 为空,因此原始PrincipalId可能找不到存储帐户。但也要确保你不会有错字。

  

Get-AzureRmStorageAccount

您姓名中的-ObjectId $aTestuatpublishedStorageAccount.Identity.PrincipalId并不反映您使用的其他变量名称。

尝试:

$[a]TestuatpublishedStorageAccount

要验证您实际上是否正在获取PrincipalId,您可以运行以下命令:

$TestdevlandingStorageAccount = (Get-AzureRmStorageAccount -ResourceGroupName "Test_Storage" -AccountName "Testdevlanding").Identity.PrincipalId;
$TestuatlandingStorageAccount = (Get-AzureRmStorageAccount -ResourceGroupName "Test_Storage" -AccountName "Testuatlanding").Identity.PrincipalId;

Set-AzureRmKeyVaultAccessPolicy -VaultName "HoneywellTest" -ObjectId $TestdevlandingStorageAccount -PermissionsToKeys wrapkey,unwrapkey,get;
Set-AzureRmKeyVaultAccessPolicy -VaultName "HoneywellTest" -ObjectId $TestuatlandingStorageAccount -PermissionsToKeys wrapkey,unwrapkey,get;

答案 1 :(得分:0)

我注意到errormessage显示的变量名称不正确:

-ObjectId $aTestuatpublishedStorageAccount.Identity.PrincipalId

在您的代码段中,您使用$TestuatlandingStorageAccount

您是否第一次运行代码时输错了? 我认为伯尼怀特有正确的答案。

答案 2 :(得分:0)

我发现了这个问题,

并非所有存储帐户都分配了身份。我使用下面的命令为我的所有存储帐户分配了身份,然后我可以成功运行命令。

Set-AzureRmStorageAccount -ResourceGroupName "MyResourceGroup" -AccountName "mystorageaccount" -AssignIdentity

真的很抱歉!我只是假设因序列而存在问题。我尝试删除这个问题,但不能。

非常感谢回答我问题的所有人。

相关问题