Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

One problem leads to another! User help just fixed one issue, which led to another as I continued working on this script. My script runs a check to see if the current user is an Administrator as follows:

If (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator"))
{   
$arguments = "& '" + $myinvocation.mycommand.definition + "'" + $loc
Start-Process powershell -Verb runAs -ArgumentList $arguments
Break
}

The argument for $loc is a parameter that was originally passed via a batch file that is as follows:

@setlocal enableextensions
@cd /d "%~dp0"
SET current="%~dp0
START /WAIT powershell.exe -ExecutionPolicy Bypass -File "JavaInstall.ps1" -loc %current%

At the top of the Powershell file, we have:

param(
[string]$loc
)

This works under two conditions:

1) The user is already an Administrator.
2) The folder this is run from at any point in the traversing does not have a space.

When this is run from a folder such as: C:\Users\User\Desktop\Java Test it suddenly fails. If I Write-Host along the way, I can see that the $loc variable is correct until after the elevation. After it elevates, anything after the space is lost. If I run this directly as an Administrator it of course bypasses the elevation and works even with spaces in the folders, but I do not want to require a right-click run-as scenario. Any ideas what might be causing the loss of text after the space during elevation?

share|improve this question
    
For the paths with spaces problem, it might be because you're missing a closing quote in this line SET current="%~dp0 –  Ƭᴇcʜιᴇ007 Apr 28 at 20:25
    
What's odd is that anything but a single quote in that spot causes it to cut off anything with spaces even before elevation. I may be using that command incorrectly to begin with though, as that seems odd. –  Tim P. Apr 28 at 20:29
    
Maybe just ditch the quotes on that line, and instead wrap %current% with them (in the START line of the batch). –  Ƭᴇcʜιᴇ007 Apr 28 at 20:32
    
Why are you starting the batch script in the first place? Can you not just call Powershell directly with the correct permissions? –  Zoredache Apr 28 at 20:36
    
I thought to run a Powershell script on another computer it was necessary to set the ExecutionPolicy, given we do not have the certifications in place and given the default behavior of Powershell. –  Tim P. Apr 28 at 20:53

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.