Getting Error while running

Anonymous
2023-04-03T09:57:27.35+00:00

While running the below scripts getting the error

 
Connect-MgGraph -xxx

$Result=""   
$Results=@()  
$Print=0
$ShowAllSubscription=$False
$PrintedOutput=0

#Output file declaration 
$ExportCSV=".\License\LicenseExpiryReport.csv" 

#Check for filters
if((!($Trial.IsPresent)) -and (!($Free.IsPresent)) -and (!($Purchased.IsPresent)) -and (!($Expired.IsPresent)) -and (!($Active.IsPresent)))
{
 $ShowAllSubscription=$true
}

#FriendlyName list for license plan 
$FriendlyNameHash=@()
$FriendlyNameHash=Get-Content -Raw -Path .\License\LicenseFriendlyName.txt -ErrorAction Stop | ConvertFrom-StringData 

#Get available subscriptions in the tenant
$Subscriptions= Get-MgSubscribedSKU | foreach{
 $SubscriptionName=$_.SKUPartNumber
 $SubscribedOn=$_.DateCreated
 $ExpiryDate=$_.NextLifeCycleDate
 $Status=$_.Status
 $TotalLicenses=$_.TotalLicenses
 $Print=0

 #Convert Skuid to friendly name  
 $EasyName=$FriendlyNameHash[$SubscriptionName] 
 $EasyName
 if(!($EasyName)) 
 {
  $NamePrint=$SubscriptionName
 } 
 else 
 {
  $NamePrint=$EasyName
 } 
 
 #Convert Subscribed date to friendly subscribed date
 $SubscribedDate=(New-TimeSpan -Start $SubscribedOn -End (Get-Date)).Days
 if($SubscribedDate -eq 0)
 {
  $SubscribedDate="Today"
 }
 else
 {
  $SubscribedDate="$SubscribedDate days ago"
 }
 $SubscribedDate="(" + $SubscribedDate + ")"
 $SubscribedDate="$SubscribedOn $SubscribedDate"

 #Determine subscription type
 If($_.IsTrial -eq $False)
 {
  if(($SubscriptionName -like "*Free*") -or ($ExpiryDate -eq $null))
  {
   $SubscriptionType="Free"
  }
  else
  {
   $SubscriptionType="Purchased"
  }
 }
 else
 {
  $SubscriptionType="Trial"
 }

 #Friendly Expiry Date
 if($ExpiryDate -ne $null)
 {
  $FriendlyExpiryDate=(New-TimeSpan -Start (Get-Date) -End $ExpiryDate).Days
  if($Status -eq "Enabled")
  {
   $FriendlyExpiryDate="Will expire in $FriendlyExpiryDate days"
  }
  elseif($Status -eq "Warning")
  {
   $FriendlyExpiryDate="Expired.Will suspend in $FriendlyExpiryDate days"
  }
  elseif($Status -eq "Suspended")
  {
   $FriendlyExpiryDate="Expired.Will delete in $FriendlyExpiryDate days"
  }
  elseif($Status -eq "LockedOut")
  {
   $FriendlyExpiryDate="Subscription is locked.Please contact Microsoft"
  }
 }
 else
 {
  $ExpiryDate="-"
  $FriendlyExpiryDate="Never Expires"
 }

 #Check for filters
 if($ShowAllSubscription -eq $true)
 {
  $Print=1
 }
 else
 {
  if(($Trial.IsPresent) -and ($SubscriptionType -eq "Trial"))
  {
   $Print=1
  }
  if(($Free.IsPresent) -and ($SubscriptionType -eq "Free"))
  {
   $Print=1
  }
  if(($Purchased.IsPresent) -and ($SubscriptionType -eq "Purchased"))
  {
   $Print=1
  }
  if(($Expired.IsPresent) -and ($Status -ne "Enabled"))
  {
   $Print=1
  }
  if(($Active.IsPresent) -and ($Status -eq "Enabled"))
  {
   $Print=1
  }
 }
 
 

 #Export result to csv
 if($Print -eq 1)
 {
  $PrintedOutput++
  $Result=@{'Subscription Name'=$SubscriptionName;'Friendly Subscription Name'=$NamePrint;'Subscribed Date'=$SubscribedDate;'Total Licenses'=$TotalLicenses;'License Expiry Date/Next LifeCycle Activity Date'=$ExpiryDate;'Friendly Expiry Date'=$FriendlyExpiryDate;'Subscription Type'=$SubscriptionType;'Status'=$Status}
  $Results= New-Object PSObject -Property $Result  
  $Results | Select-Object 'Subscription Name','Friendly Subscription Name','Subscribed Date','Total Licenses','Subscription Type','License Expiry Date/Next LifeCycle Activity Date','Friendly Expiry Date','Status' | Export-Csv -Path $ExportCSV -Notype  -Encoding UTF8 -Append
 }
}

foreach : Cannot index into a null array.
At line:1 char:39
+ $Subscriptions= Get-MgSubscribedSKU | foreach{
+                                       ~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [ForEach-Object], RuntimeException
    + FullyQualifiedErrorId : NullArray,Microsoft.PowerShell.Commands.ForEachObjectCommand
Windows for business Windows Server User experience PowerShell
{count} votes

1 answer

Sort by: Most helpful
  1. MotoX80 36,291 Reputation points
    2023-04-04T11:59:05.6866667+00:00

    So this line ..... $Subscriptions= Get-MgSubscribedSKU | foreach{ As I read that, it says to take the output from Get-MgSubscribedSKU and pipe it to a foreach, and then assign the output from the foreach to the Subscriptions variable. But you never reference the Subscriptions variable anywhere else, and the only output from the foreach looks to be EasyName. Based on the error you got, it looks like the output of Get-MgSubscribedSKU is getting assigned to Subscriptions and not getting piped to the foreach. I have no way to test your code myself.

    Remove the Subscriptions assignment and see if that works.

    #Get available subscriptions in the tenant
    Get-MgSubscribedSKU | foreach{
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.