Solved

Powershell functions

Posted on 2011-03-08
12
1,089 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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
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

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.

Question has a verified solution.

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

Microsoft Windows Server Update Service (WSUS) is free for everyone, but it lacks of some desirable features like send an e-mail to the administrator with the status of all computers on the WSUS server. This article is based on my PowerShell script …
A brief introduction to what I consider to be the best editor for PowerShell.
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

896 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

17 Experts available now in Live!

Get 1:1 Help Now