0

I have a beginner's knowledge in scripting and programming and have a set of PowerShell commands that I am having issues with figuring out how to turn it into a script.

I can successfully run the following remote commands from my Windows 7 machine by running the Exchange 2007 PowerShell console as my domain administrator account and then running the following commands to pass the commands to the Exchange 2013 hybrid server for use with Office 365:

$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://hybridexch2013.contoso.com/PowerShell/ -Authentication Kerberos

Import-PSSession $Session

Enable-RemoteMailbox jame.doe@contoso.com -RemoteRoutingAddress jane.doe@contosoinc.mail.onmicrosoft.com

The more I look into this I don't know that I am making progress. See below for how my logic is working in my head on this. I understand this is incorrect and incomplete. I have commented out a function I had earlier but wasn't sure if I was doing it correct or if it was needed at all.

param($enableMailbox)
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://hybridexch2013.contoso.com/PowerShell/ -Authentication Kerberos
Import-PSSession $Session -AllowClobber

$fname = Read-Host "What is the user's first name?"
$lname = Read-Host "What is the user's last name?"
#function Func($enableMailbox)
#{
$enableMailbox = "Enable-RemoteMailbox $fname.$lname@contoso.com -RemoteRoutingAddress $fname.$lname@contosoinc.mail.onmicrosoft.com"
#}
#Func $enableMailbox
Write-Host $enableMailbox

I also find that if I manually run:

Enable-RemoteMailbox $fname.$lname@contoso.com -RemoteRoutingAddress $fname.$lname@contosoinc.mail.onmicrosoft.com

I get nothing. So I am not even understanding how you pass variables to the string to run the command correctly. Even if I run:

$fname = "Jane"
$lname = "Doe"
$enableMailbox = "Enable-RemoteMailbox $fname.$lname@contoso.com -RemoteRoutingAddress $fname.$lname@contosoinc.mail.onmicrosoft.com"

    Write-Host $enableMailbox

I get no results.

I was trying to understand the param function using help from these pages: Powershell script with params *and* functions

Passing a variable to a powershell script via command line

https://devcentral.f5.com/blogs/us/powershell-abcs-p-is-for-parameters

http://www.experts-exchange.com/Programming/Languages/Scripting/Powershell/Q_27900846.html

But I am finding the param function difficult to understand and not sure I am even going in the right direction here. So far the only thing that seems to work is connecting to the PowerShell remotely.

Please help if I am am to be helped with this one and lack of my abilities here.

Community
  • 1
  • 1
TheBrianGuy
  • 15
  • 1
  • 1
  • 6

2 Answers2

0

Param Function in short...

 Function Write-Something
    {
        Param($InputText)
        Write-Host $InputText
    }

Write-Something Hello

Result is: Hello

Use Param Section to add Parameters to your Function like in the example above,

So for your Script:

$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://hybridexch2013.contoso.com/PowerShell/ -Authentication Kerberos
Import-PSSession $Session -AllowClobber

Function Enable-MBX
{
Param($Fname,$Lname)

 Enable-RemoteMailbox "$Fname $Lname" -RemoteRoutingAddress "$fname.$lname@contoso.mail.onmicrosoft.com"  
}

Enable-MBX Jane Doe
Avshalom
  • 8,657
  • 1
  • 25
  • 43
  • The was very helpful and helped me understand much more clearly the logic. This information allowed to me create the script I was looking for successfully. In the final script I just added the Read-Host to ask for the first and last name of the user. Then I changed the last line to Enable-MBX $fname $lname to access the variables set by the read-host :$fname = Read-Host "What is the user's first name?" $lname = Read-Host "What is the user's last name?" – TheBrianGuy Oct 15 '15 at 15:31
0

I think you're just confused about Write-Host. I'm not certain if you're trying to write the enable-remotemailbox to console or execute it. The code you have should work fine for writing to console (screen) but won't execute the command. To execute the command:

Enable-RemoteMailbox "$fname.$lname@contoso.com" -RemoteRoutingAddress "$fname.$lname@contosoinc.mail.onmicrosoft.com"

Anything inside of double-quotes will expand. Ex: "$fname" will expand to "Bob" if $fname is equal to "Bob". If you encapsulate the whole command in quotes in a variable though, the Write-Host will NOT execute the command. Write-Host is designed to write output to the screen so you'll just see the command in the console.

If you want to further expand, you can use the sub-string operator $(). Ex:

Write-Host "$($fname.length).$($lname.length)"

Comes back as "3.5" for "Bob" and "Smith".

To avoid expansion, use single quotes:

Write-Host '$($fname.length).$($lname.length)'

Writes "$($fname.length).$($lname.length)" to the console.

The following (similar to your code) without quotes: Write-Host $fname.$lname@contoso.com will try to pull the $lname@contoso.com Property of $fname which in this context doesn't make sense (doesn't exist). To elaborate, it would be equivalent to Write-Host $fname.($lname@contoso.com).

duct_tape_coder
  • 472
  • 5
  • 20
  • Yes thank you for the information. I now understand how write-host works and how it wasn't what I expected. Your examples were very clear as well and helped me understand how to correct my mistakes and will no doubt help me in any future PowerShell scripts I may write. – TheBrianGuy Oct 15 '15 at 15:35