Author Posts

January 25, 2017 at 3:23 pm

Dear People,

I have a script that putt the licenses informatien of multiple clients to excel in a folder.

The main script is:

# write-host "Connecting to Office 365..."
Import-Module MSOnline

#1 - klant1#
$username = "klant1@office365.nl"
$password = cat C:\temp\mysecurestringlocatie | convertto-securestring
$Office365credentials = new-object -typename System.Management.Automation.PSCredential `
         -argumentlist $username, $password
& 'C:\Scripts\Sandbox 2- get_O365_licenses_all_tenants\O365licenties-verkort.ps1'

#2 - klant2#
$username = "klant2@office365.nl"
$password = cat C:\temp\mysecurestringlocatie | convertto-securestring
$Office365credentials = new-object -typename System.Management.Automation.PSCredential `
         -argumentlist $username, $password
& 'C:\Scripts\Sandbox 2- get_O365_licenses_all_tenants\O365licenties-verkort.ps1'

#3 - klant3#
$username = "klant3@office365.nl"
$password = cat C:\temp\mysecurestringlocatie | convertto-securestring
$Office365credentials = new-object -typename System.Management.Automation.PSCredential `
         -argumentlist $username, $password
& 'C:\Scripts\Sandbox 2- get_O365_licenses_all_tenants\O365licenties-verkort.ps1'

as you can see this script connects to a tenant and then runs the script that give me the license information of the client.

$VerbosePreference = 'Continue'    # Makes verbose meldingen zichtbaar : Modify to your needs

# Set Working directory so that the export-files will be written to a location where the user has the necessary rights
Set-Location "\\server\o365lcienses\"

# write-host "Connecting to Office 365..."
Connect-MsolService -Credential $Office365credentials

# Get a list of all licences that exist within the tenant
$licensetype = Get-MsolAccountSku | Where {$_.ConsumedUnits -ge 1}

Write-Verbose "License types are:" 
$lts = $licensetype| select -expandproperty accountskuid | Format-Table -Autosize | Out-String
Write-Verbose $lts

Write-Verbose "Getting all users (may take a while) ..."
$allusers = Get-MsolUser -all 
Write-Verbose ("There are " + $allusers.count + " users in total")

# Loop through all licence types found in the tenant
foreach ($license in $licensetype) 
{ 
 # Build and write the Header for the CSV file
    $LicenseTypeReport = "Office365_" + ($license.accountskuid -replace ":","_") + "_" + (Get-Date -format d.M.yyyy) + ".csv"
    Write-Verbose ("New file: "+ $LicenseTypeReport)

 $headerstring = "DisplayName;AccountSku"

 Out-File -FilePath $LicenseTypeReport -InputObject $headerstring -Encoding UTF8 -append
 
 write-Verbose ("Gathering users with the following subscription: " + $license.accountskuid)

 # Gather users for this particular AccountSku
 $users = $allusers | where {$_.isLicensed -eq "True" -and $_.licenses.accountskuid -contains $license.accountskuid}

 # Loop through all users and write them to the CSV file
 foreach ($user in $users) {
  
        $thislicense = $user.licenses | Where-Object {$_.accountskuid -eq $license.accountskuid}
        $datastring = (($user.displayname -replace ","," ") + ";" + $license.SkuPartNumber)
  Out-File -FilePath $LicenseTypeReport -InputObject $datastring -Encoding UTF8 -append
 }
} 

write-Verbose ("Script Completed.")

But i want to change it a little bit because im trying to get the following thing.
in the folder \\server\o365lcienses\ i want that the script first makes a folder with the date of the creation day but only the month and year. because this script must run every month.
after the folder is created all the csv files of the clients must be put in that folder.

IS this even posible???

January 25, 2017 at 4:43 pm

Hello, Jeremy! Yes, of course it's possible, just do this:

# the below lines go before the loop
$OutDir = "\\server\o365licenses\$(Get-Date -Format yyyy.MM)" # a year goes first for the proper sorting
if (-not (Test-Path $OutDir)) {
    New-Item -Path $OutDir -ItemType Directory # create the directory if it doesn't exist
}
# ...
# replace your line inside the loop with this
$LicenseTypeReport = "$OutDir\Office365_{0}_{1}.csv" -f ($license.accountskuid -replace ":","_"), (Get-Date -Format dd.MM.yyyy)

If I didn't miss anything, it should work.