Reputation: 55
My goal is to run a powershell script that will see if the current user is a member of a group. If the user is a member then it will change the content of a file. If not, it just exits. I just ran it and the changes on my files were not made.
I know that individually these 2 separate scripts work as advertised. I need help determining why when I combine them its not giving me the desired result since there are no error messages:
Import-Module ActiveDirectory
$group = "The Beatles"
$members = Get-ADGroupMember -Identity $group -Recursive | Select -ExpandProperty Name
if($members -contains $ENV:USERNAME)
{
$File = "\\server\folder\$($Env:USERNAME)\File.reg"
$Content = Get-Content -Path $File |
ForEach-Object {
$_ -replace 'peter best',$ENV:USERNAME
}
Set-Content -Path $File -Value $Content
}
else
{
Exit
}
I replaced the contents of the if bracket with: Write-Host "WINNER WINNER!" and it still didn't do anything. But if I replace the if with a for and eliminate the else portion it works spits out "WINNER WINNER!". My guess is that my syntax or understanding on how to execute the code inside the If curly brackets is the culprit.
How do I get the desired result?
I appreciate the input.
Thanks,
Victor
Upvotes: 1
Views: 1115
Reputation: 174465
The Active Directory Name
attribute value is unlikely to be the same as the username (ie. your might be "Victor Antifries" but your username might be "v.antifries" or "victor01").
You'd want to grab the SAMAccountName
property instead:
$members = Get-ADGroupMember -Identity $group -Recursive | Select -ExpandProperty SAMAccountName
Upvotes: 2