Win10+ Setup Disk (Works with UEFI Secure Boot / BIOS / Install.wim over 4 GB)

Discussion in 'Scripting' started by freddie-o, Mar 21, 2019.

  1. freddie-o

    freddie-o MDL Expert

    Jul 29, 2009
    1,375
    2,277
    60
    Try running this Powershell script mentioned by @Jack Schitt first.

    Code:
    Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
     
  2. uffbros

    uffbros MDL Senior Member

    Aug 9, 2010
    447
    58
    10
    It doesn't thou....Jack s**tt's post is way over my head..I just want the freekin thing to work....Thanks anyways..I can live without it.
     
  3. freddie-o

    freddie-o MDL Expert

    Jul 29, 2009
    1,375
    2,277
    60
    #43 freddie-o, Jun 9, 2019
    Last edited: Jul 30, 2019
    (OP)
    If Windows installation disk [script] does NOT run:

    By default, Windows clients restricts the execution of powershell scripts to prevent the execution of malicious scripts.
    In case there is a problem running the Windows Installation Disk [script], run this Powershell command first
    Code:
    Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
    
    to remove the restriction and allow you to execute the script. (The Windows Installation Disk script first creates "script.ps1" in your Temp folder and it is this powershell script that creates the installation disk.)
     
  4. Jack Schitt

    Jack Schitt MDL Novice

    Nov 26, 2010
    35
    20
    0
    #44 Jack Schitt, Jun 9, 2019
    Last edited: Jun 11, 2019
    Unless you fix this you will live without any PowerShell Scripting.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  5. uffbros

    uffbros MDL Senior Member

    Aug 9, 2010
    447
    58
    10
    #45 uffbros, Jun 10, 2019
    Last edited: Jun 11, 2019
    Thanks all..I was able to finally figure it out and got my USB Boot Install done. Last question. Say next week the next build of Win 10 1903 comes out. What would I have to do now? Do I just need to extract the iso to the USB drive like I did before using Rufus or do I need to start this script all over again to do this? If in fact that is all I need to do....would I leave the Fat32 drive that is labeled Boot alone and only extract the new build of 1903 to the Install labeled drive?
     
  6. freddie-o

    freddie-o MDL Expert

    Jul 29, 2009
    1,375
    2,277
    60
    I'm working on modifying the script so as to have just one GUI to create the Windows installation disk. So far, with my very limited knowledge of scripting, this is as far as I got.

    Code:
    @echo off
    (
    echo #
    echo #    This script must be executed with admin privilege
    echo #
    echo #    Test Administrator privileges
    echo If ^(-NOT ^([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent^(^)^).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator"^)^)
    echo {
    echo #    Restart the script to get Administrator privileges and exit
    echo     Start-Process 'powershell.exe' -Verb runAs -ArgumentList ^("-NoLogo -WindowStyle Normal -noprofile -file " + """" + $PSCommandPath + """"^)
    echo     exit
    echo }
    echo #    We have Administrator privileges
    echo #
    echo [console]::ForegroundColor = "Yellow"
    echo [console]::BackgroundColor = "blue"
    echo #
    echo Add-Type -AssemblyName System.Windows.Forms # Load the class System.Windows.Forms
    echo # Filebrowser dialog
    echo $FileBrowser = New-Object System.Windows.Forms.OpenFileDialog -Property @{
    echo    Multiselect = $false # One file can be chosen
    echo     Filter = 'ISO images ^(*.iso^)^|*.iso' # Select iso files
    echo }
    echo #
    
    echo clear-host
    echo #    Select the USB disk
    echo $USBDiskForm=New-Object System.Windows.Forms.Form # Create the screen form ^(window^)
    echo # $USBDiskForm ^|gm;pause
    echo # Set the title and size of the window:
    echo $USBDiskForm.Text='Windows installation disk^'
    echo $USBDiskForm.Width=490 ; $USBDiskForm.Height=210
    echo $USBDiskForm.AutoSize=$true # AutoSize property to make the form automatically stretch, if the elements on the form are out of bounds
    echo # Create a drop-down list and fill it
    echo $USB=$Null
    echo $USBDisks=@^(^) # array with USB disk number
    echo $Disks=Get-Disk ^| Where-Object {^($_.BusType -eq "USB"^) -and ^($_.OperationalStatus -eq "Online"^)}
    
    echo $USBDiskList=New-Object System.Windows.Forms.ComboBox
    echo # $USBDiskList^|gm
    echo $USBDiskList.Width=450
    echo Foreach ^($USBDisk in $Disks^) {
    echo     $FriendlyName=^($USBDisk.FriendlyName^).PadRight^(30," "^).substring^(0,30^)   
    echo     $Partitions = Get-Partition ^| Where-Object { $_.DiskNumber -eq $USBDisk.DiskNumber}
    echo     If ^($Partitions^) {
    echo         Foreach ^($Partition in $Partitions^) {
    echo         $Volumes=get-volume ^| Where-Object {$Partition.AccessPaths -contains  $_.path }
    echo         Foreach ^($Volume in $Volumes^) {
    echo             $USBDisks+=$USBDisk.DiskNumber
    echo             $USBDiskList.Items.Add^(^(" {0,-30}| {1,1}:| {2,-30}| {3:n2} GB" -f $FriendlyName, ^($Partition.DriveLetter^), $Volume.FileSystemLabel.PadRight^(30," "^).substring^(0,30^), ^($Partition.Size/1GB^)^)^)^|out-null
    echo             }       
    echo         }
    echo     } Else {
    echo         $USBDisks+=$USBDisk.DiskNumber
    echo         $USBDiskList.Items.Add^(^(" {0,-30}| {1,1}| {2,-30}| {3:n2} GB" -f $FriendlyName, " ", " ",^($USBDisk.Size/1GB^)^)^)^|out-null
    echo     }
    echo }
    
    echo $SelectUSBDisk=New-Object System.Windows.Forms.Label # Put the SelectUSBDisk label on the form
    echo $SelectUSBDisk.Location=New-Object System.Drawing.Size^(10,20^)
    echo $SelectUSBDisk.Text="Select USB disk";$SelectUSBDisk.Font='Segoe UI,10'
    echo $SelectUSBDisk.width=200;$SelectUSBDisk.height=20;
    echo $USBDiskForm.Controls.Add^($SelectUSBDisk^)
    
    echo $USBDiskList.DropDownStyle=[System.Windows.Forms.ComboBoxStyle]::DropDownList;
    echo $USBDiskList.Location=New-Object System.Drawing.Point^(10,40^)
    echo $USBDiskList.width=450;$USBDiskList.height=20
    echo $USBDiskList.backColor="blue" ; $USBDiskList.ForeColor="White"
    echo $USBDiskList.Font='Segoe UI,10'
    echo $USBDiskForm.Controls.Add^($USBDiskList^)
    
    echo $SelectISOButton=New-Object System.Windows.Forms.Button # Put the SelectISO button on the form
    echo $SelectISOButton.Location=New-Object System.Drawing.Size^(10,100^)
    echo $SelectISOButton.Text="Select ISO";$SelectISOButton.Font='Segoe UI,10'
    echo $SelectISOButton.width=80;$SelectISOButton.height=60;
    echo $USBDiskForm.Controls.Add^($SelectISOButton^)
    echo $SelectISOButton.Add_Click^({$global:SelectISO=$true;$USBDiskForm.close^(^)}^)
    echo $USBDiskForm.Controls.Add^($SelectISO^)
    
    echo $CreateUSBDiskButton=New-Object System.Windows.Forms.Button # Put the SelectISO button on the form
    echo $CreateUSBDiskButton.Location=New-Object System.Drawing.Size^(350,130^)
    echo $CreateUSBDiskButton.Text="Create USB Disk";$CreateUSBDiskButton.Font='Segoe UI,10'
    echo $CreateUSBDiskButton.width=110;$CreateUSBDiskButton.height=30;
    echo $USBDiskForm.Controls.Add^($CreateUSBDiskButton^)
    echo $CreateUSBDiskButton.Add_Click^({$global:SelectISO=$true;$USBDiskForm.close^(^)}^)
    echo $USBDiskForm.Controls.Add^($SelectISO^)
    
    echo $CancelButton=New-Object System.Windows.Forms.Button # Put the Cancel button on the form
    echo $CancelButton.Location=New-Object System.Drawing.Size^(260,130^)
    echo $CancelButton.Size=New-Object System.Drawing.Size^(120,20^)
    echo $CancelButton.Text="Cancel";$CancelButton.Font='Segoe UI,10'
    echo $CancelButton.width=60;$CancelButton.height=30;
    echo $USBDiskForm.Controls.Add^($CancelButton^)
    echo $CancelButton.Add_Click^({$global:SelectISO=$False;$USBDiskForm.close^(^)}^)
    echo [void]$USBDiskForm.ShowDialog^(^) # Display the form on the screen
    echo if ^(!$global:SelectISO^){$wshell.Popup^("Operation cancelled",0,"Error"^) ^| Out-Null;exit}
    echo #
    echo [void]$FileBrowser.ShowDialog^(^)
    echo #
    echo $ImagePath = $FileBrowser.FileName;
    echo $ISO=":"
    echo If^($FileBrowser.FileNames -like "*\*"^) {
    echo #  Check if iso already mounted
    echo     $ISO = ^(Get-DiskImage -ImagePath $ImagePath ^| Get-Volume^).DriveLetter
    echo #    Mount iso
    echo     IF ^(!$ISO^) {Mount-DiskImage -ImagePath $ImagePath -StorageType ISO ^|out-null
    echo     $ISO = ^(Get-DiskImage -ImagePath $ImagePath ^| Get-Volume^).DriveLetter}
    echo     $ISO=$ISO+":"
    echo }
    echo if ^($ISO -eq ":"^) {$wshell.Popup^("No ISO image mounted or operation cancelled",0,"Error"^) ^| Out-Null;exit}
    echo $USB=$USBDisks[$USBDiskList.SelectedIndex]
    echo #    Clear the USB stick
    echo Clear-Disk $usb -RemoveData -RemoveOEM -Confirm:$false
    echo Stop-Service ShellHWDetection
    echo #    Create the fat32 boot partition
    echo $usbfat32=^(New-Partition -DiskNumber $usb -Size 1GB -AssignDriveLetter -IsActive ^| Format-Volume -FileSystem FAT32 -NewFileSystemLabel "BOOT"^).DriveLetter + ":"
    echo #    Create the ntfs intall partition
    echo $usbntfs=^(New-Partition -DiskNumber $usb -UseMaximumSize -AssignDriveLetter ^| Format-Volume -FileSystem NTFS -NewFileSystemLabel "INSTALL"^).DriveLetter + ":"
    echo Start-Service ShellHWDetection
    echo # Read-Host "Eject the iso if it is mounted. When ready press Enter"
    echo # $Volumes = ^(Get-Volume^).Where^({$_.DriveLetter}^).DriveLetter
    echo # Read-Host "Mount the iso. When ready press Enter"
    echo # $ISO = ^(Compare-Object -ReferenceObject $Volumes -DifferenceObject ^(Get-Volume^).Where^({$_.DriveLetter}^).DriveLetter^).InputObject
    echo # $ISO = ^(get-volume^| Where-Object {^($_.DriveType -eq "CD-ROM"^) -and ^($_.filesystemlabel -ne ""^) -and ^($_.OperationalStatus -eq "OK"^)} ^|Out-GridView -Title 'Select Cd-Rom image' -OutputMode Single^).DriveLetter + ":"
    echo # Copy-Item -Path $ISO\* -Destination "$($usbntfs)" -Recurse -Verbose
    echo # Copy-Item -Path $ISO"\bootmgr" -Destination $usbfat32"\" -Verbose
    echo # Copy-Item -Path $ISO"\bootmgr.efi" -Destination $usbfat32"\" -Verbose
    echo # Copy-Item -Path $ISO"\boot" -Destination $usbfat32"\boot" -Recurse  -Verbose
    echo # Copy-Item -Path $ISO"\efi" -Destination $usbfat32"\efi" -Recurse  -Verbose
    echo # new-item $usbfat32"\sources" -itemType Directory
    echo # Copy-Item -Path $ISO"\sources\boot.wim" -Destination $usbfat32"\sources\boot.wim"  -Verbose
    echo robocopy $iso $usbntfs /e
    echo robocopy $iso"\" $usbfat32"\" bootmgr bootmgr.efi
    echo robocopy $iso"\boot" $usbfat32"\boot" /e
    echo robocopy $iso"\efi" $usbfat32"\efi" /e
    echo robocopy $iso"\sources" $usbfat32"\sources" boot.wim
    echo # Eject the mounted iso image
    echo # ^(New-Object -ComObject Shell.Application^).Namespace^(17^).ParseName^($ISO^).InvokeVerb^("Eject"^)
    echo DisMount-DiskImage -ImagePath $ImagePath ^|out-null
    echo Remove-item ^($env:TEMP + "\script.ps1"^)     
    )>"%temp%\script.ps1"
    powershell "%temp%\script.ps1" -ExecutionPolicy Bypass -WindowStyle Normal
    
    
     
  7. rpo

    rpo MDL Expert

    Jan 3, 2010
    1,447
    1,421
    60
    Please replace script line 25 by
    Code:
    echo $wshell=New-Object -ComObject Wscript.Shell
     
  8. rpo

    rpo MDL Expert

    Jan 3, 2010
    1,447
    1,421
    60
    If you prefer, add after script line 25 or 26 or ...
    Code:
    echo $wshell=New-Object -ComObject Wscript.Shell
    since $wshell is referenced but but defined.
     
  9. abbodi1406

    abbodi1406 MDL KB0000001

    Feb 19, 2011
    16,195
    84,746
    340
    Not to sound picky, but it seems easier to handle and update the script without echoing to .ps1 file
    courtsey of @BAU's reg_takeownership
    Code:
    @echo off
    ::AveYo: self-elevate passing args and preventing loop
    set "args="%~f0" %*" & call set "args=%%args:"=\"%%"
    fsutil dirty query %systemdrive%>nul||(if "%?%" neq "y" powershell -c "start cmd -ArgumentList '/c set ?=y&call %args%' -verb runas" &exit)
    title Windows PowerShell
    powershell -NoLogo -NoProfile -WindowStyle Normal -ExecutionPolicy Bypass -C "$f=[io.file]::ReadAllText('%~f0') -split ':ps_usb\:.*';iex ($f[1]);" & exit/b
    
    :ps_usb: [
    [console]::ForegroundColor = "Yellow"
    [console]::BackgroundColor = "blue"
    #
    Add-Type -AssemblyName System.Windows.Forms # Load the class System.Windows.Forms
    # Filebrowser dialog
    $FileBrowser = New-Object System.Windows.Forms.OpenFileDialog -Property @{
       Multiselect = $false # One file can be chosen
       Filter = 'ISO images (*.iso)|*.iso' # Select iso files
    }
    #
    clear-host
    $wshell=New-Object -ComObject Wscript.Shell
    $wshell.Popup("Insert your USB disk before `r`nclicking the OK button.`r`n`r`nIf no USB disk is detected,`r`nthis script will exit.",0,"USB disk") | Out-Null
    #   Select the USB disk
    $USBDiskForm=New-Object System.Windows.Forms.Form # Create the screen form (window)
    # $USBDiskForm |gm;pause
    # Set the title and size of the window:
    $USBDiskForm.Text='Available USB disk(s)'
    $USBDiskForm.Width=500 ; $USBDiskForm.Height=170
    $USBDiskForm.AutoSize=$true # AutoSize property to make the form automatically stretch, if the elements on the form are out of bounds
    # Create a drop-down list and fill it
    $USB=$Null
    $USBDisks=@() # array whith USB disk number
    $Disks=Get-Disk | Where-Object {($_.BusType -eq "USB") -and ($_.OperationalStatus -eq "Online")}
    if ($Disks.count -eq 0){"No USB disk available";exit}
    $USBDiskList=New-Object System.Windows.Forms.ComboBox
    # $USBDiskList|gm
    $USBDiskList.Width=450
    Foreach ($USBDisk in $Disks) {
       $FriendlyName=($USBDisk.FriendlyName).PadRight(30," ").substring(0,30) 
       $Partitions = Get-Partition | Where-Object { $_.DiskNumber -eq $USBDisk.DiskNumber}
       If ($Partitions) {
           Foreach ($Partition in $Partitions) {
           $Volumes=get-volume | Where-Object {$Partition.AccessPaths -contains  $_.path }
           Foreach ($Volume in $Volumes) {
               $USBDisks+=$USBDisk.DiskNumber
               $USBDiskList.Items.Add((" {0,-30}| {1,1}:| {2,-30}| {3:n2} GB" -f $FriendlyName, ($Partition.DriveLetter), $Volume.FileSystemLabel.PadRight(30," ").substring(0,30), ($Partition.Size/1GB)))|out-null
               }     
           }
       } Else {
           $USBDisks+=$USBDisk.DiskNumber
           $USBDiskList.Items.Add((" {0,-30}| {1,1}| {2,-30}| {3:n2} GB" -f $FriendlyName, " ", " ",($USBDisk.Size/1GB)))|out-null
       }
    }
    $SelectTargetUSB=New-Object System.Windows.Forms.Label # Put the SelectTargetUSB label on the form
    $SelectTargetUSB.Location=New-Object System.Drawing.Size(20,5)
    $SelectTargetUSB.Text="Select Target USB";$SelectTargetUSB.Font='Segoe UI,10'
    $SelectTargetUSB.width=120;$SelectTargetUSB.height=20;
    $USBDiskForm.Controls.Add($SelectTargetUSB)
    $USBDiskList.DropDownStyle=[System.Windows.Forms.ComboBoxStyle]::DropDownList;
    $USBDiskList.Location=New-Object System.Drawing.Point(20,30)
    $USBDiskList.SelectedIndex=0
    $USBDiskList.width=450;$USBDiskList.height=20
    $USBDiskList.backColor="blue" ; $USBDiskList.ForeColor="White"
    $USBDiskList.Font='Segoe UI,10'
    $USBDiskForm.Controls.Add($USBDiskList)
    $OKButton=New-Object System.Windows.Forms.Button # Put the OK button on the form
    $OKButton.Location=New-Object System.Drawing.Size(400,80)
    $OKButton.Text="OK";$OKButton.Font='Segoe UI,10'
    $OKButton.width=60;$OKButton.height=30;
    $USBDiskForm.Controls.Add($OKButton)
    $OKButton.Add_Click({$global:OK=$true;$USBDiskForm.close()})
    $CancelButton=New-Object System.Windows.Forms.Button # Put the Cancel button on the form
    $CancelButton.Location=New-Object System.Drawing.Size(320,80)
    $CancelButton.Size=New-Object System.Drawing.Size(120,20)
    $CancelButton.Text="Cancel";$CancelButton.Font='Segoe UI,10'
    $CancelButton.width=60;$CancelButton.height=30;
    $USBDiskForm.Controls.Add($CancelButton)
    $CancelButton.Add_Click({$global:OK=$False;$USBDiskForm.close()})
    [void]$USBDiskForm.ShowDialog() # Display the form on the screen
    if (!$global:OK){$wshell.Popup("Operation cancelled",0,"Error") | Out-Null;exit}
    $USB=$USBDisks[$USBDiskList.SelectedIndex]
    #   Clear the USB stick
    Clear-Disk $usb -RemoveData -RemoveOEM -Confirm:$false
    Stop-Service ShellHWDetection
    #   Create the fat32 boot partition
    $usbfat32=(New-Partition -DiskNumber $usb -Size 500MB -AssignDriveLetter -IsActive | Format-Volume -FileSystem FAT32 -NewFileSystemLabel "BOOT").DriveLetter + ":"
    #   Create the ntfs intall partition
    $usbntfs=(New-Partition -DiskNumber $usb -UseMaximumSize -AssignDriveLetter | Format-Volume -FileSystem NTFS -NewFileSystemLabel "INSTALL").DriveLetter + ":"
    Start-Service ShellHWDetection
    # Read-Host "Eject the iso if it is mounted. When ready press Enter"
    # $Volumes = (Get-Volume).Where({$_.DriveLetter}).DriveLetter
    # Read-Host "Mount the iso. When ready press Enter"
    # $ISO = (Compare-Object -ReferenceObject $Volumes -DifferenceObject (Get-Volume).Where({$_.DriveLetter}).DriveLetter).InputObject
    # $ISO = (get-volume| Where-Object {($_.DriveType -eq "CD-ROM") -and ($_.filesystemlabel -ne "") -and ($_.OperationalStatus -eq "OK")} |Out-GridView -Title 'Select Cd-Rom image' -OutputMode Single).DriveLetter + ":"
    $wshell.Popup("Select the Windows ISO.`r`n`r`nThis script will start copying the installation files`r`nas soon as the ISO is selected.",0,"ISO image") | Out-Null
    #
    [void]$FileBrowser.ShowDialog()
    #
    $ImagePath = $FileBrowser.FileName;
    $ISO=":"
    If($FileBrowser.FileNames -like "*\*") {
    #  Check if iso already mounted
       $ISO = (Get-DiskImage -ImagePath $ImagePath | Get-Volume).DriveLetter
    #   Mount iso
       IF (!$ISO) {Mount-DiskImage -ImagePath $ImagePath -StorageType ISO |out-null
       $ISO = (Get-DiskImage -ImagePath $ImagePath | Get-Volume).DriveLetter}
       $ISO=$ISO+":"
    }
    if ($ISO -eq ":") {$wshell.Popup("No ISO image mounted or operation cancelled",0,"Error") | Out-Null;exit}
    # Copy-Item -Path $ISO\* -Destination "$($usbntfs)" -Recurse -Verbose
    # Copy-Item -Path $ISO"\bootmgr" -Destination $usbfat32"\" -Verbose
    # Copy-Item -Path $ISO"\bootmgr.efi" -Destination $usbfat32"\" -Verbose
    # Copy-Item -Path $ISO"\boot" -Destination $usbfat32"\boot" -Recurse  -Verbose
    # Copy-Item -Path $ISO"\efi" -Destination $usbfat32"\efi" -Recurse  -Verbose
    # new-item $usbfat32"\sources" -itemType Directory
    # Copy-Item -Path $ISO"\sources\boot.wim" -Destination $usbfat32"\sources\boot.wim"  -Verbose
    robocopy $iso $usbntfs /e
    robocopy $iso"\" $usbfat32"\" bootmgr bootmgr.efi
    robocopy $iso"\boot" $usbfat32"\boot" /e
    robocopy $iso"\efi" $usbfat32"\efi" /e
    robocopy $iso"\sources" $usbfat32"\sources" boot.wim
    # Eject the mounted iso image
    # (New-Object -ComObject Shell.Application).Namespace(17).ParseName($ISO).InvokeVerb("Eject")
    DisMount-DiskImage -ImagePath $ImagePath |out-null
    :ps_usb: ]
     
  10. vigipirate

    vigipirate MDL Senior Member

    Feb 24, 2011
    349
    87
    10
    thank script verry works usb simply uefi bios
     
  11. vigipirate

    vigipirate MDL Senior Member

    Feb 24, 2011
    349
    87
    10
    Restore FullControl to Administrators and set owner to TrustedInstaller
    Impossible de convertir la valeur «SERVICE» en type «System.Security.Principal.SecurityIdentifier». Erreur: «La valeur
    n'était pas valide.
    Nom du paramètre : sddlForm »
    Au caractère Ligne:7 : 26
    + ... le 2;$i++){ $usr[$i]=[System.Security.Principal.SecurityIdentifier]$s ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidArgument : :)) [], RuntimeException
    + FullyQualifiedErrorId : InvalidCastConstructorException

    Impossible de convertir l'argument «identity» (valeur «0») de «SetOwner» en type «
    System.Security.Principal.IdentityReference»: «Impossible de convertir la valeur «0» du type «System.Int32» en type «
    System.Security.Principal.IdentityReference».»
    Au caractère Ligne:14 : 34
    + $reg.SetAccessControl($acl); } $sec[0].SetOwner($usr[0]); $reg.SetA ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : NotSpecified: :)) [], MethodException
    + FullyQualifiedErrorId : MethodArgumentConversionInvalidCastArgument
     
  12. freddie-o

    freddie-o MDL Expert

    Jul 29, 2009
    1,375
    2,277
    60

    Thanks, WORKS. Simplifies things. Will do away with powershell script restrictions too. I'll replace OP script.
     
  13. AveYo

    AveYo MDL Expert

    Feb 10, 2009
    1,836
    5,693
    60
    Not to sound picky, but quotes matter :)
    set "args="%~f0" %*" & call set "args=%%args:"=\""%%"
    There must be 2 quotes there for cases were it was downloaded multiple times and windows renamed it to script(1).bat or from directories such as Program Files (x86)
    Another thing, the reason why I prefer reg query HKU\S-1-5-19 over fsutil is that fsutil can trigger a check on actually dirty volumes and it will absolutely kill all I/O performance for who knows how much time, while the script will wait for it to complete. I have lost data because of it once - never again!
    Note that it was S-1-5-20 that could fail on some tweaked crippled systems where the network service was removed, but nobody should be able to remove the local service :)

    Damn, son! I'm using SIDs exactly for multi-language compatibility, but it seems Microsoft monkeys translate sc showsid or even takeown parameters into French :)
    I have fixed the example, but keep in mind that's just an usage example - the snippet itself works just fine even in Chinese, as long as you provide it with a proper SID input (reference for the generic ones). There are various ways of getting users and SIDs, some explored in this mdl topic
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  14. freddie-o

    freddie-o MDL Expert

    Jul 29, 2009
    1,375
    2,277
    60
    #57 freddie-o, Jul 31, 2019
    Last edited: Jul 31, 2019
    (OP)
    So I replaced

    Code:
    set "args="%~f0" %*" & call set "args=%%args:"=\"%%"
    fsutil dirty query %systemdrive%>nul||(if "%?%" neq "y" powershell -c "start cmd -ArgumentList '/c set ?=y&call %args%' -verb runas" &exit)
    with

    Code:
    set "args="%~f0" %*" & call set "args=%%args:"=\""%%"
    reg query HKU\S-1-5-19>nul||(if "%?%" neq "y" powershell -c "start cmd -ArgumentList '/c set ?=y&call %args%' -verb runas" &exit)
    and this window popped up

    Code:
    ERROR: Access is denied.
    before the UAC window.
    But it worked after allowing the script to run.

    Code:
    reg query HKU\S-1-5-19>nul||(if "%?%" neq "y" powershell -c "start cmd -ArgumentList '/c set ?=y&call %args%' -verb runas" &exit)
    caused the ERROR window
     
  15. AveYo

    AveYo MDL Expert

    Feb 10, 2009
    1,836
    5,693
    60
    that's absolutely normal, since you don't have admin rights. then the script will reload after you accept the uac prompt (the reloading is mandatory, by -microsoft- design)
    you can suppress the message by adding a 2>nul after the reg query command:
    reg query HKU\S-1-5-19>nul 2>nul||(if "%?%" neq "y" powershell -c "start cmd -ArgumentList '/c set ?=y&call %args%' -verb runas" &exit)

    And speaking about this 2-liner to self-elevate a batch script passing args and preventing loops, some people complained that powershell is very slow to start the very first time.
    Not sure it matters much, but the same can be done via vbscript and it will be indeed fast at all times, but more prone to being nagged by some lame AV such as Avast=AVG
    You probably heard about rundll32 mshtml + javascript method, but you haven't seen anywhere how it's done with vbscript:
    Code:
    ::AveYo: self-elevate passing args and preventing loop ( introducing vbscript with mshtml 2-liner)
    set "args="%~f0" %*" &set "?A=rundll32 vbscript:window.close("\..\mshtml,RunHTMLApplication "+A=CreateObject("Shell.Application")"
    reg query HKU\S-1-5-19>nul 2>nul||if "%?%" neq "y" %?A%.ShellExecute("cmd.exe","/c set ?=y&call %args:"=""%",,"runas")) &exit
    
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  16. rpo

    rpo MDL Expert

    Jan 3, 2010
    1,447
    1,421
    60
    No, i'm talking about the script proposed with post 47