Script PowerShell en mode admin
Bonjour, je ne trouve pas la solution à mon problème n'étant pas un expert du PowerShell.
Je cherche a récupérer les clés Bitlocker des utilisateurs sur mon domaine de manière automatique. Pour ce faire, j'ai créer une GPO qui lance un script en BAT, qui permet juste de lancer un powershell en mode admin.
Le problème étant que certains utilisateurs sont utilisateurs de leurs machines et doivent donc entrer un utilisateur / mot de passe d'un admin pour permettre de lancer le script.
Existe-t-il une solution a ce problème ? Quitte même a spécifier un utilisateur Admin et son mot de passe en claire dans le script, les utilisateurs ne sont pas assez expérimentés pour comprendre comment le retrouver.
A toute fin utile je laisse le script ci-dessous :
Fichier BAT appelant le powershell
Code:
1 2
| @echo off
powershell Start-Process -FilePath powershell.exe -Verb Runas -ArgumentList '-ExecutionPolicy bypass', '\\G1-DC01\_Packages$\test.ps1' |
Powershell :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
|
[CmdletBinding()]
param
(
[switch]$ActiveDirectory
)
Function Get-BitLockerRecoveryKeyId {
[cmdletBinding()]
Param (
[Parameter(Mandatory = $false, ValueFromPipeLine = $false)][ValidateSet("Alltypes", "TPM", "ExternalKey", "NumericPassword", "TPMAndPin", "TPMAndStartUpdKey", "TPMAndPinAndStartUpKey", "PublicKey", "PassPhrase", "TpmCertificate", "SID")]$KeyProtectorType
)
$BitLocker = Get-WmiObject -Namespace "Root\cimv2\Security\MicrosoftVolumeEncryption" -Class "Win32_EncryptableVolume"
switch ($KeyProtectorType) {
("Alltypes") { $Value = "0" }
("TPM") { $Value = "1" }
("ExternalKey") { $Value = "2" }
("NumericPassword") { $Value = "3" }
("TPMAndPin") { $Value = "4" }
("TPMAndStartUpdKey") { $Value = "5" }
("TPMAndPinAndStartUpKey") { $Value = "6" }
("PublicKey") { $Value = "7" }
("PassPhrase") { $Value = "8" }
("TpmCertificate") { $Value = "9" }
("SID") { $Value = "10" }
default { $Value = "0" }
}
$Ids = $BitLocker.GetKeyProtectors($Value).volumekeyprotectorID
return $ids
}
function Get-BitlockerPassword {
[CmdletBinding()][OutputType([string])]
param
(
[ValidateNotNullOrEmpty()][string]$ProtectorID
)
$Password = manage-bde -protectors -get ($env:ProgramFiles).split("\")[0] -id $ProtectorID | Where-Object { $_.trim() -ne "" }
$Password = $Password[$Password.Length - 1].Trim()
Return $Password
}
function Publish-RecoveryPasswordToActiveDirectory {
[CmdletBinding()]
param
(
[ValidateNotNullOrEmpty()][string]$BitlockerID
)
$ManageBDE = $env:windir + "\System32\manage-bde.exe"
$Switches = "-protectors -adbackup" + [char]32 + ($env:ProgramFiles).split("\")[0] + [char]32 + "-id" + [char]32 + $BitlockerID
Invoke-EXE -DisplayName "Backup Recovery Key to AD" -Executable $ManageBDE -Switches $Switches
}
function Invoke-EXE {
[CmdletBinding()]
param
(
[String]$DisplayName,
[String]$Executable,
[String]$Switches
)
Write-Host "Uploading"$DisplayName"....." -NoNewline
If ((Test-Path $Executable) -eq $true) {
$ErrCode = (Start-Process -FilePath $Executable -ArgumentList $Switches -Wait -Passthru).ExitCode
} else {
$ErrCode = 1
}
If (($ErrCode -eq 0) -or ($ErrCode -eq 3010)) {
Write-Host "Success" -ForegroundColor Yellow
} else {
Write-Host "Failed with error code "$ErrCode -ForegroundColor Red
}
}
Clear-Host
#Retrieve numerical password ID
[string]$BitlockerID = Get-BitLockerRecoveryKeyId -KeyProtectorType NumericPassword
#Retrieve Bitlocker recovery password from the local system
[string]$BitlockerPassword = Get-BitlockerPassword -ProtectorID $BitlockerID
Publish-RecoveryPasswordToActiveDirectory -BitlockerID $BitlockerID |