Run script as local administrator

Jun 21, 2011 at 3:02 PM

I have 2 questions maybe someone can help out.

1. How do you impersonate a local administrator user while using the HyperV modules?

 

I have to load the hyper V module with a batch file like so...

start %windir%\System32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Bypass -noExit -Command "Import-Module '%ProgramFiles%\modules\Hyperv'"

2. After importing the HyperV module i would like to create a snapshot

start %windir%\System32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Bypass -noExit -Command "Import-Module '%ProgramFiles%\modules\Hyperv'" new-VMSnapshot

does not work. I would like to run it all in a script.

Jul 20, 2011 at 8:40 AM

powershell -c "set-executionpolicy bypass -force"
if not exist "C:\windows\System32\WindowsPowerShell\v1.0\Modules\HyperV" (robocopy D:\HyperV C:\windows\System32\WindowsPowerShell\v1.0\Modules\HyperV *.* & PowerShell.exe -Command "& {echo 'Import-Module HyperV.psd1' >> $profile.AllUsersAllHosts}" )

powershell -c "get-vm -running"

[System.Diagnostics.ProcessStartInfo] $psi = (new-object System.Diagnostics.ProcessStartInfo)

$psi.FileName = "powershell.exe"

$psi.Verb = "runas"
$psi.WorkingDirectory = $pwd

$psi.Arguments = "new-VMSnapshot"

$p = [System.Diagnostics.Process]::Start($psi)

#wait until the process exits
$p.WaitForExit()