Jump to content

WinPE 3.0 and Powershell


monarch684

Recommended Posts

I have create a PowerShell script that builds my WInPE 3.0 and then writes it to my choosen jump drive. I automate some changes in the WinPE that seem to crash WinPE when launched. On the other hand if I make the same changes manually everything works fine. The actual WinPE build is fine it is the startnet.cmd that I am editing. Here is the code:

$dir = "C:\WinPE64"
$waik = "C:\Program Files\Windows AIK\Tools\amd64"
$tools = "C:\Program Files\Windows AIK\Tools\PETools\amd64"
$pak = "C:\Program Files\Windows AIK\Tools\PETools\amd64\WinPE_FPs"
$cmd = "$dir\mount\Windows\System32\startnet.cmd"

CLS;

write-host "Creating Directories" -ForegroundColor Yellow
new-item -type directory $dir\ISO -force | Out-Null
new-item -type directory $dir\ISO\boot -force | Out-Null
new-item -type directory $dir\ISO\EFI -force | Out-Null
new-item -type directory $dir\ISO\sources -force | Out-Null
new-item -type directory $dir\mount -force | Out-Null
write-host "Copying bootmgr to $dir\ISO\" -ForegroundColor Yellow
copy-item $tools\bootmgr $dir\ISO\ -force
write-host "Copying bootmgr.efi to $dir\ISO\" -ForegroundColor Yellow
copy-item $tools\bootmgr.efi $dir\ISO\ -force
write-host "Copying etfsboot.com to $dir\ISO\" -ForegroundColor Yellow
copy-item $tools\boot\etfsboot.com $dir\ -force
write-host "Copying efisys.bin to $dir\" -ForegroundColor Yellow
copy-item $tools\boot\efisys.bin $dir\ -force
write-host "Copying efisys_noprompt.bin to $dir\" -ForegroundColor Yellow
copy-item $tools\boot\efisys_noprompt.bin $dir\ -force
write-host "Copying boot folder and contents to $dir\ISO\boot\" -ForegroundColor Yellow
copy-item $tools\boot\* $dir\ISO\boot\ -recurse -force
write-host "Copying EFI folder and contents to $dir\ISO\EFI\" -ForegroundColor Yellow
copy-item $tools\EFI\* $dir\ISO\EFI\ -recurse -force
write-host "Copying winpe.wim to $dir\" -ForegroundColor Yellow
copy-item $tools\winpe.wim $dir\ -force
write-host "Copying imagex.exe to $dir\" -ForegroundColor Yellow
copy-item $wiak\imagex.exe $dir\ -force
Write-Host ""
Write-Host "Mounting WinPE" -foregroundcolor magenta
dism /Mount-Wim /WimFile:$dir\winpe.wim /index:1 /MountDir:$dir\mount
Write-Host ""
Write-Host "Adding Lan Drivers to WinPE" -foregroundcolor magenta
dism /image:$dir\mount /Add-Driver /Driver:C:\LanDrivers\ /recurse
Write-Host ""
Write-Host "Adding Packages to WinPE" -foregroundcolor magenta
dism /image:$dir\mount /Add-Package /PackagePath:"$pak\winpe-wmi.cab" /PackagePath:"$pak\winpe-scripting.cab" /PackagePath:"$pak\winpe-hta.cab"
Write-Host ""
Write-Host "Adding GUI Interface to WinPE" -foregroundcolor magenta
<#$startnet = [diagnostics.process]::start("notepad.exe", "'$dir\mount\Windows\System32\startnet.cmd'")
$startnet.waitforexit() #>
Get-Content $cmd | Where {$_ -ne "wpeinit"} | Out-File $cmd
Write-Output "@echo off" >> $cmd
Write-Output "echo Inititalizing WinPE ..." >> $cmd
Write-Output "wpeinit" >> $cmd
Write-Output "echo Initialized!" >> $cmd
Write-Output "echo Connecting to Image Server ..." >> $cmd
Write-Output "net use Z: `"\\SRHS-SUP-FS\Desktop Support\Images`"" >> $cmd
Write-Output "echo Connected!" >> $cmd
Write-Output "cd /d Z:\" >> $cmd
Write-Output "echo Launching GUI Interface ..." >> $cmd
Write-Output "index.hta" >> $cmd
Write-Host ""
Write-Host "Saving Changes and UnMounting WinPE" -foregroundcolor magenta
dism /Unmount-Wim /MountDir:$dir\mount /commit
Write-Host ""
Write-Host "Coping winpe.wim and Changing Name to boot.wim" -foregroundcolor magenta
copy-item $dir\winpe.wim $dir\ISO\sources\boot.wim | Out-Null

"list disk" | diskpart
$disk = Read-Host "Select Disk Number"
"select disk $disk", "clean", "create partition primary", "select partition 1", "active", "format fs=fat32 quick", "assign", "exit" | diskpart

$drive = Read-Host "What letter was assigned to drive"
Write-Host ""
Write-Host "Copying WinPE Files to Jump Drive" -foregroundcolor magenta
copy-item $dir\ISO\* $drive`:\ -recurse -force
Write-Host "Completed!" -foregroundcolor green
Start-sleep 3

