gpt4 book ai didi

azure Automation powershell runbook,用于获取 CSV 文件中的 VM 详细信息并将该 CSV 文件导出到 azure blob 存储,有人可以帮忙吗?

转载 作者:行者123 更新时间:2023-12-03 06:23:11 25 4
gpt4 key购买 nike

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

最佳答案

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

最初,我尝试了相同的脚本并得到了相同的错误,就像在没有虚拟机详细信息的情况下保存一样。

控制台: enter image description here

上述错误告诉我们,我们没有与我们的 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

输出: enter image description here

门户:

enter image description here

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

关于azure Automation powershell runbook,用于获取 CSV 文件中的 VM 详细信息并将该 CSV 文件导出到 azure blob 存储,有人可以帮忙吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75744604/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com