S
S
Steve Road2020-04-09 08:21:59
PowerShell
Steve Road, 2020-04-09 08:21:59

How to run PS script via cmd?

There is a script.

$username = 'логин администратора'
$password = 'пароль'
$secstr = New-Object -TypeName System.Security.SecureString
$password.ToCharArray() | ForEach-Object {$secstr.AppendChar($_)}
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $secstr
Start-Process -credential $cred -filepath '\\ad\TISert\SysInfo Detector Pro\SysInfoDetector.exe'


if you run it from PowerShell, then everything works, the program starts. But when you run this script from a batch file...

@echo off
SetLocal EnableExtensions

For /F "UseBackQ delims=" %%a in (`
  %SystemRoot%\System32\WindowsPowerShell\v1.0\PowerShell.exe -ExecutionPolicy ByPass -command ". 'c:\zzz\scr.ps1'"
`) do (
  echo %%a
)
pause


This problem appears
5e8eb02e23a4d069887136.png

Maybe someone faced similar?
And all this machination is needed so that when users start the system, a program with admin rights is launched.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
P
pavelsha, 2020-04-09
@pavelsha

Use a scheduler. Logging in to the system and running as the specified user are its regular features.
Well, specifying the Administrator's login and password in the clear... This is "best practice"...

E
Eugene, 2020-04-14
@zloy_zaya

Try the part where you run the script like this:
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -NonInteractive -command ". 'c:\zzz\scr.ps1'

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question