none
dism command to apply image on C:\ Drive. Powershell doesn't recognize imagex command. RRS feed

  • Question

  • Hello,

    I have this script that I am running to deploy the image but when I try to run the script I get the error saying imagex is not recognizable cmdlet. I tried using Dism.exe /applyimage and its not working either. The line is highlighted. Any help would be appreciated. 


    function DeployImage($fManufacturer,$fModel,$fImagepath,$fDiskpartpath,$fEnvironmentTypeList)
     {
         $model = $fModel.Replace(" ","-")
         $AvailableImages = gci $fImagepath\* -include *$model*

        $colAvailImageInventory = @()

        foreach ($imagefile in $AvailableImages)`
         {
             #write-host $imagefile
             if ($imagefile -like "*Windows-10*") `
             {
                 $splitname = $imagefile.Name.split("_")
                 $objAvailImageInventory = New-Object System.Object
                 $objAvailImageInventory | Add-Member -type NoteProperty -name Environment -value $splitname[0]
                 $objAvailImageInventory | Add-Member -type NoteProperty -name FileName -value $imagefile.Name
                 $objAvailImageInventory | Add-Member -type NoteProperty -name DriveLetter -value $splitname[2].Replace("-"," ")
                 $objAvailImageInventory | Add-Member -type NoteProperty -name PartitionSize -value $splitname[3].Replace("-"," ")
                 $objAvailImageInventory | Add-Member -type NoteProperty -name OS -value $splitname[4].Replace("-"," ")
                 $objAvailImageInventory | Add-Member -type NoteProperty -name Application -value $splitname[5].Replace("-"," ")
                 $objAvailImageInventory | Add-Member -type NoteProperty -name DateCaptured -value $splitname[6].Trim(".wim")
                 $colAvailImageInventory += $objAvailImageInventory
             } else {
                 $splitname = $imagefile.Name.split("_")
                 $objAvailImageInventory = New-Object System.Object
                 $objAvailImageInventory | Add-Member -type NoteProperty -name Environment -value $splitname[0]
                 $objAvailImageInventory | Add-Member -type NoteProperty -name FileName -value $imagefile.Name
                 $objAvailImageInventory | Add-Member -type NoteProperty -name OS -value $splitname[3].Replace("-"," ")
                 $objAvailImageInventory | Add-Member -type NoteProperty -name Application -value $splitname[4].Replace("-"," ")
                 $objAvailImageInventory | Add-Member -type NoteProperty -name DateCaptured -value $splitname[5].Trim(".wim")
                 $colAvailImageInventory += $objAvailImageInventory
             }

        }

        # Select Environment Type

        $uniqueenvtype = @($colAvailImageInventory | select Environment -Unique)

        for($i=1;$i-le $uniqueenvtype.length;$i++)
         {
             $Line = $i.toString() + ') ' + $uniqueenvtype[$i-1].Environment
             write-host $line
         }

        write-host ""
         $SelectedEnvTypeNumber = read-host "Select Environment"
         $SelectedEnvType = ($uniqueenvtype[$SelectedEnvTypeNumber-1]).Environment
         write-host ""

        # Select Application

        $uniqueapps = @($colAvailImageInventory | select Application -Unique)

        for($i=1;$i-le $uniqueapps.length;$i++)
         {
             $Line = $i.toString() + ') ' + $uniqueapps[$i-1].Application
             write-host $line
         }

        write-host ""
         $SelectedAppNumber = read-host "Select Application"
         $SelectedApp = ($uniqueapps[$SelectedAppNumber-1]).Application
         write-host ""


         # Select OS

        $uniqueOSlist = @($colAvailImageInventory | where { $_.Application -eq $SelectedApp } | select OS -Unique)

        for($i=1;$i-le $uniqueOSlist.length;$i++)
         {
             $Line = $i.toString() + ') ' + $uniqueOSlist[$i-1].OS
             write-host $line
         }

        write-host ""
         $SelectedOSNumber = read-host "Select OS"
         $SelectedOS = ($uniqueOSlist[$SelectedOSNumber-1]).OS
         write-host ""

        # Select Date

        $uniquedates = @($colAvailImageInventory | where { $_.Environment -eq $SelectedEnvType -and $_.Application -eq $SelectedApp -and $_.OS -eq $SelectedOS } | select DateCaptured -Unique)

        
         for($i=1;$i-le $uniquedates.length;$i++)
         {
             $Line = $i.toString() + ') ' + $uniquedates[$i-1].DateCaptured
             write-host $line
         }

        write-host ""
         $SelectedDateNumber = read-host "Select Date"
         $SelectedDate = ($uniquedates[$SelectedDateNumber-1]).DateCaptured

        write-host ""

        if ($SelectedOS -like "*Windows 10*") `
         {
             $imagefilestodeploy = @(($colAvailImageInventory | where { $_.Environment -eq $SelectedEnvType -and $_.Application -eq $SelectedApp -and $_.OS -eq $SelectedOS -and $SelectedDate -eq $_.DateCaptured }).FileName)
             write-host "`nImages to Be Deployed:`n"
             $imagefilestodeploy | % { write-host "  $_" }
             write-host "`nHit 'Enter' to confirm..."
             $inputkey = [Console]::ReadKey($hideKeysStrokes)
             if ($inputkey.key -eq "Enter") `
             {
                 Get-Partition | ? { $_.Type -like "Basic" } | Remove-Partition -Confirm:$false
                 foreach ($imagefile in $imagefilestodeploy) `
                 {
                     $disknumber = (Get-Disk | ? { $_.Path -notlike "*usb*" }[0]).number
                     $driveletter = $imagefile.split("_")[2]
                     $driveletterfull = $driveletter + ':\'
                     $volsize = $imagefile.split("_")[3]
                  $imagefilefullpath = $fImagepath + "\" + $imagefile
                     write-host "`nCreating Partition for $driveletter...`n"
                  New-Partition -DiskNumber $disknumber -Size $volsize -DriveLetter $driveletter 
                     Format-Volume -DriveLetter $driveletter -FileSystem NTFS -Force -Confirm:$false
                     write-host "`nApplying Image for $driveletter...`n"
                  Dism /apply-image /imagefile:$imagefilefullpath  /index:1 /ApplyDir:$driveletterfull
                 }
             }
         } else {
             $imagefiletodeploy = ($colAvailImageInventory | where { $_.Environment -eq $SelectedEnvType -and $_.Application -eq $SelectedApp -and $_.OS -eq $SelectedOS -and $SelectedDate -eq $_.DateCaptured }).FileName

            write-host "The following image will be deployed: $imagefiletodeploy"
             write-host ""

            diskpart /s "$fDiskpartpath\diskpart-preimaging.txt"

            $imagefiletodeployfullpath = $fImagepath + "\" + $imagefiletodeploy
             imagex /check /verify /apply $imagefiletodeployfullpath 1 C:\

            diskpart /s "$fDiskpartpath\diskpart-postimaging.txt"

            start-sleep -s 60

            $bootpartitioncount = (bcdedit /enum | select-string "path" | select-string "\\Windows\\system32\\winload.exe").Count
         
             if ($bootpartitioncount -lt 1) {bcdboot C:\Windows /s C:}

            write-host ""
             write-host "Image Deployment was successful. Press any key to reboot..."

            x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown") | out-null

            restart-computer -force
         }
     }
    • Moved by Bill_Stewart Monday, May 7, 2018 9:54 PM This is not "fix/debug/rewrite my script for me" forum
    Friday, March 2, 2018 5:08 PM