Azure自动化powershell runbook以获取CSV文件中的虚拟机详细信息,并将该CSV文件导出到Azure blob存储,有人能提供帮助吗

fnatzsnv  于 2023-03-19  发布在  Shell
关注(0)|答案(1)|浏览(120)

我正在运行Azure自动化PowerShell Runbook脚本以获取CSV文件中的虚拟机详细信息并将该CSV文件导出到Azure Blob存储,但我无法通过Azure自动化PowerShell Runbook脚本捕获虚拟机详细信息并将其保留在Blob存储中

Connect-AzAccount -Identity
$subscriptionId = "mySubID"
$reportName = "myReport.csv"
Select-AzSubscription $subscriptionId
$report = @()
$vms = Get-AzVM
$publicIps = Get-AzPublicIpAddress 
$nics = Get-AzNetworkInterface | ?{ $_.VirtualMachine -NE $null} 
foreach ($nic in $nics) { 
    $info = "" | Select VmName, ResourceGroupName, Region, VmSize, VirtualNetwork, Subnet, PrivateIpAddress, OsType, PublicIPAddress, NicName, ApplicationSecurityGroup 
    $vm = $vms | ? -Property Id -eq $nic.VirtualMachine.id 
    foreach($publicIp in $publicIps) { 
        if($nic.IpConfigurations.id -eq $publicIp.ipconfiguration.Id) {
            $info.PublicIPAddress = $publicIp.ipaddress
            } 
        } 
        $info.OsType = $vm.StorageProfile.OsDisk.OsType 
        $info.VMName = $vm.Name 
        $info.ResourceGroupName = $vm.ResourceGroupName 
        $info.Region = $vm.Location 
        $info.VmSize = $vm.HardwareProfile.VmSize
        $info.VirtualNetwork = $nic.IpConfigurations.subnet.Id.Split("/")[-3] 
        $info.Subnet = $nic.IpConfigurations.subnet.Id.Split("/")[-1] 
        $info.PrivateIpAddress = $nic.IpConfigurations.PrivateIpAddress 
        $info.NicName = $nic.Name 
        $info.ApplicationSecurityGroup = $nic.IpConfigurations.ApplicationSecurityGroups.Id 
        $report+=$info 
    } 
$report | ft VmName, ResourceGroupName, Region, VmSize, VirtualNetwork, Subnet, PrivateIpAddress, OsType, PublicIPAddress, NicName, ApplicationSecurityGroup 
$report | Export-CSV "AutomationFile123.csv"
$Context = New-AzureStorageContext -StorageAccountName "storagename" -StorageAccountKey "storagekey"
Set-AzureStorageBlobContent -Context $Context -Container "conttainername" -File "AutomationFile123.csv" -Blob "AutomationFile123.csv"
mkshixfv

mkshixfv1#

我在自己的环境中尝试,得到了以下结果:

最初,我尝试了相同的脚本,并得到了相同的错误,如保存没有Vm的细节。

控制台:

上述错误告诉我们,我们没有连接到我们的azure帐户。
我尝试在自动化帐户中使用**service principal login**,它工作得很完美。

命令:

$Appid = "<Your app id>"
$PWord = ConvertTo-SecureString -String "<App secret >" -AsPlainText -Force
$tenant = "tenant id"
$Credential = New-Object -TypeName "System.Management.Automation.PSCredential" -ArgumentList $Appid,$PWord
    # $Credential = Get-Credential
Connect-AzAccount -Credential $Credential -Tenant $tenant -ServicePrincipal -Subscription "<subsciption Id>"
$reportName = "myReport.csv"
$report = @()
$vms = Get-AzVM
$publicIps = Get-AzPublicIpAddress 
$nics = Get-AzNetworkInterface | ?{ $_.VirtualMachine -NE $null} 
foreach ($nic in $nics) { 
    $info = "" | Select VmName, ResourceGroupName, Region, VmSize, VirtualNetwork, Subnet, PrivateIpAddress, OsType, PublicIPAddress, NicName, ApplicationSecurityGroup 
    $vm = $vms | ? -Property Id -eq $nic.VirtualMachine.id 
    foreach($publicIp in $publicIps) { 
        if($nic.IpConfigurations.id -eq $publicIp.ipconfiguration.Id) {
            $info.PublicIPAddress = $publicIp.ipaddress
            } 
        } 
        $info.OsType = $vm.StorageProfile.OsDisk.OsType 
        $info.VMName = $vm.Name 
        $info.ResourceGroupName = $vm.ResourceGroupName 
        $info.Region = $vm.Location 
        $info.VmSize = $vm.HardwareProfile.VmSize
        $info.VirtualNetwork = $nic.IpConfigurations.subnet.Id.Split("/")[-3] 
        $info.Subnet = $nic.IpConfigurations.subnet.Id.Split("/")[-1] 
        $info.PrivateIpAddress = $nic.IpConfigurations.PrivateIpAddress 
        $info.NicName = $nic.Name 
        $info.ApplicationSecurityGroup = $nic.IpConfigurations.ApplicationSecurityGroups.Id 
        $report+=$info 
    } 
$report | ft VmName, ResourceGroupName, Region, VmSize, VirtualNetwork, Subnet, PrivateIpAddress, OsType, PublicIPAddress, NicName, ApplicationSecurityGroup 
$report | Export-CSV "AutomationFile123.csv"
$Context = New-AzureStorageContext -StorageAccountName "storage name" -StorageAccountKey "Account key"
$copytoblob=Set-AzureStorageBlobContent -Context $Context -Container "test" -File "AutomationFile123.csv" -Blob "AutomationFile123.csv"
$copytoblob

输出:

门户网站:

参考:azure - How to Connect-AzAccount in Powershell Core (without prompt)? - Stack Overflow

相关问题