启动 - 处理提升的 powershell 并发送运行提升的脚本时要使用的变量

启动 - 处理提升的 powershell 并发送运行提升的脚本时要使用的变量

当执行此脚本(非提升)时,该脚本会询问密码并映射 onedrive,然后它自动启动 powershell(提升)并再次询问 bitlocker 密码。

如果对 onedrive 和 bitlocker 使用单一密码,如何使其仅需要输入一次密码?或者是否可以将变量(包含用户输入的密码)传递给新的提升的 powershell 以用于脚本来解锁 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"

相关内容