Link to home
Start Free TrialLog in
Avatar of Sleestack90
Sleestack90

asked on

.\script.ps1 does not work correct

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?
Avatar of Chris Dent
Chris Dent
Flag of United Kingdom of Great Britain and Northern Ireland image


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
Avatar of Sleestack90
Sleestack90

ASKER

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"
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
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.
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'"
@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

I thought you were calling this script from PowerShell itself?

Chris
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:\>
@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.

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"
@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 :)
@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
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.
ASKER CERTIFIED SOLUTION
Avatar of endital1097
endital1097
Flag of United States of America 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
SOLUTION
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