Solved

Powershell functions

Posted on 2011-03-08
12
1,106 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
check java version using powershell 13 85
Delete Msol groups from csv file 2 37
powershell function location 6 37
Exchange powershell help 4 25
I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
This article will help you understand what HashTables are and how to use them in PowerShell.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

776 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