1

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: "https://d.docs.live.net/EXAMPLE-CID" /user:[email protected] $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
  • 1
    Why not run everything elevated, and ask the password in that session?
    – LPChip
    Jan 1, 2022 at 14:06
  • 1
    mapping network drive as admin will not allow it to be shown in windows explorer @LPChip
    – proseosoc
    Jan 1, 2022 at 17:16
  • 1
    True, but there's nothing preventing you from mapping the network drive as administrator as well. If I want to install a program to one of my network shares, If I don't have group policy configured to push the network shares to the administrative session, I have to make the same network connection as administrator in a command prompt run as administrator, then keep that cmd instance open and perform the install. So in theory, you can do the same. Just make the connection without entering login credentials and it should re-establish the connection.
    – LPChip
    Jan 1, 2022 at 18:20

0

You must log in to answer this question.

Browse other questions tagged .