Execute PowerShell (.PS1) scripts not working

Started by jpbreese, August 25, 2014, 12:47:30 PM

Previous topic - Next topic

jpbreese

I have three PowerShell scripts that I am trying to launch from a WinBatch script.  The command works fine on Windows 7 (x86), but when I execute them from Windows 7 (x64) or Windows 8.1 (x64) I receive "access denied" errors.  From all OS platforms I can open a command prompt and type the command in and they work.  I only receive the errors when the PowerShell scripts are executed from the WinBatch script.

What is the proper way of executing the PowerShell script?  I'm including one of the PowerShell scripts with this post.  I am trying to manage Microsoft's Application Virtualization (App-V) 5.0 applications. 
Code (winbatch) Select

; ******************************************************************************************
; *****          App-V 5 Clean                                                         *****
; ******************************************************************************************
:CLEAN_5


; ******************************************************************************************
; *****          Remove all App-V 5.0 applications                                     *****
; ******************************************************************************************
:5_DELETE_APPS
;App-V 5.0 Deep Clean Banner One
BoxesUp("300,300,700,800", @NORMAL)
BoxCaption(1, "APP-V Deep Clean Process")
BoxBitMap(1, "0,0,1000,1000", "%SourceDir%\WinBatchPictures\DC_5_Step_1.bmp", 4)

RunWait ("PowerShell.exe", '-File "%SourceDir%\Scripts\01_RemoveApps.ps1"')
BoxDestroy (1)



; ******************************************************************************************
; *****          Delete Residual Directories And Clean The Registry                    *****
; ******************************************************************************************
:5_DELETE_FILES_REGISTRY
;App-V 5.0 Deep Clean Banner Two
BoxesUp("300,300,700,800", @NORMAL)
BoxCaption(1, "APP-V Deep Clean Process")
BoxBitMap(1, "0,0,1000,1000", "%SourceDir%\WinBatchPictures\DC_5_Step_2.bmp", 4)

;Delete residual directories and clean the registry
RunWait ("PowerShell.exe", '-File "%SourceDir%\Scripts\02_CleanUpCache.ps1"')
BoxDestroy (1)




; ******************************************************************************************
; *****          Sync App-V Server                                                     *****
; ******************************************************************************************
;App-V 5.0 Deep Clean Banner Three
BoxesUp("300,300,700,800", @NORMAL)
BoxCaption(1, "APP-V Deep Clean Process")
BoxBitMap(1, "0,0,1000,1000", "%SourceDir%\WinBatchPictures\DC_5_Step_3.bmp", 4)

;Sync the App-V server to restore App-V 5.0 applications.
RunWait ("PowerShell.exe", '-File "%SourceDir%\Scripts\03_SyncServer.ps1"')
BoxDestroy (1)




; ******************************************************************************************
; *****          App-V 5 Clean Completed                                               *****
; ******************************************************************************************
;Completion Message
BoxesUp("300,300,700,800", @NORMAL)
BoxCaption(1, "APP-V Deep Clean Process")
BoxBitMap(1, "0,0,1000,1000", "%SourceDir%\WinBatchPictures\DC_5_Complete.bmp", 4)

TimeDelay (5)
BoxDestroy (1)



:THATS_ALL_FOLKS
;Exit Process
EXIT

td

It would have been more helpful to state exactly where you are getting an access deny error. 

That aside, running Powershell 'commands' from WinBatch is relatively state forward:
http://techsupt.winbatch.com/webcgi/webbatch.exe?techsupt/nftechsupt.web+WinBatch/How~To+Run~PowerShell~Command.txt

Powershell can also be accessed via WinBatch CLR hosting using the OptionClr* functions. An example can be found here:
http://techsupt.winbatch.com/webcgi/webbatch.exe?techsupt/nftechsupt.web+WinBatch/dotNet/System_Management/Automation/PowerShell+Process~a~PowerShell~Script~File.txt

The Immediate cause of your problem may have something to do with file redirection on 64-bit versions of Windows or a UAC issue. The simplest way to check for file redirection problems is to run your script using 64-bit WinBatch.  If the script works then you know the problem.  Note that you can get around file redirection without having to use 64-bit WinBatch but that is the easiest way to diagnose the problem without having to write any addition script code.

You can find more information about file redirection on 64-bit systems here:
http://techsupt.winbatch.com/webcgi/webbatch.exe?techsupt/nftechsupt.web+WinBatch/64-bit+File~Redirection.txt

If file redirection isn't the issue and your different systems have different UAC settings then UAC may be causing your problem.   More information about working around UAC issues can be found here:
http://techsupt.winbatch.com/webcgi/webbatch.exe?techsupt/nftechsupt.web+~+Run~Scripts~on~Windows~Vista~and~Newer.txt

Given an access deny error and not having any additional information, UAC would get the nod as the best guess.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

SpenceWorks

Fyi, I discovered in August 2010 that placing the following statement at the beginning of my 32-bit WinBatch scripts will insure script compatibility on both 32-bit and 64-bit Windows platforms, eliminating most if not all Registry redirection-related errors... ;-)

;*** Change the default Registry view to 32-bit for 64-bit Windows 08-04-10:
RegOpenFlags (1)

td

The above thread was about file redirection and not registry redirection.  Setting WinBath's registry function to the 32-bit view can cause as many problems as it can fix.  Which view you uses depends on what exactly it is you are trying to do.  You can find additional information on registry redirection on the Tech Database.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade