rakkad
asked on
Powershell script running in a dos batch script
Hi
I have created a powershell script that works within powershell, but I need to run the script in command prompt in a dos batch script. Can anyone help please ?
Thanks
I have created a powershell script that works within powershell, but I need to run the script in command prompt in a dos batch script. Can anyone help please ?
Thanks
simply, you can call it within your batch script by adding
ref:
https://poshoholic.com/2007/09/27/invoking-a-powershell-script-from-cmdexe-or-start-run/
powershell -noexit & “C:\script.ps1”
ref:
https://poshoholic.com/2007/09/27/invoking-a-powershell-script-from-cmdexe-or-start-run/
Or you can combine them into one file
@Echo Off
PowerShell.exe -Command "Invoke-Expression -Command ((Get-Content -Path '%~f0' | Select-Object -Skip 3) -join [environment]::NewLine)"
Exit /b %ErrorLevel%
## Powershell starts here
Write-Host "Hello World"
ASKER
How can I run powershell as an administrator in a dos batch command ?
Thanks
Thanks
You can't - that would circumvent any measures introduced to not allow exactly for that. We can check if the batch script runs as admin though:
whoami /groups | find "S-1-16-12288" >nul || (echo *** Not running elevated - STOP *** & pause & exit /b 1)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
"You can't" - because you do not use UAC with less than at least confirmation prompt :D.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
In your batch file simply put a line in the cmd script calling the PowerShell like this:
Open in new window
Usually I like to create a wrapper script file with the same name as the PowerShell script so that I can run it and log the output to a file similar to below:
Open in new window