• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 362
  • 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

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

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