.\script.ps1 does not work correct

Sleestack90
Sleestack90 used Ask the Experts™
on
I have a batch file setup to write some mail statics to a csv file...pretty straight forward.

Scheduled Task->BatchFile->Script-CSV file to folder on the network....A little sorting in order to place the information in another format and done.

My problem is that when I get into the Exchange 2010 Management Shell and I go to the scripts directory and do a:

.\script.ps1

It just opens the script in notepad and doesn't execute. I just need that part to execute.

Any ideas why the '.\' isn't working for me?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Chris DentPowerShell Developer
Top Expert 2010

Commented:

Daft question, but it's not hiding extensions from you is it? It doesn't have a little .txt on the end?

Otherwise you could try:

& '.\script.ps1'

Although it should work as you've been using it above.

Chris

Author

Commented:
i checked the extentions just to make sure. also a .ps1 file has a different icon that a .txt file, so you can tell when you actually go look at them side by side.

Good thought, but thats not it.
under the actions tab for the scheduled task i have

Program:
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
Add arguments:
-noexit -command ". 'C:\Program Files\Microsoft\Exchange Server\V14\bin\RemoteExchange.ps1'; Connect-ExchangeServer -auto" -Command "C:\Data\Scripts\get-ExchangeDatabaseSizes.ps1"
CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

Chris DentPowerShell Developer
Top Expert 2010

Commented:
I thought that would be far too easy :)

No spaces in the path or script name at all are there?

I'm sure I've seen someone else with this problem, but I can't quite remember the circumstances.

Out of interest, PowerShell 1 or 2?

Chris

Author

Commented:
right, I have this all worked out as well....but my issue comes in when you get to the last part of your task script:

"C:\Data\Scripts\get-ExchangeDatabaseSizes.ps1"

For whatever reason my own "C:\location\scripts\script.ps1" will not execute, it opens up a notepad file....with the command inside of it.

Author

Commented:
Exchange 2010 runs Power Shell 2 by default I believe.

Here is the script i am trying to run:

PowerShell.exe -command ". 'C:\Program Files\Microsoft\Exchange Server\V14\bin\RemoteExchange.ps1'; Connect-ExchangeServer -auto; 'C:\Exchange 2010\scripts\MonthlyMailboxReport.ps1'"

Author

Commented:
@endital1097:

Is the  -Command after the -auto significant?
check for file associations
right-click a ps1 file and choose open with
yes, it won't launch a script without it
Chris DentPowerShell Developer
Top Expert 2010

Commented:

I thought you were calling this script from PowerShell itself?

Chris

Author

Commented:
I tired to add the -Command a couple of ways....no dice...have a look:

C:\>PowerShell.exe -command ". 'C:\Program Files\Microsoft\Exchange Server\V14\b
in\RemoteExchange.ps1'; Connect-ExchangeServer -auto -Command 'C:\Exchange 2010\
scripts\MonthlyMailboxReport.ps1'"

         Welcome to the Exchange Management Shell!

 Full list of cmdlets:          get-command
 Only Exchange cmdlets:         get-excommand
 Cmdlets for a specific role:   get-help -role *UM* or *Mailbox*
 Get general help:              help
 Get help for a cmdlet:         help <cmdlet-name> or <cmdlet-name> -?
 Show quick reference guide:    quickref
 Exchange team blog:            get-exblog
 Show full output for a cmd:    <cmd> | format-list

Tip of the day #10:

Cmdlets that end in "Config" manage singleton configuration, either one per server or organization. For these tasks, you
 don't have to specify an identity because there is only one instance of the configuration. You may have to specify the
Server parameter if the configuration is per server.

VERBOSE: Connecting to EXCHANGESERVER.DOMAIN.local
VERBOSE: Connected to EXCHANGESERVER.DOMAIN.local.

