Import-Module azuread
$AdminPassword = "*******"
$Directory = "abcltdnew.onmicrosoft.com"
$spURL = "https://abcltdnew-admin.sharepoint.com/"
$onedrivelike = 'https://abcltdnew-my.sharepoint.com/'
# Create a new Password Profile for the new users
$NewUserPassword = 'Dummy@365' #'Dummy@1234'
$PasswordProfile = New-Object -TypeName Microsoft.Open.AzureAD.Model.PasswordProfile
$PasswordProfile.Password = $NewUserPassword
$CsvFilePath = "E:\CHG156539\PSDevelopmentZone\PendingUsers4.csv"
# Create a PowerShell connection to my directory
$SecPass = ConvertTo-SecureString $AdminPassword -AsPlainText -Force
$Cred = New-Object System.Management.Automation.PSCredential ($Admin, $SecPass)
Connect-AzureAD -Credential $cred
# Import the csv file
$NewUsers = import-csv -Path $CsvFilePath
$result = @()
#$result += @('"User","AdUser","Group","OneDriveProvisioned"')
# Loop through all new users in the file
Foreach ($NewUser in $NewUsers) {
# Construct the UserPrincipalName, the MailNickName and the FirstName
$UPN = $NewUser.'User Name' + '@' + $Directory
$FirstName = $NewUser.'First Name'
$MailNickName = $FirstName
$DisplayName = $NewUser.'User Name'
$oneDriveFlag = 'No'
$adUserFlag = 'No'
$groupFlag = 'No'
$GroupName = 'Global-OneDrive E3 Default Azure'
$adUserID = ''
If(-not [string]::IsNullOrEmpty((Get-AzureADUser | Where {$_.UserPrincipalName -eq $UPN}).ObjectID))
{
$adUserID = (Get-AzureADUser | Where {$_.UserPrincipalName -eq $UPN}).ObjectID
$adUserFlag = 'Yes'
Connect-PnPOnline -Url $spURL -credential $cred
# Get OneDrive URL
If(-not [string]::IsNullOrEmpty((Get-PnPTenantSite | Where {$_.Url -like $onedrivelike}).Url))
{
#Check License also
IF(-not [string]::IsNullOrEmpty((Get-AzureADUserLicenseDetail -ObjectId $adUserID )) )
#IF(-not [string]::IsNullOrEmpty((Get-AzureADUserLicenseDetail -ObjectId (Get-AzureADUser | Where {$_.UserPrincipalName -eq $UPN}).ObjectID )) )
{
$oneDriveFlag = 'Yes'
}
else{
$oneDriveFlag = 'No'
}
}
else
{
$oneDriveFlag = 'No'
}
$adGroup = ''
If(-not [string]::IsNullOrEmpty((Get-AzureADGroup | Where {$_.DisplayName -eq $GroupName}).ObjectID))
{
Write-Host $GroupName "Group exists"
$adGroup = (Get-AzureADGroup | Where {$_.DisplayName -eq $GroupName})
If([string]::IsNullOrEmpty(( Get-AzureADGroupMember -ObjectId $adGroup.ObjectId )) )
{
$groupFlag = 'No'
#Write-Host $DisplayName "is not a Member of the Group"
}
else{
$groupFlag = 'Yes'
#Write-Host $DisplayName "is a Member of the Group"
}
}
else
{
Write-Host $DisplayName $GroupName "Group doesn't exist"
$groupFlag = 'No'
}
}
else
{
$adUserFlag = 'No'
Write-Host $DisplayName "doesn't exist in the Active Directory[AD]"
}
$ExportItem = New-Object PSObject
$ExportItem | Add-Member -MemberType NoteProperty -name "User" -value $UPN
$ExportItem | Add-Member -MemberType NoteProperty -name "AdUser" -value $adUserFlag
$ExportItem | Add-Member -MemberType NoteProperty -name "Group" -value $groupFlag
$ExportItem | Add-Member -MemberType NoteProperty -name "OneDriveProvisioned" -value $oneDriveFlag
$result += $ExportItem
}
$result | Export-Csv -Path "E:\CHG156539\PSDevelopmentZone\TotalOutput.csv" -NoTypeInformation
Pause