I’m try to get a Progress bar for a foreach loop. It starts over when last foreach name in variable $nic. I have atached both the section of the code that is not working and the complete script.
Code Block that is not working
$vminfo = @()foreach ($nic in $nics) {
$progressCount = 0
for($i = 0; $i -le $Users.Count; $i++) {
# Just writes status Write-Progress -Id 0 -Activity "Collecting VM info" -Status "$progressCount of $($nics.Count)" -PercentComplete (($progressCount / $nics.Count) * 100) $vminfo += ($nic)[$i] | Select-Object -Property *
$progressCount++
$rg = $vm.ResourceGroupName $size = $vm.HardwareProfile.VmSize ##### Custom Object##### $nicint = Get-AzNetworkInterface -name $nic.name $vm = $vms | ? -Property Id -eq $nic.VirtualMachine.id $vminfo += ( [pscustomobject][ordered] @{ VMName = $vm.Name VmSize = $vm.HardwareProfile.VmSize "CPU(S)" = ($vmsizelist | ? {$_.name -eq $size}).numberOfCores Memory = ($vmsizelist | ? {$_.name -eq $size}).memoryinmb OsType = $vm.StorageProfile.OsDisk.OsType ResourceGroupName = $vm.ResourceGroupName Region = $vm.Location NicName = $nic.Name -join ", " PrivateIpAddress = $nic.IpConfigurations.PrivateIpAddress VirtualNetwork = $nic.IpConfigurations.subnet.Id.Split("/")[-3] Subnet = $nic.IpConfigurations.subnet.Id.Split("/")[-1] NSG = $nicint.NetworkSecurityGroup.Id.Split("/")[-1] -join ", " })}} $vminfo | Export-Excel -WorksheetName VMINFO -path $path\$reportName</pre>
$nsgnamesout = (Get-AzNetworkSecurityGroup).name | out-file $rpttemp\nsgnames.txt$nsgnameins = get-content -Path C:\temp\temp\nsgnames.txt
<#
Version 1
VMName | VmSize | CPU(S) | Memory | DiskName | Disk Size | OsType | ResourceGroupName | Region | NicName | PrivateIpAddress | VirtualNetwork | Subnet
Version 2
Exports all NSG
Version 3
Split VMinfo, Disk, and NSG into Different tabs
#>
$date = get-date -Format MM-dd-yy
$customername = read-host -Prompt “Enter Customer Name”
$reportName = “$customername-VMinfo-$date.xls”
$path = “c:\temp”
$vms = Get-AzVM
$rg = $vms.ResourceGroupName
$nics = Get-AzNetworkInterface | ? { $_.VirtualMachine -NE $null}
Get all the VM CPU and Memory
$vmsizelist = Get-AzVMSize -Location ‘EASTUS’
($vmsizelist | ? {$_.name -eq $size}).memoryinmb
$rpttemp = "c:\temp\rpttemp"
if (Test-Path $path$reportName) {
Remove-Item -force $path\$reportName}
if (Test-Path $path$rpttemp) {
write-host “nsg name folder Path Found”}
else { (Test-Path $path$reporttmp)
write-host Creating nsgname folder
New-Item -ItemType “directory” -Path $rpttemp}
$vminfo = @()
foreach ($nic in $nics) {
$progressCount = 0
for($i = 0; $i -le $Users.Count; $i++) {
# Just writes status Write-Progress -Id 0 -Activity "Collecting VM info" -Status "$progressCount of $($nics.Count)" -PercentComplete (($progressCount / $nics.Count) * 100) $vminfo += ($nic)[$i] | Select-Object -Property *
$progressCount++
$rg = $vm.ResourceGroupName $size = $vm.HardwareProfile.VmSize ##### Custom Object##### $nicint = Get-AzNetworkInterface -name $nic.name $vm = $vms | ? -Property Id -eq $nic.VirtualMachine.id $vminfo += ( [pscustomobject][ordered] @{ VMName = $vm.Name VmSize = $vm.HardwareProfile.VmSize "CPU(S)" = ($vmsizelist | ? {$_.name -eq $size}).numberOfCores Memory = ($vmsizelist | ? {$_.name -eq $size}).memoryinmb OsType = $vm.StorageProfile.OsDisk.OsType ResourceGroupName = $vm.ResourceGroupName Region = $vm.Location NicName = $nic.Name -join ", " PrivateIpAddress = $nic.IpConfigurations.PrivateIpAddress VirtualNetwork = $nic.IpConfigurations.subnet.Id.Split("/")[-3] Subnet = $nic.IpConfigurations.subnet.Id.Split("/")[-1] NSG = $nicint.NetworkSecurityGroup.Id.Split("/")[-1] -join ", " })}} $vminfo | Export-Excel -WorksheetName VMINFO -path $path\$reportName
<#
Get All Disk
#>
$vmdiskinfo = @()
foreach ($nic in $nics) {
$rg = $vm.ResourceGroupName
$size = $vm.HardwareProfile.VmSize
$osdisk =((get-azvm -resourcegroupname $rg -name $vm.Name).StorageProfile).OsDisk.name
$osdisksize = ((get-azvm -resourcegroupname $rg -name $vm.Name).StorageProfile).OSDisk.DiskSizeGB
$datadiskname = ((get-azvm -resourcegroupname $rg -name $vm.Name).StorageProfile).DataDisks.Name -join ", "
$datadisksize = ((get-azvm -resourcegroupname $rg -name $vm.Name).StorageProfile).DataDisks.DiskSizeGb -join ", "
$vm = $vms | ? -Property Id -eq $nic.VirtualMachine.id
$vmdiskinfo += ( [pscustomobject][ordered] @{ VMName = $vm.Name OSDisk = $osdisk OSDiskSize = $osdisksize DataDisk = $datadiskname DataDiskSize = $datadisksize } ) }
$vmdiskinfo | Export-Excel -WorksheetName DISKS -path $path$reportName
<#
Get All NSG
#>
$nsgconfigs = @()
$nsgnamesout = (Get-AzNetworkSecurityGroup).name | out-file $rpttemp\nsgnames.txt
$nsgnameins = get-content -Path C:\temp\temp\nsgnames.txt
Foreach ($nsgsg in $nsgnameins) {
$nsgconfigs += ( Get-AzNetworkSecurityGroup -Name $nsgsg | Get-AzNetworkSecurityRuleConfig )
}
$NsgRuleSet = @()
foreach ($nsg in $nsgconfigs) {
$ASGGroupNameSource = $nsg.SourceApplicationSecurityGroups.id -replace ‘.*/’
$ASGGroupNameDestination = $nsg.DestinationApplicationSecurityGroups.id -replace ‘.*/’
$nsgsid = $nsg.id
$nsgsplit = $nsgsid.split(“/”)[-3]
$NsgRuleSet += (
[pscustomobject]@{ NSGNAME = $nsgsplit RuleName = $nsg.Name Priority = $nsg.Priority DestinationPortRange = $nsg.DestinationPortRange -join ", " Protocol = $nsg.Protocol SourceAddressPrefix = $nsg.SourceAddressPrefix -join ", " SourceApplicationSecurityGroups = $ASGGroupNameSource DestinationAddressPrefix = $nsg.DestinationAddressPrefix -join ", " DestinationApplicationSecurityGroups = $ASGGroupNameDestination Direction = $nsg.Direction Access = $nsg.Access } ) }
$NsgRuleSet | Export-Excel -WorksheetName NSG -path $path$reportName
Get the basic list of users
$Users = Get-AzADUser
$UserDetails = @()
For each user retrieve the details (hidden in extended properties)
$progressCount = 0
for($i = 0; $i -le $Users.Count; $i++) {
# Just writes status Write-Progress -Id 0 -Activity "Retrieving User " -Status "$progressCount of $($Users.Count)" -PercentComplete (($progressCount / $Users.Count) * 100) $UserDetails += (Get-AzADUser)[$i] | Select-Object -Property * $progressCount++
}
Stops the progress report
Write-Progress -Id 0 -Activity " " -Status " " -Completed
Display the Properties
$UserDetails | Select DisplayName, AccountEnabled, JobTitle, Mobile, UserPrincipalName, UserType | Format-Table
try {
$UserDetails | Select DisplayName, AccountEnabled, JobTitle, Mobile, UserPrincipalName, UserType | Export-Excel -WorksheetName ADUSERS -path $path\$reportName Write-Host "Exported all user details" -ForegroundColor Green
} catch {
Write-Host "$error" -ForegroundColor red
}