As you can see here:

Write-Host "Adding GUI Interface to WinPE" -foregroundcolor magenta
<#$startnet = [diagnostics.process]::start("notepad.exe", "'$dir\mount\Windows\System32\startnet.cmd'")
$startnet.waitforexit() #>
Get-Content $cmd | Where {$_ -ne "wpeinit"} | Out-File $cmd
Write-Output "@echo off" >> $cmd
Write-Output "echo Inititalizing WinPE ..." >> $cmd
Write-Output "wpeinit" >> $cmd
Write-Output "echo Initialized!" >> $cmd
Write-Output "echo Connecting to Image Server ..." >> $cmd
Write-Output "net use Z: `"\\SRHS-SUP-FS\Desktop Support\Images`"" >> $cmd
Write-Output "echo Connected!" >> $cmd
Write-Output "cd /d Z:\" >> $cmd
Write-Output "echo Launching GUI Interface ..." >> $cmd
Write-Output "index.hta" >> $cmd
Write-Host ""

I have palyed with manually entering the code and automating the input of the code. The error I get when I automate this is:

X:\ windows\system32> @

' @' is not recognized as an internal or eternal command, operable program or batch file.

Any ideas why this works when I manually edit this file verses automatically editting this file?

Link to comment
Share on other sites


It doesn't like the default unicode encoding of out-file. It also appears to have a problem with the file being "open" at the same time you are writing to it, though since the default startnet.cmd file only has the one wpeinit line it may not make a difference to you.

Personally I would replace this:

Get-Content $cmd | Where {$_ -ne "wpeinit"} | Out-File $cmd
Write-Output "@echo off" >> $cmd
Write-Output "echo Inititalizing WinPE ..." >> $cmd
Write-Output "wpeinit" >> $cmd
Write-Output "echo Initialized!" >> $cmd
Write-Output "echo Connecting to Image Server ..." >> $cmd
Write-Output "net use Z: `"\\SRHS-SUP-FS\Desktop Support\Images`"" >> $cmd
Write-Output "echo Connected!" >> $cmd
Write-Output "cd /d Z:\" >> $cmd
Write-Output "echo Launching GUI Interface ..." >> $cmd
Write-Output "index.hta" >> $cmd

With something like this:

$startnet = Get-Content $cmd | Where {$_ -ne "wpeinit"}
$startnet += "@echo off"
$startnet += "echo Inititalizing WinPE ..."
$startnet += "wpeinit"
$startnet += "echo Initialized!"
$startnet += "echo Connecting to Image Server ..."
$startnet += "net use Z: `"\\SRHS-SUP-FS\Desktop Support\Images`""
$startnet += "echo Connected!"
$startnet += "cd /d Z:\"
$startnet += "echo Launching GUI Interface ..."
$startnet += "index.hta"
Out-File -filepath $cmd -InputObject $startnet -Encoding ASCII

Link to comment
Share on other sites

I tried this:

$startnet = Get-Content $cmd | Where {$_ -ne "wpeinit"} 
$startnet += "@echo off"
$startnet += "echo Inititalizing WinPE ..."
$startnet += "wpeinit"
$startnet += "echo Initialized!"
$startnet += "echo Connecting to Image Server ..."
$startnet += "net use Z: `"\\SRHS-SUP-FS\Desktop Support\Images`""
$startnet += "echo Connected!"
$startnet += "cd /d Z:\"
$startnet += "echo Launching GUI Interface ..."
$startnet += "index.hta"
Out-File -filepath $cmd -InputObject $startnet -Encoding ASCII

and got this:

echo off echo Initializing WinPE ... wpeinit echo Initializied! echo Connecting to Image Server ... net use Z: “\\SRHS-SUP-FS\Desktop Support\Images” echo Connected cd /d Z:\ echo Launching GUI Interface ... index.hta

X:\windows\system32>

It looks like there are no hard returns being put in.

Link to comment
Share on other sites

I modified the script as follows:

$startnet = Get-Content $cmd | Where {$_ -ne "wpeinit"} 
$startnet += "@echo off`r`n"
$startnet += "echo Inititalizing WinPE ...`r`n"
$startnet += "wpeinit`r`n"
$startnet += "echo Initialized!`r`n"
$startnet += "echo Connecting to Image Server ...`r`n"
$startnet += "net use Z: `"\\SRHS-SUP-FS\Desktop Support\Images`"`r`n"
$startnet += "echo Connected!`r`n"
$startnet += "cd /d Z:\`r`n"
$startnet += "echo Launching GUI Interface ...`r`n"
$startnet += "index.hta`r`n"

