Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/azure/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell 登录AzurerAccount是一个对象还是我需要将其存储在一个对象中?_Powershell_Azure - Fatal编程技术网

Powershell 登录AzurerAccount是一个对象还是我需要将其存储在一个对象中?

Powershell 登录AzurerAccount是一个对象还是我需要将其存储在一个对象中?,powershell,azure,Powershell,Azure,我有一个关于登录AzurerAccount cmdlet的问题,我正试着去想它。当我运行Login AzurerAccount并使用凭据进行身份验证时,凭据对象是否存储在任何位置?如果我正在编写一个包含大量Azure cmdlet的脚本,我是否需要保留这些凭据或以某种方式存储这些凭据以传递到其他cmdlet,或者我是否需要获得某种身份验证令牌,该令牌将保留一段时间?我对此做了一些研究,如果我的谷歌fu在这方面做得不够好,我向你道歉。谢谢 登录AzurerAccount没有任何需要保留的对象。一旦

我有一个关于登录AzurerAccount cmdlet的问题,我正试着去想它。当我运行Login AzurerAccount并使用凭据进行身份验证时,凭据对象是否存储在任何位置?如果我正在编写一个包含大量Azure cmdlet的脚本,我是否需要保留这些凭据或以某种方式存储这些凭据以传递到其他cmdlet,或者我是否需要获得某种身份验证令牌,该令牌将保留一段时间?我对此做了一些研究,如果我的谷歌fu在这方面做得不够好,我向你道歉。谢谢

登录AzurerAccount
没有任何需要保留的对象。一旦您调用它,它将设置其他调用将使用的环境上下文。您可以修改此上下文,例如,通过使用
Set-AzureRmContext
commandlet更改命令将与哪个订阅和tenent通信

我唯一需要使用的“上下文对象”是从
新AzureStorageContext
返回的对象,该对象将由使用环境上下文的
Get-AzureStorageKey
调用生成。该上下文对象将与来自
Azure.Storage
模块的commandlet一起使用,并将与
-context
参数一起传入

下面是一个脚本,我用来创建一个新的存储容器,它显示了abient上下文和正在使用的存储容器上下文

$resourceGroupName = "MyResourceGroup"
$storageAccountName ="examplestorageaccount"
$storageContainerName = "zips"
$resourceGroupLocation = "centralus"


#******************************************************************************
# Script body
# Execution begins here
#******************************************************************************
$ErrorActionPreference = "Stop"

# sign in
Write-Host "Logging in...";
$null = Login-AzureRmAccount;

# select subscription
$subscriptionId = ( Get-AzureRmSubscription | Out-GridView -Title “Select an Azure Subscription …” -PassThru).SubscriptionId

Set-AzureRmContext -SubscriptionId $subscriptionId

# Register RPs
$resourceProviders = @("microsoft.storage");
if($resourceProviders.length) {
    Write-Host "Registering resource providers"
    foreach($resourceProvider in $resourceProviders) {
        $null = Register-AzureRmResourceProvider -ProviderNamespace $resourceProvider;
    }
}

#Create or check for existing resource group
$resourceGroup = Get-AzureRmResourceGroup -Name $resourceGroupName -ErrorAction SilentlyContinue
if(!$resourceGroup)
{
    Write-Host "Creating resource group '$resourceGroupName' in location '$resourceGroupLocation'";
    New-AzureRmResourceGroup -Name $resourceGroupName -Location $resourceGroupLocation
}
else{
    Write-Host "Using existing resource group '$resourceGroupName'";
}

#Create or check for existing storage account
$storageAccount = Get-AzureRmStorageAccount -Name $storageAccountName -ResourceGroup $resourceGroupName -ErrorAction SilentlyContinue
if(!$storageAccount)
{
    Write-Host "Account not found, creating new storage account $storageAccountName";
    $storageAccount = New-AzureRmStorageAccount -Location $resourceGroupLocation -Name $storageAccountName -ResourceGroupName $resourceGroupName -SkuName Standard_LRS -Kind Storage
}
else
{
    Write-Host "Using existing storage account $storageAccountName'";
}

$storageAccountKey0 = (Get-AzureRmStorageAccountKey -Name $storageAccountName -ResourceGroupName $resourceGroupName).Value[0]
$storageAccountContext = New-AzureStorageContext -StorageAccountName $storageAccountName -StorageAccountKey $storageAccountKey0

#Create or check for existing storage container
$storageContainer = Get-AzureStorageContainer -Name $storageContainerName -Context $storageAccountContext -ErrorAction SilentlyContinue
if (!$storageContainer)
{
    Write-Host "Container not foind, creating new storage container $storageContainerName"
    $storageContainer = New-AzureStorageContainer -Name $storageContainerName -Permission Blob -Context $storageAccountContext
}
else
{
    Write-Host "Using existing storage container $storageContainerName'";
}

Write-Host "------------------------------------------------"
Write-Host "New defaults for WriteToStorageAccount.ps1"
Write-Host '$storageAccountName =' "'$storageAccountName'"
Write-Host '$storageContainerName =' "'$storageContainerName'"
Write-Host '$key =' "'$storageAccountKey0'"

登录AzureRmAccount
没有任何需要保留的对象。一旦您调用它,它将设置其他调用将使用的环境上下文。您可以修改此上下文,例如,通过使用
Set-AzureRmContext
commandlet更改命令将与哪个订阅和tenent通信

我唯一需要使用的“上下文对象”是从
新AzureStorageContext
返回的对象,该对象将由使用环境上下文的
Get-AzureStorageKey
调用生成。该上下文对象将与来自
Azure.Storage
模块的commandlet一起使用,并将与
-context
参数一起传入

