Start-Process elevated powershell & send a variable to be used when running elevated script

When this scirpt is executed(non-elevated) this script asks password and maps onedrive and then it automatically launch powershell(elevated) and asks again password for bitlocker.

If single password is used for onedrive and bitlocker, how to make it to require password once? Or is it possible to pass variable(contains user input password) to new elevated powershell to be used in script to unlock bitlocker?

#
# (FIRST SET EXECUTION POLICY WITH ELEVATED POWERSHELL) Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Unrestricted
#
param([switch]$Elevated)
function Test-Admin { $currentUser = New-Object Security.Principal.WindowsPrincipal $([Security.Principal.WindowsIdentity]::GetCurrent()) $currentUser.IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)
}
if ((Test-Admin) -eq $false) { if ($elevated) { 'tried to elevate, did not work' } else { ### START - CODE FOR "NON-ELEVATED POWERSHELL" $pwd = Read-Host 'Enter PW:' -AsSecureString $BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($pwd) $UnsecurePassword = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR) net use O: "" /user: $UnsecurePassword /p:no ### END - CODE FOR "NON-ELEVATED POWERSHELL" # RUN "ELEVATED POWERSHELL" Start-Process powershell.exe -Verb RunAs -ArgumentList ('-noprofile -noexit -file "{0}" -elevated' -f ($myinvocation.MyCommand.Definition)) } exit
}
### START - CODE FOR "ELEVATED POWERSHELL"
$pwd = Read-Host 'Enter PW:' -AsSecureString
Unlock-BitLocker -MountPoint "D:" -Password $pwd
exit
### END - CODE FOR "ELEVATED POWERSHELL"
3 Reset to default

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like