What I am trying to achieve is: I have a huge script that does a lot of functions for myPC. All of those function prefer to not have admin privileges when executed for eg running application "outlook" etc. Only one function (2 commands) in the script requires admin privileges. Is it possible to run just those two commands as new powershell Adminwindow and close?
$TouchScreen = Get-PnpDevice -FriendlyName 'HID-Compliant Touch Screen'
if ( $TouchScreen.Status -eq 'OK' ) { $TouchScreen | Disable-PnpDevice -confirm:$false } else { $TouchScreen | Enable-PnpDevice -confirm:$false }
I have tried couple of things but to no avail. The -command of Start-Process powershell -Verb runAs -ArgumentList "-noexit","-command XXX" does not accept variables. Am I doing something wrong?
Some attempts:
Start-Process powershell -Verb runAs -ArgumentList "-noexit","-command { [string]$TouchScreen = Get-PnpDevice -FriendlyName 'HID-Compliant Touch Screen'; write-host 'testing $TouchScreen'; if ( $TouchScreen.Status -eq 'OK' ) { $TouchScreen | Disable-PnpDevice -confirm:$false } else { $TouchScreen | Enable-PnpDevice -confirm:$false }}"
Any and all help will be appreciated
PS: The following lines on standalone script works. Because it just runs the current script as admin. But as I mentioned earlier not an option for me. Furthermore, not looking to call other scripts in here, the reason being I am going to use PS2EXE and that additional script will be an issue.
If (-not ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) {
$Arguments = "& '" + $MyInvocation.mycommand.definition +"'"
Start-Process powershell -Verb runAs -ArgumentList $Arguments
Break
}
$TouchScreen = Get-PnpDevice -FriendlyName 'HID-Compliant Touch Screen'
if ( $TouchScreen.Status -eq 'OK' ) { $TouchScreen | Disable-PnpDevice -confirm:$false } else { $TouchScreen | Enable-PnpDevice -confirm:$false }
Solved it this way
$MyVar = {"$TouchScreen = Get-PnpDevice -FriendlyName 'HID-Compliant Touch Screen'"; "if ( $TouchScreen.Status -eq 'OK' ) { $TouchScreen | Disable-PnpDevice -confirm:$false } else { $TouchScreen | Enable-PnpDevice -confirm:$false }";}
Start-Process powershell -Verb runas -ArgumentList "-noexit",$MyVar
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.