下面是一个脚本,我用来创建一个新的存储容器,它显示了abient上下文和正在使用的存储容器上下文

$resourceGroupName = "MyResourceGroup"
$storageAccountName ="examplestorageaccount"
$storageContainerName = "zips"
$resourceGroupLocation = "centralus"


#******************************************************************************
# Script body
# Execution begins here
#******************************************************************************
$ErrorActionPreference = "Stop"

# sign in
Write-Host "Logging in...";
$null = Login-AzureRmAccount;

# select subscription
$subscriptionId = ( Get-AzureRmSubscription | Out-GridView -Title “Select an Azure Subscription …” -PassThru).SubscriptionId

Set-AzureRmContext -SubscriptionId $subscriptionId

# Register RPs
$resourceProviders = @("microsoft.storage");
if($resourceProviders.length) {
    Write-Host "Registering resource providers"
    foreach($resourceProvider in $resourceProviders) {
        $null = Register-AzureRmResourceProvider -ProviderNamespace $resourceProvider;
    }
}

#Create or check for existing resource group
$resourceGroup = Get-AzureRmResourceGroup -Name $resourceGroupName -ErrorAction SilentlyContinue
if(!$resourceGroup)
{
    Write-Host "Creating resource group '$resourceGroupName' in location '$resourceGroupLocation'";
    New-AzureRmResourceGroup -Name $resourceGroupName -Location $resourceGroupLocation
}
else{
    Write-Host "Using existing resource group '$resourceGroupName'";
}

#Create or check for existing storage account
$storageAccount = Get-AzureRmStorageAccount -Name $storageAccountName -ResourceGroup $resourceGroupName -ErrorAction SilentlyContinue
if(!$storageAccount)
{
    Write-Host "Account not found, creating new storage account $storageAccountName";
    $storageAccount = New-AzureRmStorageAccount -Location $resourceGroupLocation -Name $storageAccountName -ResourceGroupName $resourceGroupName -SkuName Standard_LRS -Kind Storage
}
else
{
    Write-Host "Using existing storage account $storageAccountName'";
}

$storageAccountKey0 = (Get-AzureRmStorageAccountKey -Name $storageAccountName -ResourceGroupName $resourceGroupName).Value[0]
$storageAccountContext = New-AzureStorageContext -StorageAccountName $storageAccountName -StorageAccountKey $storageAccountKey0

#Create or check for existing storage container
$storageContainer = Get-AzureStorageContainer -Name $storageContainerName -Context $storageAccountContext -ErrorAction SilentlyContinue
if (!$storageContainer)
{
    Write-Host "Container not foind, creating new storage container $storageContainerName"
    $storageContainer = New-AzureStorageContainer -Name $storageContainerName -Permission Blob -Context $storageAccountContext
}
else
{
    Write-Host "Using existing storage container $storageContainerName'";
}

Write-Host "------------------------------------------------"
Write-Host "New defaults for WriteToStorageAccount.ps1"
Write-Host '$storageAccountName =' "'$storageAccountName'"
Write-Host '$storageContainerName =' "'$storageContainerName'"
Write-Host '$key =' "'$storageAccountKey0'"

登录AzurerAccount返回一个
AzureContextContainer
对象。您可以将其存储并在以后与
设置AzureRmContext
保存AzureRmContext
一起使用。该对象主要可以被脚本捕获和使用,并向shell窗口显示相关信息


请注意,当前为每个PowerShell窗口创建了一个新的上下文,有cmdlet允许您保存(
save AzureRmContext
)和导入(
import AzureRmContext
)上下文,以便您可以将此类命令添加到用户配置文件中,并始终从相同的上下文开始。这将在夏季改变,因为一项新功能将允许一组上下文在PowerShell会话中自动保持。

登录AzurerAccount返回一个
AzureContextContainer
对象。您可以将其存储并在以后与
设置AzureRmContext
保存AzureRmContext
一起使用。该对象主要可以被脚本捕获和使用,并向shell窗口显示相关信息


请注意,当前为每个PowerShell窗口创建了一个新的上下文,有cmdlet允许您保存(
save AzureRmContext
)和导入(
import AzureRmContext
)上下文,以便您可以将此类命令添加到用户配置文件中,并始终从相同的上下文开始。这将在今年夏天改变,因为一项新功能将允许一组上下文在PowerShell会话中自动持久化。

实际上,现在您可以使用上下文持久化,这就没那么有用了。如果运行
启用AzureRmContextAutosave
,则PowerShell会将此上下文容器存储在“%AppData%\Roaming\Windows Azure PowerShell”中,这样下次打开PowerShell窗口时就不需要再次登录了。

实际上,既然可以使用上下文持久性,这就没那么有用了。如果运行
启用AzureRmContextAutosave
,则PowerShell将此上下文容器存储在“%AppData%\Roaming\Windows Azure PowerShell”中,以便下次打开PowerShell窗口时无需再次登录。

这是真的,您可以将容器对象导出到文件中。如果您希望稍后导入上下文/凭据,以避免脚本提示您输入实时凭据,这将非常有用。。。PS C:\>保存AzureRmContext-Profile(添加AzureRmAccount)-Path C:\test.json然后PS C:\>导入AzureRmContext-Path C:\test.json这是真的,您可以将容器对象导出到文件中。如果您希望稍后导入上下文/凭据,以避免脚本提示您输入实时凭据,这将非常有用。。。PS C:\>保存AzureRmContext-Profile(添加AzureRmAccount)-路径C:\test.json然后PS C:\>导入AzureRmContext-路径C:\test.json