Hi,
I have a script that I'm trying to run from a batch file so I can schedule it to run every morning.
If I execute the script in PS, it works find and creates a csv file and email it to me, but when I run it from a batch file it opens PS but it doesn't fetch data and PS closes.
I have other PS script that run fine in a batch file and creates a report that email it to me without any problems.
@Echo offcd \tempC:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -noexit '.\Get-MBRep.ps1 -all -filename AFTRAMBRep.csv'
How can get PS to stay open and Execute the script?
Thanks
Chang
There are many variations on doing this, but it may be simple as relocating the period and quotes in the file and arguments separately:
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -noexit . "\Get-MBRep.ps1" "-all -filename AFTRAMBRep.csv'
or
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -noexit ". '\Get-MBRep.ps1' '-all -filename AFTRAMBRep.csv'"
You would quote the filename separately in case you decide to use filenames in the future with spaces
For more examples, go here:
http://stackoverflow.com/questions/6359618/pass-parameter-from-batch-file-to-the-powershell-script
Thanks Richard for your reply.
I tried both ways and other variants of the same but they didn't work on fetching the data. This script runs a report of all exchange 2010 mailboxes and creates a csv file. It runs fine when is ran in Powershell but I cannot get it to create a report when I run it in a batch file. The command runs but it closes PS right away. I also tried the one on the site stackoverflow.com and still the same results.
Is the script signed? If so, you may need to set ExecutionPolicy on the command line.
Try adding "; exit $LASTEXITCODE" after the parameters to see if you can figure out what is failing:
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -noexit . "\Get-MBRep.ps1" "-all -filename AFTRAMBRep.csv; exit $LASTEXITCODE"
Richard,
Thanks again for your help, the problem was with the PS script, it was running fine in the Exchange PS but then I tried to run it in Windows PS and I didn't get any data. I added the Exchange Snapin in the script to call Exchange and it's working fine now.