• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 365
  • Last Modified:

powershell 2.0 cmd line execution problem

Hi, I am new to power shell.
I wrote a simple code to echo out the current time with message. This works fine inside powershell_ise but when i try to run this script from a cmd file, an error is given.

The term 'main' is not recognized as the name of a cmdlet, function, script fil
e, or operable program. Check the spelling of the name, or if a path was includ
ed, verify that the path is correct and try again.
At C:\Release\Release\deploySite.ps1:14 char:5
+ main <<<< 
    + CategoryInfo          : ObjectNotFound: (main:String) [], CommandNotFoun
   dException
    + FullyQualifiedErrorId : CommandNotFoundException

cmd line script
powershell -file "./deploySite.ps1" > result.log
Content of deploySite.ps1 is as follows

#Run program
main

function main
{
    outlog "Initiate deployment"
 }
function outlog($message)
{
    $output = ( get-date ).ToString('yyyyMMdd hh:mm:ss') + " $message"
    echo $output
}

Open in new window


Could someone please tell me why I get this error when running from cmd and how to fix this issue? thank you
0
JRI-Support
Asked:
JRI-Support
1 Solution
 
JRI-SupportAuthor Commented:
I am using windows 2008 Standard
0
 
KenMcFCommented:
Put your functions first like this. I didnt check the rest of the script just moved the functions around.



function outlog($message)
{
    $output = ( get-date ).ToString('yyyyMMdd hh:mm:ss') + " $message"
    echo $output
}
function main
{
    outlog "Initiate deployment"
 }

#Run program
main

Open in new window

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

Join & Write a Comment

Featured Post

Protect Your Employees from Wi-Fi Threats

As Wi-Fi growth and popularity continues to climb, not everyone understands the risks that come with connecting to public Wi-Fi or even offering Wi-Fi to employees, visitors and guests. Download the resource kit to make sure your safe wherever business takes you!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now