Solved

Powershell functions

Posted on 2011-03-08
12
1,153 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
What, When and Where - Security Threats from Q1

Join Corey Nachreiner, CTO, and Marc Laliberte, Information Security Threat Analyst, on July 26th as they explore their key findings from the first quarter of 2017.

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

Are You Headed to Black Hat USA 2017?

Getting ready for Black Hat next week? Kick things off with the WatchGuard Badge Challenge and test your puzzle and cipher skills. Do you have what it takes to earn our limited edition Firebox Badge? Get started today - https://crimsonthorn.net

Question has a verified solution.

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

Synchronize a new Active Directory domain with an existing Office 365 tenant
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
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…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

632 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