Run Powershell as administrator from command line?

elwayisgod
elwayisgod used Ask the Experts™
on
Is there a way to execute Powershell 'as administrator' from command line?  When I open a command window on the Server and select 'Run As Administrator' then run my main script that then calls this script it works great.  If I try and schedule the main script in Task Scheduler this part doesn't run.  Thus was thinking a permissions issue.  Not really sure as nothing writes to my log file......

@echo off

if not exist %ARCHIVEDIR%\PBCS\Backups\%Timestamp% mkdir %ARCHIVEDIR%\PBCS\Backups\%Timestamp%

setlocal
set Source=%FILEDIR%\Backup_Data\%AppName%\%AppName%_FY*_Export_SUI.txt
set Target=%ARCHIVEDIR%\PBCS\Backups\%Timestamp%\%AppName%_Data.zip
powershell.exe -Command "Get-ChildItem '%Source%' | Compress-Archive -DestinationPath '%Target%' -Force"


exit /b

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Sam JacobsDirector of Technology Development, IPM

Commented:
You can add this snippet to your script to make sure that it is being run in an elevated session:
# make sure script is running elevated
If (-NOT ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator"))
{
    Write-Warning "This script needs to be run elevated.`nPlease re-run this script as an Administrator!"
    Break
}

Open in new window

Author

Commented:
Not a PS person at all, where do I put it?  Above?

Author

Commented:
Like so?

@echo off

if not exist %ARCHIVEDIR%\PBCS\Backups\%Timestamp% mkdir %ARCHIVEDIR%\PBCS\Backups\%Timestamp%

setlocal
set Source=%FILEDIR%\Backup_Data\%AppName%\%AppName%_FY*_Export_SUI.txt
set Target=%ARCHIVEDIR%\PBCS\Backups\%Timestamp%\%AppName%_Data.zip


# make sure script is running elevated
If (-NOT ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator"))
{
    Write-Warning "This script needs to be run elevated.`nPlease re-run this script as an Administrator!"
    Break
}


powershell.exe -Command "Get-ChildItem '%Source%' | Compress-Archive -DestinationPath '%Target%' -Force"



exit /b

Open in new window

Introduction to Web Design

Develop a strong foundation and understanding of web design by learning HTML, CSS, and additional tools to help you develop your own website.

Sam JacobsDirector of Technology Development, IPM

Commented:
Sorry ... I answered your question without looking at your script - which is a command script - not a PowerShell script, so my snippet won't help for that.
Philip ElderTechnical Architect - HA/Compute/Storage

Commented:
In Task Scheduler make sure to tick the options: Run while user not logged on, and Run elevated. The task will prompt for credentials.

That should do it.

Author

Commented:
Those are checked in Task Scheduler.  Weird.

Author

Commented:
TaskScheduler.jpg

Author

Commented:
Let me do some more troubleshooting.  Problem is it's not behaving same when running from command prompt so seeing the error is impossible as I pipe to log file via >, but nothing shows up...

Author

Commented:
It's resolved.  Wasn't administrator privilege issue.  Sorry for confusion.  Issue was the files in the source directory for the powershell script were not there.  Had to adjust the 'Start In' directory of the Task Scheduler to get the tool to download to the source directory so the .zip could actually work.
See above

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial