Link to home
Start Free TrialLog in
Avatar of mbromb
mbromb

asked on

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?
Avatar of Bruno PACI
Bruno PACI
Flag of France image

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

??
Avatar of mbromb
mbromb

ASKER

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
---------------------------------------------
ASKER CERTIFIED SOLUTION
Avatar of Bruno PACI
Bruno PACI
Flag of France image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Sorry my english is poor.... I hope you understood all my "strange" language ;)
Avatar of mbromb

ASKER

Didn't work.  Like I said, it worked before the path renaming, and i tried it from the shell.
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...
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...
Avatar of mbromb

ASKER

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


Avatar of mbromb

ASKER

Yup, I missed it.  Well, problem solved.  thanks again.