0

I am trying to run script to manage some VHD Disks, but the disk mount is failing due to elevated permissions required. The user the script is run under is a local admin, but UAC is blocking it I think. The error which comes back is: “DiskState=Failed to mount disk - "Access to a CIM resource was not available to the client”

Ideally I need to the script to run under elevated command prompt automatically. Any idea's how I can achieve that programmatically?

The script I am running is this:

$location = "C:\temp"
$name = "downloadfile"
$Author = "FSLogix"
$FilePath = "Filepath here"
$LogFilePath = "Logfilepath here"

# Force to create a zip file 
$ZipFile = "$location\$Name.zip"
New-Item $ZipFile -ItemType File -Force

$RepositoryZipUrl = "https://github.com/FSLogix/Invoke-FslShrinkDisk/archive/master.zip"

# download the zip 
Write-Host 'Starting downloading the GitHub Repository'
Invoke-RestMethod -Uri $RepositoryZipUrl -OutFile $ZipFile
Write-Host 'Download finished'

#Extract Zip File
Write-Host 'Starting unzipping the GitHub Repository locally'
Expand-Archive -Path $ZipFile -DestinationPath $location -Force
Write-Host 'Unzip finished'
 
# remove the zip file
Remove-Item -Path $ZipFile -Force

# Run the FSLogix Optimisation
C:\temp\Invoke-FslShrinkDisk-master\Invoke-FslShrinkDisk.ps1 -Path $FilePath -Recurse -PassThru -LogFilePath $LogFilePath\logfile.csv
6
  • You have two options, not running if elevated privileges are not present or prompting UAC for elevation if permissions aren't present. Which one do you want? Commented Mar 25, 2021 at 9:28
  • The script needs to be run via an automated tasks so prompting for UAC won't be possible. Any ways around there? There must be a way. Commented Mar 25, 2021 at 9:30
  • Nope. Simple answer: you can't use administrator privileges without administrator perms when running, that simply would be really bad if that was possible. If you are automating this, how are you running the script? Commented Mar 25, 2021 at 9:31
  • The user running the script does have local administrator rights, but by default the Powershell scripts doesn't run under elevated permissions. It's being run via a scripted action by something called Nerdio. Commented Mar 25, 2021 at 9:37
  • hmmm I don't think there is anyway to by default run powershell as administrator but you might want to check the documentation for nerdio to see if there is a run with administrator privileges option Commented Mar 25, 2021 at 9:39

1 Answer 1

0

You can elevate the PS script using the Powershell as a separate process and make it "run as admin" like below:

start-process PowerShell -verb runas

OR

Powershell -Command "Start-Process PowerShell -Verb RunAs"

Apart from that , you can condition it as well. There is a beautiful conditional code shared by PGK which can help as well:

if (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator"))  
{  
  $arguments = "& '" +$myinvocation.mycommand.definition + "'"
  Start-Process powershell -Verb runAs -ArgumentList $arguments
  Break
}
2
  • Shouldn't that be exit rather than Break to close the un-elevated session?
    – Theo
    Commented Mar 25, 2021 at 10:08
  • Yup. Exit will do @Theo Commented Mar 25, 2021 at 11:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.