GithubHelp home page GithubHelp logo

Comments (3)

undergroundwires avatar undergroundwires commented on June 3, 2024

Hi @MilesFarber, thanks for the report and recommendation in your project. It's appreciated <3.

The existing code should already run the process automatically as administrator if admin is not detected.

If we look at this line:

PowerShell Start -Verb RunAs '%0' 2> nul

%0 is the script path. Single quotes are used to handle paths with spaces included. -Verb RunAs prompts for administrator privileges.

Did this not work for you? In that case what was the full absolute path of the script you were executing? You can also remove 2> nul and re-run the script to see what was causing the error.

We run today a lot of powershell scripts, but need to inline them to be able to properly execute them using cmd (batchfile). This makes the code harder to read. This can be acceptable for individual scripts but preferable to keep the global code as readable as possible (i.e. batchfile). The reason to use batchfile is that they're simple to execute without being affected by execution policies of the host operating system. This makes the generated scripts more accessible and easier to execute by many.

from privacy.sexy.

MilesFarber avatar MilesFarber commented on June 3, 2024

When i tried to run it it said "Right-click on the script and select "Run as administrator"." Then "Press any key to continue..." but now i cannot reproduce it anymore akdfjlghspfhmbfh ANGY

from privacy.sexy.

undergroundwires avatar undergroundwires commented on June 3, 2024

😃 It can be due to some naming in the path.

PowerShell solution is well-written, and much more robust than this "clever" (hacky) way.

It also allows exiting with same exit code as the previous running process, showing proper error messages etc.

But I'm thinking from the users perspective, would people appreciate more robust approach here with the values given, or readable?

Because right know it's pretty simple to read and follow, but it will get ugly with inlined powershell.

For example, following code is inlined powershell for deleting directory contents (look at how ugly it is):

:: Clear directory contents  : "%LOCALAPPDATA%\Microsoft\Windows\INetCache\IE"
PowerShell -ExecutionPolicy Unrestricted -Command "$pathGlobPattern = "^""$($directoryGlob = '%LOCALAPPDATA%\Microsoft\Windows\INetCache\IE'; if ($directoryGlob.EndsWith('\*')) { $directoryGlob } elseif ($directoryGlob.EndsWith('\')) { "^""$($directoryGlob)*"^"" } else { "^""$($directoryGlob)\*"^"" } )"^""; $expandedPath = [System.Environment]::ExpandEnvironmentVariables($pathGlobPattern); Write-Host "^""Searching for items matching pattern: `"^""$($expandedPath)`"^""."^""; $deletedCount = 0; $failedCount = 0; $foundAbsolutePaths = @(); Write-Host 'Iterating files and directories recursively.'; try {; $foundAbsolutePaths += @(; Get-ChildItem -Path $expandedPath -Force -Recurse -ErrorAction Stop | Select-Object -ExpandProperty FullName; ); } catch [System.Management.Automation.ItemNotFoundException] {; <# Swallow, do not run `Test-Path` before, it's unreliable for globs requiring extra permissions #>; }; try {; $foundAbsolutePaths += @(; Get-Item -Path $expandedPath -ErrorAction Stop | Select-Object -ExpandProperty FullName; ); } catch [System.Management.Automation.ItemNotFoundException] {; <# Swallow, do not run `Test-Path` before, it's unreliable for globs requiring extra permissions #>; }; $foundAbsolutePaths = $foundAbsolutePaths | Select-Object -Unique | Sort-Object -Property { $_.Length } -Descending; if (!$foundAbsolutePaths) {; Write-Host 'Skipping, no items available.'; exit 0; }; Write-Host "^""Initiating processing of $($foundAbsolutePaths.Count) items from `"^""$expandedPath`"^""."^""; foreach ($path in $foundAbsolutePaths) {; if (-not (Test-Path $path)) { <# Re-check existence as prior deletions might remove subsequent items (e.g., subdirectories). #>; Write-Host "^""Successfully deleted: $($path) (already deleted)."^""; $deletedCount++; continue; }; try {; Remove-Item -Path $path -Force -Recurse -ErrorAction Stop; $deletedCount++; Write-Host "^""Successfully deleted: $($path)"^""; } catch {; $failedCount++; Write-Warning "^""Unable to delete $($path): $_"^""; }; }; Write-Host "^""Successfully deleted $($deletedCount) items."^""; if ($failedCount -gt 0) {; Write-Warning "^""Failed to delete $($failedCount) items."^""; }"

I think we should keep the issue with another name, but I'm not sure if it's good to do this or not due to mentioned concern.

from privacy.sexy.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.