C:\>PowerShell.exe -command ". 'C:\Program Files\Microsoft\Exchange Server\V14\bin\RemoteExchange.ps1'; Connect-Exchange
Server -auto -Command; 'C:\Exchange 2010\scripts\MonthlyMailboxReport.ps1'"

         Welcome to the Exchange Management Shell!

 Full list of cmdlets:          get-command
 Only Exchange cmdlets:         get-excommand
 Cmdlets for a specific role:   get-help -role *UM* or *Mailbox*
 Get general help:              help
 Get help for a cmdlet:         help <cmdlet-name> or <cmdlet-name> -?
 Show quick reference guide:    quickref
 Exchange team blog:            get-exblog
 Show full output for a cmd:    <cmd> | format-list

Tip of the day #18:

Get all Win32 WMI information, such as Perfmon counters and local computer configurations. For example, type:

 Get-WMIObject Win32_PerfRawData_PerfOS_Memory

VERBOSE: Connecting to EXCHANGESERVER.DOMAIN.local
VERBOSE: Connected to EXCHANGESERVER.DOMAIN.local.
C:\Exchange 2010\scripts\MonthlyMailboxReport.ps1

C:\>

Author

Commented:
@Chris-Dent:

I wrote a script to manage the task that I wanted completed...which is get a few things from exchange. I ran this in power shell using the commands. It all works.

I then wrote a .cmd file to call the script.ps1 file. The batch file calls the script fine, but the script doesn't run, it opens notepad.

So overview:

The Batch file is fine.
The Commands in the script are fine.

Problem: I can not run the script.ps1 instead of just running the script commands in power shell.
Chris DentPowerShell Developer
Top Expert 2010

Commented:

If a batch file runs "script.ps1" then it will open in Notepad. ps1 scripts don't execute.

If you're at the PowerShell prompt then you should be able to execute the script. This is where you see it open in Notepad? That's the impression I got.

You don't actually need a cmd file at all, you can call PowerShell scripts directly like this:

PowerShell.exe c:\wherever\script.ps1

Chris
PowerShell.exe -command ". 'C:\Program Files\Microsoft\Exchange Server\V14\bin\RemoteExchange.ps1'; Connect-ExchangeServer -auto; C:\Data\Scripts\test.ps1"

Author

Commented:
@chris: understood

You are correct:  the script does not execute from power shell, that is the problem.
comment 33126506 has been tested and should work in a bat file :)

Author

Commented:
@endital1097: yes that is what I am currently using.

The problem is that the script won't execute in powershell.  When you name something file.ps1 (using the .ps1 extention), my understanding is that you can simply call that from in power shell from the directory where the .ps1 file is located and it should execute the commands in the script.
powershell is not going to start where your batch file is, therefore it is best to include the full path to the ps1 file

Author

Commented:
Here is the result of trying to run the script from power shell:

[PS] C:\exchange 2010\scripts>MonthlyMailboxReport.ps1
The term 'MonthlyMailboxReport.ps1' is not recognized as the name of a cmdlet, function, script file, or operable progr
am. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:25
+ MonthlyMailboxReport.ps1 <<<<
    + CategoryInfo          : ObjectNotFound: (MonthlyMailboxReport.ps1:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException


Suggestion [3,General]: The command MonthlyMailboxReport.ps1 was not found, but does exist in the current location. Wind
ows PowerShell doesn't load commands from the current location by default. If you trust this command, instead type ".\Mo
nthlyMailboxReport.ps1". See "get-help about_Command_Precedence" for more details.
[PS] C:\exchange 2010\scripts>



So I tired dropping the script in the Root of C:, same result.
from within powershell if you are in the directory it should be:
.\MonthlyMailboxReport.ps1

if you are in any other directory it should be:
c:\path\MonthlyMailboxReport.ps1
Chris DentPowerShell Developer
Top Expert 2010
Commented:

That's why you need the .\, you included that in the original though? It must be moderately accurate for it to open it in Notepad.

Either it needs the full path or it needs .\ or it doesn't execute.

Apologies for the circles we seem to be travelling in. I'm trying to think what might cause the file to open instead of executing when called from PS and I'm coming up blank.

Could you run "Get-ExecutionPolicy"? And perhaps we can try a very simple test script?

Chris

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial