Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Running power shell via bat file

Status
Not open for further replies.

iwahyudi

Programmer
Nov 26, 2013
7
ID
Need help,

I have power shell script and want to running in vfp 9.0, here is content of power shell file

Content of disableprint.ps1:
$username = 'test'
$password = 'test123'
$cred = New-Object System.Management.Automation.PSCredential -ArgumentList @($username,(ConvertTo-SecureString -String $password -AsPlainText -Force))
Invoke-Command -FilePath D:\data\datanya\disableprint0.ps1 -Credential $cred -ComputerName $env:computername

Here is disableprint0.ps1 content:
net start "Print Spooler"

Content of testps2.bat
powershell -command .\disableprint.ps1

How to handle it in vfp 9.0 since I used bat file and type in vfp command not running

I tried as below
DECLARE INTEGER ShellExecute IN SHELL32.DLL ;
INTEGER nWinHandle, ;
STRING cAction, ;
STRING cFileName, ;
STRING cParameters, ;
STRING cDirectory, ;
INTEGER nShowWindow

cAction = "Open"
cFileName = "d:\data\datanya\testps2.bat"
ShellExecute( 0, cAction, cFileName, "", "", 2 )

Please help how to handle those command?

Thank you
 
If I click testps2.bat manually via root explorer can running well
 
ShellExecute behaviour is OS behaviour. This has little to do with VFP, in general what you do as user in the shell cuases other elevations of processes you start than programmatically starting processes eg via RUN or via ShellExecute. They typically run in low elevations, even if the VFP9.exe or your compiled EXE is started by you. System Policies can also limit the rights on executing power shell scripts.

OTOH I don't know a way to overcome this. I can only hint you to investigate in the direction of execution privileges and how Windows elevation of process privileges works. If any process spawned by anything you start would get admin privileges, that would compromise secutrity as it enables even processes you didn't start knowingly yourself would inherit your admin privileges. This was the case before vista but it's not working that way anymore. So you might need to dig into CreateProcessEx aPI and all the structs you need to prepare to let the bat file run elevated.

Bye, Olaf.
 
You can try cAction = "runas" instead of cAction = "Open" for admin privileges.

Respectfully,
Vilhelm-Ion Praisach
Resita, Romania
 
Give it a try. I found this...


ss64.com said:
The runas verb is undocumented but can be used to elevate permissions. When a script is run with elevated permissions several aspects of the user environment may change: The current directory, the current TEMP folder and any mapped drives will be disconnected.

[highlight #FCE94F]runas will fail if you are running in WOW64[/highlight] (a 32 bit process on 64 bit windows) for example %systemroot%\syswow64\cmd.exe ...

The elevation will not work for any user, though, you have to start your EXE with an elevated account anyway.

Administrative things should not be done via an application, if you want your application to be an administrative application you should embed a manifest enforcing an elevated user to start it in the first place, as shown here: with requestedExecutionLevel level="requireAdministrator".

I can only guess the finally called power shell script "disableprint0.ps1" should disable printing for the user, you should rather do that on the administration level, as it seems odd your users will need to be admins to deny themselves from printing via this application. There should be a much more straight way to do this.

Bye, Olaf.
 
Hello Olaf,
Thank you for your responded, my current windows is windows 8. I avoid use run as "administrator" since need user access only. but why I can run file testps2.bat manually via explorer and not need login as administrator

Note:
testps2.bat = powershell -command .\disableprint.ps1

Hello DSummZZZ,
I will try your a way
 
@ DSummZZZ
I think your way similar with mine and results is same

cAction = "Open"
cFileName = "d:\data\datanya\testps2.bat"
ShellExecute( 0, cAction, cFileName, "", "", 2 )
 
net start typically needs administrative privileges. Are you sure your account is not in the administrator group?`

Bye, Olaf.
 
If net start "Print Spooler" is what you want to do, then the solution is to not do it, instead set up the service tó be started automatic at windows start. It should run with a system account.

Bye, Olaf.
 
I think your way similar with mine and results is same
Yes, similar, but not the same. My way is telling ShellExecute to execute the .bat where it actually resides rather than try to find it down a path. Which I know works. But if you're not running ShellExecute or a command prompt with admin privileges, 'net start spooler' won't work.
Also, is "d:" a mapped drive or is it really the 'd' drive? Either version will either need to be able to find the drive either through a retained mapping or an actual UNC like "\\server\d\data".


-Dave Summers-
[cheers]
Even more Fox stuff at:
 
If you can't set the service to start automatic, give a try to starting the service this way:

Code:
o = CreateObject("shell.application")
o.ServiceStart("Print Spooler")

Anyway, in the Management Console you cannot only set up a service to start automatic, but also to restart in case of an error. See properties of the service.

Bye, Olaf.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top