Powershell: Ulvar Petmanson, BootUSB.ps1

From ICO wiki

<source lang="powershell">

<# .Synopsis

 Teeb Windows 7 või Windows 2008 paigaldamise USB pulga.

.Description

 1. Otsib arvuti DVD seadmetest üles kõik sobivad Windows'i installi plaadid ja USB kettad, ning moodustab nendest sobivad paarid
 2. Annab võimalusel kasutajale valida, milliselt plaadilt, millisele USB kettale Windows'i paigaldusfailid kopeeritakse.
 3. Kui valitud USB ketas pole tühi, küsib kasutajalt nõusolekut ketta tühjendamiseks, seejärel teeb USB ketta buutivaks.
 4. Kopeerib Windowsi paigaldusfailid USB kettale. 
  1. >


$SourcesDir = "\sources\" $InstallFileToCheck = "install.wim" $Kettad = (Get-WmiObject win32_logicaldisk -filter 'DriveType=5 and Access>0') $USBKettad = (Get-WmiObject win32_logicaldisk -filter 'DriveType=2') $UusKettad = @() $KetastePaarid =@() $i = 0

Function Select-Item { <#

    From: http://blogs.technet.com/b/jamesone/archive/2009/06/24/how-to-get-user-input-more-nicely-in-powershell.aspx
    
    .Synopsis        Allows the user to select simple items, returns a number to indicate the selected item. 
   .Description 
       Produces a list on the screen with a caption followed by a message, the options are then        displayed one after the other, and the user can one. 
         Note that help text is not supported in this version. 
   .Example 
       PS> select-item -Caption "Configuring RemoteDesktop" -Message "Do you want to: " -choice "&Disable Remote Desktop",           "&Enable Remote Desktop","&Cancel"  -default 1       Will display the following 
         Configuring RemoteDesktop           Do you want to:           [D] Disable Remote Desktop  [E] Enable Remote Desktop  [C] Cancel  [?] Help (default is "E"): 
   .Parameter Choicelist 
       An array of strings, each one is possible choice. The hot key in each choice must be prefixed with an & sign 
   .Parameter Default 
       The zero based item in the array which will be the default choice if the user hits enter. 
   .Parameter Caption 
       The First line of text displayed 
    .Parameter Message 
       The Second line of text displayed     #> 

Param( [String[]]$choiceList,

        [String]$Caption="Palun vali", 
        [String]$Message="Valikud on järgnevad", 
        [int]$default=0 
     ) 
  $choicedesc = New-Object System.Collections.ObjectModel.Collection[System.Management.Automation.Host.ChoiceDescription] 
  $choiceList | foreach  { $choicedesc.Add((New-Object "System.Management.Automation.Host.ChoiceDescription" -ArgumentList $_))} 
  $Host.ui.PromptForChoice($caption, $message, $choicedesc, $default) 

}


foreach ($Ketas in $Kettad) {

 $Usable = Test-Path ($Ketas.DeviceID + $SourcesDir + $InstallFileToCheck)
 if ($Usable) {
     $UusKetas = New-Object PSobject -Property @{
         DeviceID = $Ketas.DeviceID
         DriveType = $Ketas.DriveType
         ProviderName = $Ketas.ProviderName
         FreeSpace = $Ketas.FreeSpace
         Size = $Ketas.Size
         VolumeName = $Ketas.VolumeName
         Usable = $Usable
         Jrk = $i
         }
     $i++
 $UusKettad += $UusKetas
 }
 

}


if ( $UusKettad.Count -lt 1 ) {

 echo "Windowsi installi plaati ei leitud!"
 exit 1

}

$i = 0 foreach ($Ketas in $UusKettad) {

 foreach ($USBKetas in $USBKettad)
 {
 $Ketas.Size
 $USBKetas.Size
   if ($Ketas.Size -lt $USBKetas.Size) {
     $KetastePaar = New-Object PSobject -Property @{
         Source = $Ketas.DeviceID
         SourceVolumeName = $Ketas.VolumeName
         Destination = $USBKetas.DeviceID
         DestinationVolumeName = $USBKetas.VolumeName
         Jrk = $i
         }
   if ( ($KetastePaar.DestinationVolumeName).Length -eq 0) { $KetastePaar.DestinationVolumeName = "Nimetu" }
     $i++
     $KetastePaarid += $KetastePaar
   }
 }

}

$KetastePaarid if ( $Ketastepaarid.Count -eq 0 ) {

 echo "Pole ühtki sobivat ketaste paari!"
 exit

} $i = 0 $Valikud = @()

foreach ($KetastePaar in $KetastePaarid) {

 $Valikud += "&" + $i + "." + $KetastePaar.Source + " (" + $KetastePaar.SourceVolumeName + ") " + " -> " + $KetastePaar.Destination + " (" + $KetastePaar.DestinationVolumeName + ") "
 $i++

}

$Valikjrk = Select-Item -Choice $Valikud

$KetastePaarid

foreach ( $KetastePaar in $KetastePaarid) {

if ( $KetastePaar.Jrk -eq $Valikjrk)
  {
    $KetastePaar.DestinationVolumeName
    $Source = $KetastePaar.Source
    $Destination = $KetastePaar.Destination
    $DestinationVolumeName = $KetastePaar.DestinationVolumeName
    
  }

}

$Source $Destination

if ( @(Get-ChildItem $Destination -force).Count -gt 0) {

  $a = new-object -comobject wscript.shell 
 $intAnswer = $a.popup("Pulk $Destination ( $DestinationVolumeName ) ei ole tühi, kas võib tühjaks teha?",0,"Teeme tühjaks?",4)
 If (!($intAnswer -eq 6)) { 
   echo "Nägemist!"
   exit

} }

$command= @" select volume $Destination clean create partition primary select partition 1 active format fs=fat32 quick exit "@

$command | Diskpart

& "$Source\Boot\BOOTSECT.EXE" /NT60 $Destination

echo "Kopeerin failid pulgale."

Copy-Item $Source\* $Destination\ -Recurse