Exchange powershell function command errors with "not recognized as the name of a cmdlet, function, script file, or operable program"

I've been using the Get-ExchangeWhiteSpace from Shay Levi’s blog,  http://blogs.microsoft.co.il/blogs/scriptfanatic/archive/2009/08/13/exchange-white-space.aspx, to get a white space report.  It was working until I changed the name of one of the directories in the path to the scripts.  Now the function runs successfully, but then when I run the command it's not recognized.  I changed the directory name back with no luck.  Any suggestions?
mbrombAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bruno PACIIT ConsultantCommented:
Hi,

How did you launch your function exactly ? Tell us more...

Do you launch a PowerShell console and type something like:

. C:\MyScripts\WhiteSpaceFunction.ps1
Get-ExchangeWhiteSpace ....

??
0
mbrombAuthor Commented:
I tried it a couple ways.  When it was working I was running it from the powershell editor.  I ran  RunFxnThenCmd.ps1 to run the function (whitespacefunction.ps1) and the commands at once. then I changed the path from C:\Tools\Scripts\Powershell to C:\Tools\~Scripts\Powershell, and that's when it got unhappy.  The usual way stopped working even after changing the path name back.  I tried from the pshell prompt run as admin.  first running RunFxnThenCmd.ps1 to get the error "not recognized...". Then I tried running the whitespacefunction.ps1, which succeeds in all cases, then running just a single command, "Get-ExchangeWhiteSpace -ComputerName server1", with the same error message reulsting. I used the .\ notation in the pshell cases.  This has to be something stupid.  Ain't it always.

whitespacefunction.ps1:
---------------------------------------------
# Get-ExchangeWhiteSpace from Shay Levi’s blog
# http://blogs.microsoft.co.il/blogs/scriptfanatic/archive/2009/08/13/exchange-white-space.aspx 


function Get-ExchangeWhiteSpace {

param(
   $ComputerName = $(throw "ComputerName cannot be empty.")
)

# Convert Dates to WMI CIM dates
$tc = [System.Management.ManagementDateTimeconverter]
$Start =$tc::ToDmtfDateTime( (Get-Date).AddDays(-1).Date )
$End =$tc::ToDmtfDateTime( (Get-Date).Date)

# Create two claculated properties for InsertionStrings values
$DB = @{Name="DB";Expression={$_.InsertionStrings[1]}}
$FreeMB = @{Name="FreeMB";Expression={[int]$_.InsertionStrings[0]}}

Get-WMIObject Win32_NTLogEvent -ComputerName $ComputerName -Filter "LogFile='Application' AND EventCode=1221 AND TimeWritten>='$Start' AND TimeWritten<='$End'" | Select-Object ComputerName,$DB,$FreeMB | Sort-Object FreeMB –Unique –Descending

}
---------------------------------------------

RunFxnThenCmd.ps1:
---------------------------------------------
C:\Tools\Scripts\Powershell\WhiteSpaceFunction.ps1
Get-ExchangeWhiteSpace -ComputerName server1
Get-ExchangeWhiteSpace -ComputerName server2
Get-ExchangeWhiteSpace -ComputerName server3
Get-ExchangeWhiteSpace -ComputerName server4
Get-ExchangeWhiteSpace -ComputerName server5
Get-ExchangeWhiteSpace -ComputerName server6
Get-ExchangeWhiteSpace -ComputerName server7
Get-ExchangeWhiteSpace -ComputerName server8
---------------------------------------------
0
Bruno PACIIT ConsultantCommented:
Hi,

OK. I think I have it. Here is your RunFxnThenCmd.ps1 script corrected:

. C:\Tools\Scripts\Powershell\WhiteSpaceFunction.ps1
Get-ExchangeWhiteSpace -ComputerName server1
Get-ExchangeWhiteSpace -ComputerName server2
Get-ExchangeWhiteSpace -ComputerName server3
Get-ExchangeWhiteSpace -ComputerName server4
Get-ExchangeWhiteSpace -ComputerName server5
Get-ExchangeWhiteSpace -ComputerName server6
Get-ExchangeWhiteSpace -ComputerName server7
Get-ExchangeWhiteSpace -ComputerName server8


Note that I added a dot "." on the first line of your script followed by a space " " and followed by the function script path.

In this line the dot "." doesn't mean "current directory" ! The dot is sort of operator that tells the powershell interpretor to take the content of the file that follows and interpret it as if the content had been typed in the console... I suppose you though the dot had something to do with current directory and deleted it but you're wrong...
If you omit the dot all happen like powershell interpretor launched another powershell console and execute the script in this new console, then the new console stops and all what has been done in this new console is forgotten... so the function declaration is forgotten when your initial console get back the execution.
If you add the dot the content of the script is not executed in a new console but in the current console, like if you had typed it there.

Have a good day.


0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Bruno PACIIT ConsultantCommented:
Sorry my english is poor.... I hope you understood all my "strange" language ;)
0
mbrombAuthor Commented:
Didn't work.  Like I said, it worked before the path renaming, and i tried it from the shell.
0
Bruno PACIIT ConsultantCommented:
Hi again,

I found better explanations in a "good" english in this article: http://www.powershellpro.com/organizing-powershell-script-code-is-a-snap-in/102/

Look down at the "Dot Sourcing" chapter...

By the way I forgot the "{" and "}" in my previous explanations... sorry...
0
Bruno PACIIT ConsultantCommented:
When it was working you probably were working with all .PS1 scripts in the same directory and this directory was the current directory...

If you type something like Test-MyScript in the console, powershell look for a known commandlet and then search for a file named Test-MyScript.PS1 in the current directory...
0
mbrombAuthor Commented:
I noticed the link showed a dot and then a space.  That worked for me!  thanks!  i don't get why it worked before.  the scripts have always been in the same directory, and the current.
0
Bruno PACIIT ConsultantCommented:
Yes... As I told you in my first answer, I said: Note that I added a dot "." on the first line of your script followed by a space " " and followed by the function script path.
You need the space...


0
mbrombAuthor Commented:
Yup, I missed it.  Well, problem solved.  thanks again.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Exchange

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.