damienvanrobaeys / Run-in-Sandbox

Run PS1, VBS, CMD, EXE, MSI, Intunewin, MSIX, or extract ISO, ZIP in Windows Sandbox very quickly just from a right-click
https://www.systanddeploy.com/2023/06/runinsandbox-quick-way-to-runextract.html
698 stars 82 forks source link

add PSEdition Desktop requirement #28

Open ImportTaste opened 1 year ago

ImportTaste commented 1 year ago

I ran Add-Structures.ps1 on PowerShell 7 without knowing any better, which doesn't offer the Get-WmiObject or Checkpoint-Computer commands.

#Requires -PSEdition Desktop will prevent PowerShell 7 from running these scripts since it will require the Windows-specific version of PowerShell: 5.1

ImportTaste commented 1 year ago

@damienvanrobaeys Should this be closed too?

Joly0 commented 1 year ago

As a note from my side: I think it would be smarter not to block powershell 7 but rather adjust the scripts to use both. For get-wmitobject the get-ciminstance cmdlet could be used instead, they are quite interchangeable. Regarding the checkpoint-computer cmdlet maybe check if the script is run in powershell >=7 or >=5

if($IsCoreCLR) {
    # im running in pwsh
}
else {
    # running in windows powershell
}

should do the trick for cases where there is no equivalent cmdlet for newer versions of powershell and then give a notification to the user

ImportTaste commented 1 year ago

I mean, sure, but this would be a good stopgap until that's sorted out.

ImportTaste commented 1 year ago

@damienvanrobaeys How about running Checkpoint-Computer by directly calling powershell.exe and then checking the error code? That would give it compatibility with pwsh.

Joly0 commented 1 year ago

Imo this is quite a smart idea, could you implement that?

Unfortunately it wont change the fact, that get-wmiobject is not available in pwsh, so we would still need a workaround for that, but i could try to implement that and make a separate pr for it

damienvanrobaeys commented 1 year ago

I'm working on it to run checkpoint-computer using powershell if currently running in pwsh

damienvanrobaeys commented 1 year ago

Solved this with this: $Checkpoint_Command = 'Checkpoint-Computer -Description "Windows_Sandbox_Context_menus" -RestorePointType "MODIFY_SETTINGS" -ErrorAction Stop' $retValue = Start-Process powershell -WindowStyle Hidden -ArgumentList $Checkpoint_Command -wait -PassThru If($retValue.ExitCode -eq 0) { Write_Log -Message_Type "SUCCESS" -Message "Creation of restore point "Add Windows Sandbox Context menus"" } Else {
Write_Log -Message_Type "ERROR" -Message "Creation of restore point "Add Windows Sandbox Context menus"" }

Joly0 commented 1 year ago

Why the hassle with if/else? Just use try/catch and you dont need a separate variable for the whole command

ImportTaste commented 1 year ago

Imo this is quite a smart idea, could you implement that?

Unfortunately it wont change the fact, that get-wmiobject is not available in pwsh, so we would still need a workaround for that, but i could try to implement that and make a separate pr for it

There shouldn't be anything that Get-WmiObject can do that Get-CIMInstance can't, to my knowledge, and Get-CIMInstance is compatible with both.

Joly0 commented 1 year ago

Ye, that is true. So i guess try/catch for checkpoint-computer and get-ciminstance should make this compatible with pwsh and remain it with powershell

damienvanrobaeys commented 1 year ago

I updated add_structure.ps1 with checkpoint-computer running with start-process powershell for al versions