Solved

Powershell functions

Posted on 2011-03-08
12
1,073 Views
Last Modified: 2012-05-11
Hello,

Pls advice, I am trying to get email with function. In the description is the string which contains samaccountname, I extract it with regular expression. But I am not sure how to do if it doesnt contain that samaccount (looks like g1234567 ) name.


$mailbox = Get-QADUser -Enabled 'xxxx'
function GetMail
{
$descSamAccount = $mailbox |%{[regex]::Match($_.description,"\w\d{7}")}

if ($descSamAccount = $null)
{
$descNameMail = 'xxx.yyyyy@xxx.com'
return $descNameMail
}
else
{
$descNameMail = $descSamAccount.value | Get-QADUser | select -ExpandProperty email
return $descNameMail
}


and then how to call it:


$MailMessage = New-Object Net.Mail.MailMessage
$MailMessage.To.Add($descNameMail)    # ..To.Add(GetMail) -this doesnt work
$MailMessage.From = 'xxx.yyyyy@xxx.com'
$MailMessage.Subject = "Account Expiration Notice"
$MailMessage.IsBodyHtml = $True

......
;;;
0
Comment
Question by:Radim88
  • 6
  • 4
  • 2
12 Comments
 
LVL 12

Expert Comment

by:prashanthd
ID: 35068916
Why not use something like following to check if the samaccountname is valid, I am not familiar with quest cmdlets

get-qaduser -samaccountname $descSamAccount

If the above is returning null then it does not contain samaccount
0
 

Author Comment

by:Radim88
ID: 35069315
Thx but actually its there. I dont understand that parts how to call that function in ..To.Add(GetMail)  and how to specify if regex doesnt match the pattern.
0
 
LVL 12

Expert Comment

by:prashanthd
ID: 35069496
Try the following...not sure why Function is required, removed it
$mailbox = Get-QADUser -Enabled 'xxxx'
$sendmail = $false

$descSamAccount = $mailbox |%{[regex]::Match($_.description,"\w\d{7}")}

if ($descSamAccount -eq $False){
	$sendmail=$true
}
else{
$descNameMail = $descSamAccount.value | Get-QADUser | select -ExpandProperty email
}

if ($sendmail -eq $true){
$MailMessage = New-Object Net.Mail.MailMessage
$MailMessage.To.Add($descNameMail)    # ..To.Add(GetMail) -this doesnt work
$MailMessage.From = 'xxx.yyyyy@xxx.com'
$MailMessage.Subject = "Account Expiration Notice"
$MailMessage.IsBodyHtml = $True
}

Open in new window

0
 
LVL 12

Expert Comment

by:prashanthd
ID: 35069587
Missed out on the Add(GetMail)...working on it
0
 

Author Comment

by:Radim88
ID: 35069673
I believe I will need that functions, because this code is quite extensive. You see only fragment of it. I am totally lost there without functions. I am also not sure, can I somehow define the function in PS like public or private?
0
 

Author Comment

by:Radim88
ID: 35069733
It also seems that its not possible to use:

if ($descSamAccount -eq $false)


error:

Select-Object : Cannot process argument because the value of argument "obj" is null. Change the value of argume
nt "obj" to a non-null value.

-------------------------------------------------------------------------

$mailbox = Get-QADUser -Enabled 'xxx'
function GetMail
{
$descSamAccount = $mailbox |%{[regex]::Match($_.description,"\w\d{7}")}
if ($descSamAccount -eq $false)
{
$descNameMail = "xxxx@yyyy.com"
$descNameMail
}
else
{
$descNameMail = $descSamAccount.value | Get-QADUser | select -ExpandProperty email
$descNameMail
}
}
GetMail
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 12

Expert Comment

by:prashanthd
ID: 35069770
Can you explain what is expected in steps?
0
 

Author Comment

by:Radim88
ID: 35069888
This GetMail function only needs to get email from user. There are a lot of special accounts, which have in description some string like: "O:x1234567;V:01.01.2012;Car operation" . From this string I am extracting samaccountname of some users, in GetMail function I only need to get email from that samaccountname. If there is not samaccountname in this string, could be only "Car operation", I need to use hardcoded email address. and pass it into "$MailMessage.To.Add($descNameMail)".
And I am not sure how to do that, firstly it gives me always error written above , if I run this function and in the description is not listed samaacount name and secondly, this method  "$MailMessage.To.Add(GetMail)"  doesnt accept input from the function.

It could be some misunderstading from my side of course.

Many thx
0
 
LVL 70

Expert Comment

by:Chris Dent
ID: 35072657

> To.Add(GetMail) -this doesnt work

No, but that's because of execution order. You need to make GetMail execute first.

To.Add((GetMail))

For the rest, to make sure I understand:

1. Test Description
2. If desciption contains a SamAccountName, pull email from that account instead
3. Else return Email from the current account

Is that right? And is the RegEx you're using working as intended?

Chris
0
 

Author Comment

by:Radim88
ID: 35073525
Hello Chris,

Exactly.

I did it via:

$mailbox = Get-QADUser -Enabled 'xxx'
function GetMail
{
try{

$descSamAccount = $mailbox |%{[regex]::Match($_.description,"\w\d{7}")}
$descNameMail = $descSamAccount.value | Get-QADUser | select -ExpandProperty email
$descNameMail

}
catch
{
$descNameMail = "xxxx@yyyy.com"
$descNameMail
}
}
GetMail


Its working, but its not the best solution I think. Please advice something, regex is working ok. I wanted to use "if" there, but it doesnt work. "if ($descSamAccount = $null)".

And I would like to ask also about functions, can I somehow make them private, like in C# for instance. I mean by this, that variables inside cannot be seen in other parts of code.

And also thanks for this "To.Add((GetMail))" I figured it by testing.

0
 
LVL 70

Accepted Solution

by:
Chris Dent earned 250 total points
ID: 35073775
= is used for assignment in PowerShell, not for comparison. To compare, you want -eq:
if ($descSamAccount -eq $Null) {
  # Do stuff
}

Open in new window

By default, variables used within functions are local / private. However, if you declare a varaible in script or global scope you can modify it within a function.

For example:
Function Set-X {
  $x = 123
  $y = 321
}

$x = 987
Set-X
$x
$y

Open in new window

You should find that $y does not exist outside of the function.

There are more scope modifiers, but use of those has a tendency to make code obscure so I generally advise that you avoid using them.

For example, inside functions you have $Private:SomeVariable. Inside scripts, $Script:SomeVariable. And outside both you have $Global:SomeVariable. $x in the example above is also $Global:x.

Chris
0
 

Author Comment

by:Radim88
ID: 35073909
Thanks,

But its strange if I used "$descNameMail" in $MailMessage.To.Add($descNameMail)  it normally worked and this variable exists only inside the body of function.

But you answered my questions :-)

Thx
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Utilizing an array to gracefully append to a list of EmailAddresses
Set OWA language and time zone in Exchange for individuals, all users or per database.
This video discusses moving either the default database or any database to a new volume.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now