MSFN Forum: WinPE 3.0 and Powershell - MSFN Forum

Jump to content


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

WinPE 3.0 and Powershell Rate Topic: -----

#1 User is offline   monarch684 

  • Group: Members
  • Posts: 4
  • Joined: 11-August 12
  • OS:Windows 7 x64
  • Country: Country Flag

Posted 11 August 2012 - 02:22 PM

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:

Quote

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?


#2 User is offline   IcemanND 

  • MSFN Junkie
  • Group: Super Moderator
  • Posts: 3,266
  • Joined: 24-September 03
  • OS:Windows 7 x64
  • Country: Country Flag

Posted 11 August 2012 - 03:01 PM

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





#3 User is offline   monarch684 

  • Group: Members
  • Posts: 4
  • Joined: 11-August 12
  • OS:Windows 7 x64
  • Country: Country Flag

Posted 11 August 2012 - 03:15 PM

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:

Quote

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.

#4 User is offline   monarch684 

  • Group: Members
  • Posts: 4
  • Joined: 11-August 12
  • OS:Windows 7 x64
  • Country: Country Flag

Posted 11 August 2012 - 04:02 PM

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


#5 User is offline   IcemanND 

  • MSFN Junkie
  • Group: Super Moderator
  • Posts: 3,266
  • Joined: 24-September 03
  • OS:Windows 7 x64
  • Country: Country Flag

Posted 11 August 2012 - 04:06 PM

Strange. It added all of the carriage returns when I tested it before posting otherwise I would have added them.

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users



All trademarks mentioned on this page are the property of their respective owners
Copyright © 2001 - 2013 msfn.org
Privacy Policy