英文:
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账户连接。
我尝试了使用**服务主体登录
**在自动化帐户中,它完美地工作了。
命令:
$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 - 如何在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:
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 = "<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
Output:
Portal:
Reference:
azure - How to Connect-AzAccount in Powershell Core (without prompt)? - Stack Overflow
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论