I'm new to any form of scripting/coding and I'm trying to get a piece of PowerShell code to allow the ICT teachers at the school to have access to the students OneDrive's. The code is pretty much all there, except whenever a variable is called it seems to output in a strange format.
As an example, here is my declaration of the variabl $teachers
$teachers = Get-ADGroupMember -Identity "ICT_Teacher_Group" | ForEach-Object {
Get-ADUser -Identity $_.SamAccountName | Select-Object -Property UserPrincipalName
}
When outputted via Write-Output $teachers
I get this format:
UserPrincipalName
-----------------
teacher1@mydomain.co.uk
teacher2@mydomain.co.uk
teacher3@mydomain.co.uk
teacher4@mydomain.co.uk
Which includes the heading of 'UserPrincipalName', which I think it where the problem lies.
When combined with a bit more of the code, the output gets worse.
Full Code:
$teachers = Get-ADGroupMember -Identity "Teacher Security Group" | ForEach-Object {
Get-ADUser -Identity $_.SamAccountName | Select-Object -Property UserPrincipalName
}
$students = Get-ADGroupMember -Identity "Student Security Group" | Select-Object SamAccountName
$(forEach ($student in $students){
forEach ($teacher in $teachers){
Write-Output "OneDrive for $student is now available to $teacher"
}
}) | Out-File test.txt
Output file:
OneDrive for @{SamAccountName=student1} is now available to @{UserPrincipalName=teacher1@mydomain.co.uk}
OneDrive for @{SamAccountName=student1} is now available to @{UserPrincipalName=teacher2@mydomain.co.uk}
OneDrive for @{SamAccountName=student1} is now available to @{UserPrincipalName=teacher3@mydomain.co.uk}
OneDrive for @{SamAccountName=student1} is now available to @{UserPrincipalName=teacher4@mydomain.co.uk}
OneDrive for @{SamAccountName=student2} is now available to @{UserPrincipalName=teacher1@mydomain.co.uk}
OneDrive for @{SamAccountName=student2} is now available to @{UserPrincipalName=teacher2@mydomain.co.uk}
etc...
Can anyone explain why the variables are outputted in the format @{UserPrincipalName=teacher1@mydomain.co.uk}
and how I can change this to simply output teacher1@mydomain.co.uk
As a novice, any and all help would be greatly appreciated!