Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Powershell functions

Posted on 2011-03-08
12
Medium Priority
?
1,171 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
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
 
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 71

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 71

Accepted Solution

by:
Chris Dent earned 1000 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
In the absence of a fully-fledged GPO Management product like AGPM, the script in this article will provide you with a simple way to watch the domain (or a select OU) for GPOs changes and automatically take backups when policies are added, removed o…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

688 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