It now works. Here is the final script:

$dir = "C:\WinPE64"
$waik = "C:\Program Files\Windows AIK\Tools\amd64"
$tools = "C:\Program Files\Windows AIK\Tools\PETools\amd64"
$pak = "C:\Program Files\Windows AIK\Tools\PETools\amd64\WinPE_FPs"
$cmd = "$dir\mount\Windows\System32\startnet.cmd"

CLS;

write-host "Creating Directories" -ForegroundColor Yellow
new-item -type directory $dir\ISO -force | Out-Null
new-item -type directory $dir\ISO\boot -force | Out-Null
new-item -type directory $dir\ISO\EFI -force | Out-Null
new-item -type directory $dir\ISO\sources -force | Out-Null
new-item -type directory $dir\mount -force | Out-Null
write-host "Copying bootmgr to $dir\ISO\" -ForegroundColor Yellow
copy-item $tools\bootmgr $dir\ISO\ -force
write-host "Copying bootmgr.efi to $dir\ISO\" -ForegroundColor Yellow
copy-item $tools\bootmgr.efi $dir\ISO\ -force
write-host "Copying etfsboot.com to $dir\ISO\" -ForegroundColor Yellow
copy-item $tools\boot\etfsboot.com $dir\ -force
write-host "Copying efisys.bin to $dir\" -ForegroundColor Yellow
copy-item $tools\boot\efisys.bin $dir\ -force
write-host "Copying efisys_noprompt.bin to $dir\" -ForegroundColor Yellow
copy-item $tools\boot\efisys_noprompt.bin $dir\ -force
write-host "Copying boot folder and contents to $dir\ISO\boot\" -ForegroundColor Yellow
copy-item $tools\boot\* $dir\ISO\boot\ -recurse -force
write-host "Copying EFI folder and contents to $dir\ISO\EFI\" -ForegroundColor Yellow
copy-item $tools\EFI\* $dir\ISO\EFI\ -recurse -force
write-host "Copying winpe.wim to $dir\" -ForegroundColor Yellow
copy-item $tools\winpe.wim $dir\ -force
write-host "Copying imagex.exe to $dir\" -ForegroundColor Yellow
copy-item $wiak\imagex.exe $dir\ -force
Write-Host ""
Write-Host "Mounting WinPE" -foregroundcolor magenta
dism /Mount-Wim /WimFile:$dir\winpe.wim /index:1 /MountDir:$dir\mount
Write-Host ""
Write-Host "Adding Lan Drivers to WinPE" -foregroundcolor magenta
dism /image:$dir\mount /Add-Driver /Driver:C:\LanDrivers\ /recurse
Write-Host ""
Write-Host "Adding Packages to WinPE" -foregroundcolor magenta
dism /image:$dir\mount /Add-Package /PackagePath:"$pak\winpe-wmi.cab" /PackagePath:"$pak\winpe-scripting.cab" /PackagePath:"$pak\winpe-hta.cab"
Write-Host ""
Write-Host "Adding GUI Interface to WinPE" -foregroundcolor magenta
$startnet = Get-Content $cmd | Where {$_ -ne "wpeinit"}
$startnet += "@echo off`r`n"
$startnet += "echo Inititalizing WinPE ...`r`n"
$startnet += "wpeinit`r`n"
$startnet += "echo Initialized!`r`n"
$startnet += "echo Connecting to Image Server ...`r`n"
$startnet += "net use Z: `"\\SRHS-SUP-FS\Desktop Support\Images`"`r`n"
$startnet += "echo Connected!`r`n"
$startnet += "cd /d Z:\`r`n"
$startnet += "echo Launching GUI Interface ...`r`n"
$startnet += "index.hta`r`n"
Out-File -filepath $cmd -InputObject $startnet -Encoding ASCII
Write-Host ""
Write-Host "Saving Changes and UnMounting WinPE" -foregroundcolor magenta
dism /Unmount-Wim /MountDir:$dir\mount /commit
Write-Host ""
Write-Host "Coping winpe.wim and Changing Name to boot.wim" -foregroundcolor magenta
copy-item $dir\winpe.wim $dir\ISO\sources\boot.wim | Out-Null

"list disk" | diskpart
$disk = Read-Host "Select Disk Number"
"select disk $disk", "clean", "create partition primary", "select partition 1", "active", "format fs=fat32 quick", "assign", "exit" | diskpart

$drive = Read-Host "What letter was assigned to drive"
Write-Host ""
Write-Host "Copying WinPE Files to Jump Drive" -foregroundcolor magenta
copy-item $dir\ISO\* $drive`:\ -recurse -force
Write-Host "Completed!" -foregroundcolor green
Start-sleep 3

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...