azure automation powershell runbook to get VM details in CSV file and export that CSV file to azure blob storage, can some one help on this

huangapple go评论66阅读模式
英文:

azure automation powershell runbook to get VM details in CSV file and export that CSV file to azure blob storage, can some one help on this

问题

以下是翻译好的部分:

我正在运行Azure自动化PowerShell运行簿脚本以获取CSV文件中的VM详细信息并将该CSV文件导出到Azure Blob存储但我无法通过Azure自动化PowerShell运行簿脚本捕获VM详细信息并将其保存在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"

注意:这是提供的代码的翻译,不包括问题或其他信息。

英文:

I am running Azure automation PowerShell runbook script to get VM details in CSV file and export that CSV file to azure blob storage, but I am unable to capture the VM details through Azure automation PowerShell runbook script and keep it in blob storage

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"

答案1

得分: 1

我在我的环境中尝试并获得了以下结果:

最初,我尝试了相同的脚本,并获得了与没有Vm详细信息保存相同的错误。

控制台:
azure automation powershell runbook to get VM details in CSV file and export that CSV file to azure blob storage, can some one help on this

上述错误告诉我们我们没有与我们的Azure账户连接。

我尝试了使用**服务主体登录**在自动化帐户中,它完美地工作了。

命令:

$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 automation powershell runbook to get VM details in CSV file and export that CSV file to azure blob storage, can some one help on this

门户:
azure automation powershell runbook to get VM details in CSV file and export that CSV file to azure blob storage, can some one help on this

参考:
azure - 如何在PowerShell Core中进行Connect-AzAccount(无提示)? - Stack Overflow

英文:

I tried in my environment and got the below results:

Initially, I tried the same script and got the same error like saving without Vm details.

Console:
azure automation powershell runbook to get VM details in CSV file and export that CSV file to azure blob storage, can some one help on this

The above error tells us that we are not connecting with our azure account.

I tried with service principal login in the automation account it worked perfectly.

Command:

$Appid = &quot;&lt;Your app id&gt;&quot;
$PWord = ConvertTo-SecureString -String &quot;&lt;App secret &gt;&quot; -AsPlainText -Force
$tenant = &quot;tenant id&quot;
$Credential = New-Object -TypeName &quot;System.Management.Automation.PSCredential&quot; -ArgumentList $Appid,$PWord
    # $Credential = Get-Credential
Connect-AzAccount -Credential $Credential -Tenant $tenant -ServicePrincipal -Subscription &quot;&lt;subsciption Id&gt;&quot;
$reportName = &quot;myReport.csv&quot;
$report = @()
$vms = Get-AzVM
$publicIps = Get-AzPublicIpAddress 
$nics = Get-AzNetworkInterface | ?{ $_.VirtualMachine -NE $null} 
foreach ($nic in $nics) { 
    $info = &quot;&quot; | 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(&quot;/&quot;)[-3] 
        $info.Subnet = $nic.IpConfigurations.subnet.Id.Split(&quot;/&quot;)[-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 &quot;AutomationFile123.csv&quot;
$Context = New-AzureStorageContext -StorageAccountName &quot;storage name&quot; -StorageAccountKey &quot;Account key&quot;
$copytoblob=Set-AzureStorageBlobContent -Context $Context -Container &quot;test&quot; -File &quot;AutomationFile123.csv&quot; -Blob &quot;AutomationFile123.csv&quot;
$copytoblob

Output:
azure automation powershell runbook to get VM details in CSV file and export that CSV file to azure blob storage, can some one help on this

Portal:

azure automation powershell runbook to get VM details in CSV file and export that CSV file to azure blob storage, can some one help on this

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

huangapple
  • 本文由 发表于 2023年3月15日 20:19:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/75744604.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定