当用户未作为Powershell中的系统帐户登录并通过SCCM运行时,BCD编辑导出失败
在尝试将VHD部署到系统时,我遇到一个问题,即通过SCCM将powershell脚本作为系统帐户运行 脚本尝试执行bcdedit/export-但是,当我以自己的身份运行它或使用psexec/I/s cmd,然后通过它运行powershell时,当我使用自己的帐户登录到系统时,它工作正常 当通过SCCM运行脚本时,它会停止脚本并抛出日志输出注释中的自定义错误,无论我是否登录,这使我相信系统无法将bcd导出到文件 脚本如下所示:当用户未作为Powershell中的系统帐户登录并通过SCCM运行时,BCD编辑导出失败,powershell,export,sccm,bcd,bcdedit,Powershell,Export,Sccm,Bcd,Bcdedit,在尝试将VHD部署到系统时,我遇到一个问题,即通过SCCM将powershell脚本作为系统帐户运行 脚本尝试执行bcdedit/export-但是,当我以自己的身份运行它或使用psexec/I/s cmd,然后通过它运行powershell时,当我使用自己的帐户登录到系统时,它工作正常 当通过SCCM运行脚本时,它会停止脚本并抛出日志输出注释中的自定义错误,无论我是否登录,这使我相信系统无法将bcd导出到文件 脚本如下所示: #Set Package Details $PackageName
#Set Package Details
$PackageName = "Certiport-Office2013_01.30_86a"
# Set Logging Location
$Global:LogFile = "C:\Logs\$PackageName.log"
$Global:Returnval = 0
##Set Diskpart Commands
$DiskpartAttachVDisk = "SELECT VDISK FILE=D:\Certiport\$PackageName.vhd
ATTACH VDISK"
$DiskpartChangeLetter ="SELECT VDISK FILE=D:\Certiport\$PackageName.vhd
SELECT PARTITION 1
ASSIGN LETTER=V
EXIT"
$DiskpartDetachVDisk = "SELECT VDISK FILE=D:\Certiport\$PackageName.vhd
DETACH VDISK
EXIT"
# Set PKGLocation
$Global:hostinvocation = (Get-Variable MyInvocation).Value
$Global:PkgLocation = if($hostinvocation.MyCommand.path -ne $null) { Split-Path $hostinvocation.MyCommand.path }
else { (Get-Location).Path }
######################
## LOGGING FUNCTION ##
##Writes to the log file in C:\Logs
$OSName = (Get-WmiObject -class Win32_OperatingSystem).Caption.trim()
Function Log {
Param ([string]$LogEntry)
$TimeStamp = Get-Date -Format "HH:mm:ss"
Write-Host $TimeStamp - $LogEntry
Add-Content "$LogFile" -value "$TimeStamp - $LogEntry"
}
##Creates the C:\Logs\PackageName.log or renames it to .lo_ if it already exists
##The .lo_ format is still readable by some live log file readers such as CMTrace and Trace32 and doesn't require being renamed before reading.
$Date = Get-Date
If (test-path $LogFile) {
Copy-Item -Path $LogFile ($LogFile.TrimEnd("log") + "lo_") -Force
New-Item -Path "C:\Logs\$PackageName.log" -Force -ItemType File -Value "---Log File---`r`nInstalling = $PackageName`r`nDate = $Date`r`nOperating System = $OSName`r`nOS Architecture = $env:PROCESSOR_ARCHITECTURE`r`n`r`n`r`n---Start Logging---`r`n`r`n"
} else {
New-Item -Path "C:\Logs\$PackageName.log" -Force -ItemType File -Value "---Log File---`r`nInstalling = $PackageName`r`nDate = $Date`r`nOperating System = $OSName`r`nOS Architecture = $env:PROCESSOR_ARCHITECTURE`r`n`r`n`r`n---Start Logging---`r`n`r`n"
}
######################
Function Check-CriticalError(){
If($Global:CriticalError -eq $true){
Log("Critical Error detected! - Script will now Exit")
$returnval = 1
Exit($returnval)
}
}
$Global:CriticalError = $False
Log("######################")
Log("Starting Certiport 2013 VHD Installation")
Log("Source Directory is: $PKGLocation")
# Check that D Drive Exists
If(-Not (Test-Path D:\)){
Log("ERROR: D Drive does not exist - Exiting")
$Global:CriticalError = $true
}
Check-CriticalError
# Check Disk Space requirement (40 GB) for D Drive
If(((Get-WmiObject Win32_Volume -Namespace "root\CIMV2" -Filter {DriveLetter = "D:"}).FreeSpace) -lt "21474836480"){
Log("ERROR: Insufficient Disk Space - 40GB Required - $("{0:N2}" -f (((Get-WmiObject Win32_Volume -Namespace "root\CIMV2" -Filter {DriveLetter = "D:"}).FreeSpace)/1024/1024)) Mb Free - Exiting")
$Global:CriticalError = $true
}
Check-CriticalError
# Check the Certiport Install Directory exists and create it if not.
If(-Not (Test-Path D:\CertiPort)){
New-Item -ItemType Directory D:\Certiport | Out-Null
}
# Extract the VHD to the correct directory and perform an MD5 Check OR Verify and validate the state of the currently existing VHD.
$Global:VHDFile = "D:\Certiport\$PackageName.vhd"
$Global:MasterHash = Get-Content $PkgLocation\MD5.txt
If(-Not (Test-Path D:\Certiport\$PackageName.vhd)){
Log("VHD Does not exist in D:\Certiport - Extracting from Compressed File")
$ScriptBlock = [Scriptblock]::Create("$PkgLocation\7za.exe x `"$PkgLocation\$PackageName.7z`" -oD:\ -r -aoa")
Log("Running - `'$ScriptBlock`'")
$7ZipExtract = Invoke-Command -ScriptBlock $ScriptBlock
Log("Verifying MD5 Hash")
$hash = [Security.Cryptography.HashAlgorithm]::Create( "MD5" )
$stream = ([IO.StreamReader]"$VHDFile").BaseStream
$HashCode = -join ($hash.ComputeHash($stream) | ForEach { "{0:x2}" -f $_ })
$stream.Close()
If($MasterHash -ne $HashCode){
Log("ERROR: Hash Check Failed - `"$MasterHash`" is not `"$HashCode`"")
Log("ERROR: Source appears corrupted")
$Global:CriticalError = $true
}Else{
Log("Hash Check Successful")
}
}Else{
Log("VHD already exists in D:\Certiport - Verifying MD5 Hash")
$hash = [Security.Cryptography.HashAlgorithm]::Create( "MD5" )
$stream = ([IO.StreamReader]"$VHDFile").BaseStream
$HashCode = -join ($hash.ComputeHash($stream) | ForEach { "{0:x2}" -f $_ })
$stream.Close()
If($MasterHash -ne $HashCode){
Log("WARNING: Hash Check Failed - `"$MasterHash`" is not `"$HashCode`"")
Log("Extracting file from source...")
$ScriptBlock = [Scriptblock]::Create("$PkgLocation\7za.exe x `"$PkgLocation\$PackageName.7z`" -oD:\ -r -aoa")
Log("Running - `'$ScriptBlock`'")
$7ZipExtract = Invoke-Command -ScriptBlock $ScriptBlock
Log("Verifying MD5 Hash")
$hash = [Security.Cryptography.HashAlgorithm]::Create( "MD5" )
$stream = ([IO.StreamReader]"$VHDFile").BaseStream
$HashCode = -join ($hash.ComputeHash($stream) | ForEach { "{0:x2}" -f $_ })
$stream.Close()
If($MasterHash -ne $HashCode){
Log("ERROR: Hash Check Failed - `"$MasterHash`" is not `"$HashCode`"")
Log("ERROR: Source appears corrupted")
$Global:CriticalError = $true
}Else{
Log("VHD Hash Check Successful")
}
}Else{
Log("VHD Hash Check Successful")
}
}
Check-CriticalError
# Check BCD For any Previous Entry and remove it
$ScriptBlock = [Scriptblock]::Create("bcdedit /v")
$BCDConfig = Invoke-Command -ScriptBlock $ScriptBlock
$BCDItem = (0..($BCDConfig.Count - 1) | Where { $BCDConfig[$_] -like "description*Certiport 2013 Certification*" }) - 3
If($BCDConfig[$BCDItem] -ne $BCDConfig[-3]){
$BCDIdentifier = (((($BCDConfig[$BCDItem]).Split("{"))[1]).Split("}"))[0]
Log("Previous entry found - $BCDIdentifier")
$ScriptBlock = [Scriptblock]::Create("bcdedit /delete ``{$BCDIdentifier``}")
$cmdReturn = Invoke-Command -ScriptBlock $ScriptBlock
If($cmdReturn -eq "The operation completed successfully."){
Log("Successfully Removed previous Certiport Entry")
}Else{
Log("ERROR: Could not remove previous Entry - $cmdReturn")
$Global:CriticalError = $true
}
}
Check-CriticalError
# Update Boot Files for UEFI devices and Windows 7
$ScriptBlock = [Scriptblock]::Create("bcdedit /export C:\CertiportVHD-2013-BCD-Backup")
$cmdReturn = Invoke-Command -ScriptBlock $ScriptBlock
If($cmdReturn -eq "The operation completed successfully."){
Set-Content $Env:Temp\CertiportVHD.txt $DiskpartAttachVDisk
$ScriptBlock = [Scriptblock]::Create("diskpart /s $($Env:Temp)\CertiportVHD.txt")
$cmdReturn = Invoke-Command -ScriptBlock $ScriptBlock
If($cmdReturn -like "*DiskPart successfully attached the virtual disk file.*"){
Sleep 10
Set-Content $Env:Temp\CertiportVHD.txt $DiskpartChangeLetter
$ScriptBlock = [Scriptblock]::Create("diskpart /s $($Env:Temp)\CertiportVHD.txt")
$cmdReturn = Invoke-Command -ScriptBlock $ScriptBlock
If($cmdReturn -like "*DiskPart successfully assigned the drive letter or mount point.*"){
Log("VHD Successfully Mounted")
$ScriptBlock = [Scriptblock]::Create("bcdboot.exe V:\Windows")
$cmdReturn = Invoke-Command -ScriptBlock $ScriptBlock
If($cmdReturn -eq "Boot files successfully created."){
Log("Boot files successfully created")
Set-Content $Env:Temp\CertiportVHD.txt $DiskpartDetachVDisk
$ScriptBlock = [Scriptblock]::Create("diskpart /s $($Env:Temp)\CertiportVHD.txt")
$cmdReturn = Invoke-Command -ScriptBlock $ScriptBlock
If($cmdReturn -like "DiskPart successfully detached the virtual disk file."){
Log("Successfully Detached the VHD")
sleep 10
$ScriptBlock = [Scriptblock]::Create("bcdedit /import C:\CertiportVHD-2013-BCD-Backup")
$cmdReturn = Invoke-Command -ScriptBlock $ScriptBlock
If($cmdReturn -eq "The operation completed successfully."){
Log("Successfully Imported the BCD Backup")
}Else{
Log("ERROR: Could not restore the BCD Backup - $cmdReturn")
}
}Else{
Log("ERROR: Could not detach the VHD - $cmdReturn")
}
}Else{
Log("ERROR: Could not create the boot files - $cmdReturn")
}
}Else{
Log("ERROR: Could not assign the VHD to `"V:`" drive - $cmdReturn")
$Global:CriticalError = $true
Check-CriticalError
}
}Else{
Log("ERROR: Could not mount the VHD - $cmdReturn")
$Global:CriticalError = $true
Check-CriticalError
}
}Else{
Log("ERROR: Could not back up BCD - Quitting immediately to avoid destroying boot order - $cmdReturn")
$Global:CriticalError = $true
Check-CriticalError
}
# Configure new BCD Entry For Certiport 2013
Log("Creating BCD Entry")
$ScriptBlock = [Scriptblock]::Create("bcdedit.exe /copy ``{default``} /d `"Certiport 2013 Certification v1.3`"")
$cmdReturn = Invoke-Command -ScriptBlock $ScriptBlock
If($cmdReturn -like "The entry was successfully copied to*"){
$CertiportGuid = ((($cmdReturn.Split(" "))[6]) -replace ".$") -replace '[{}]',''
Log("Created new BCD entry - $CertiportGuid")
$ScriptBlock = [Scriptblock]::Create("bcdedit.exe /set ``{$CertiportGuid``} osdevice vhd=[d:]\Certiport\$PackageName.vhd")
$cmdReturn = Invoke-Command -ScriptBlock $ScriptBlock
If($cmdReturn -like "The operation completed successfully."){
Log("Successfully changed BCD osdevice")
$ScriptBlock = [Scriptblock]::Create("bcdedit.exe /set ``{$CertiportGuid``} device vhd=[d:]\Certiport\$PackageName.vhd")
$cmdReturn = Invoke-Command -ScriptBlock $ScriptBlock
If($cmdReturn -like "The operation completed successfully."){
Log("Successfully changed BCD device")
$ScriptBlock = [Scriptblock]::Create("bcdedit.exe /timeout 5")
$cmdReturn = Invoke-Command -ScriptBlock $ScriptBlock
If($cmdReturn -like "The operation completed successfully."){
Log("Successfully changed boot timeout")
}Else{
Log("ERROR: Could not change boot timeout - $cmdReturn")
}
}Else{
Log("ERROR: Could not change BCD device - $cmdReturn")
$Global:CriticalError = $true
}
}Else{
Log("ERROR: Could not change BCD osdevice - $cmdReturn")
$Global:CriticalError = $true
}
}Else{
Log("ERROR: Could not copy BCD Entry for Certiport - $cmdReturn")
$Global:CriticalError = $true
}
Check-CriticalError
Log("Certiport Exam VHD Deployed")
exit $returnval
日志输出:
10:28:29-######################
10:28:29-开始Certiport 2013 VHD安装
10:28:29-源目录为:C:\Windows\SysWOW64\CCM\Cache\00000379.2.System
10:28:29-VHD已存在于D:\Certiport-验证MD5哈希
10:36:41-VHD哈希检查成功
10:36:44-错误:无法备份BCD-立即退出到
避免破坏启动顺序-
10:36:44-检测到严重错误!-脚本现在将退出
在我看来,问题在于此脚本块:
$ScriptBlock = [Scriptblock]::Create("bcdedit /export C:\CertiportVHD-2013-BCD-Backup")
$cmdReturn = Invoke-Command -ScriptBlock $ScriptBlock
我在多种设备上运行代码,结果是一样的。
Windows 10 x64、Windows 8.1 x64和Windows 7 x86/x64
运行powershell v4
我尝试将我认为是上述问题的部分更改为:
$App = "bcdedit"
$Arguments = "/export C:\CertiportVHD-2013-BCD-Backup"
$cmdReturn = Start-Process -FilePath $App -ArgumentList $Arguments -Wait -PassThru
然而,这也不起作用,并给出了相同的结果
我非常感谢您提供的任何帮助,提前谢谢您。关于您在评论中提出的问题,很遗憾,我无法使用procmon找到错误。还有其他想法吗 不,不是真的。在系统中使用psexec执行命令,正如您所说,在SCCM下运行它没有什么区别
- procmon是否捕获了
的开始和停止bcdedit
- 出口代码是什么?您可以发布屏幕截图或共享procmon跟踪吗
事实证明,解决此问题的方法是复制bcdedit并将其放入包中,而不是简单地指定“bcdedit” 我通过脚本将对文件“bcdedit.exe”的引用更新为“\bcdedit.exe”,因为SCCM将目录更改为它复制到本地缓存的包文件夹的根目录 我不知道为什么在作为登录到设备的用户运行时可以指定bcdedit,但在作为系统运行时,SCCM不能使用相同的语法,但此修复程序有效 感谢@LievenKeersmaekers帮助我发现系统找不到该文件,当时他说$cmdReturn为空,这让我思考了为什么会出现这种情况,当他测试它时,它返回了一个值,不管它是否出错 编辑:
我还注意到bcdboot.exe也发生了同样的事情,必须将该文件复制到包源文件和引用中。\bcdboot.exe您将
调用命令的结果存储到$BCDConfig
中,但在下一行中,您将$cmdReturn
与成功完成的操作进行比较。
和fwiw,我会在您的脚本执行时尝试运行procmon,以获取执行bcdedit
@LievenKeersmaekers时的实际错误(如果有)。谢谢您的评论,我很抱歉,在将脚本的该部分转换回其原始版本时,这是一个错误。谢谢你指出,我已经把它编辑成它应该是什么样子了。您现在看到的是由SCCM运行的。再次道歉。根据您当时的日志,$cmdReturn
为空或空。我尝试在没有权限的情况下执行该命令,并尝试执行无效的命令。两者至少都返回一个错误。我不知道$cmdReturn
如何可以为空。我建议的procmon
路线是我下一步要尝试的。谢谢@LievenKeersmaekers,我明天回去工作的时候再试试。但是我对ProcMon不是很熟练,所以希望我能理解它。我没有真正使用过它。感谢您的帮助,事实上,在脚本中简单地指定“bcdedit”时,由于某种原因,以SCCM运行时无法找到bcdedit。我必须复制“bcdedit.exe”,并使用“.\bcdedit.exe”在缓存中的包文件夹中引用它。