I have been playing with PowerShell to interrogate an AD.
I have looked, but am failing to understand some of the behaviour when using ‘echo’ to dump stuff to screen (when debugging). My lack of understanding means I cant be sure it will behave as expected.
It is the second output I want - how do I ensure returning the value (@smith.com) and not the label (emailAddress)? Is there a simple rule to force the value to be used instead of the label? What am I missing?
Many thanks
I don’t expect the rest of my script to be relevant, but as it is my first* and I’m not sure, its attached here. You’ll note that this is not really my work - just an adaptation of others work (borrowed this approach from u$)
foreach ($user in $group.members)
{
# only process users valid for my domain (should just check it ends "@smith.com")
IF (($user.emailAddress).length -gt 5)
{
#Is user prohibited from changing password?
if ($user.UserCannotChangePassword -eq $false)
{
#is user enabled?
if ($user.Enabled -eq $true)
{
#calculate number of days before expiry
$daysRemaining = ($user.LastPasswordSet.AddDays($MaxPwdAge) - $todaysDate).Days
#Has Password got ($maxWarningDays or less) before expiry?
if (($daysRemaining -lt 8) -And ($daysRemaining -gt 0))
{
$emailAddStr = $user.emailAddress
#password is old enough for warning - and has not expired
echo "FOUND ONE!! $emailAddStr password expires in $daysRemaining days"
#send nag email
}
}
# Echo EVERY user to screen for debugging
$debStr = $user.emailAddress
echo "DEBUG INFO $debStr password expires in $daysRemaining days"
}
}
}
First of all: You should not use aliasses in scripts and please do not use aliasses in forums.
Then: If you want to learn more about a particular cmdlet you should start with its help. The cmdlet for the alias “echo” is
The behaviour you experience comes from the variable expansion PowerShell does implicitly for you when you provide a variable like this for Write-Host.
The proper way for an output like this would be to use a subexpression like this:
Write-Host "DEBUG INFO $($user.emailAddress) password expires in $daysRemaining days"
… and the real proper way to output debug information would be to use
This way you could run the function or script by default without outputting the debug information and in case you need them you can add the parameter -Debug and get the info you need.
It looks like I will need a lot of extra brackets to be sure I get what I expect.
In your link about variables, it explains braces and back ticks can actually be used as part of a variable name - almost as confusing as aliases (which I will omit from any future posts).
I will keep trying with PS - I’m currently playing with ISE. Hope to be able to use the debugger, else it will be a return to c#.
Hmmm … not everything what’s possible make sense and should be used. Especially backticks should be avoided if possible at any costs in my opinion … not just in variable names.
You may keep in mind … that’s just the IDE. The target runtime environment for PowerShell scripts - especially non interactive scripts - is the normal PowerShell console.
And if you want to keep using PowerShell and writing script you may take a look at