Home > database >  Powershell - how to check if installation is running in user or system context
Powershell - how to check if installation is running in user or system context

Time:05-07

I have packaged an application with the App Deployment Toolkit.

I install the application with the command:

Start-Process -FilePath $setupx64 -ArgumentList "/silent /install" -verb runas

That works. But only if the installation is running in the user context and not in the system context. If the installation runs in the system context (via SCCM) I need this command without the parameter -verb runas => otherwise it does not work

Start-Process -FilePath $setupx64 -ArgumentList "/silent /install" 

How can I check in my script if the installation is started in the user context or in the system context?

Thank you!!

BR

CodePudding user response:

Here is an example snippet of code to determine if the current security context is NT AUTHRORITY\SYSTEM:

if ($env:USERNAME -eq "$env:COMPUTERNAME$") {
  # do the thing that's special for the system
}
else {
  # do whatever you want for the user
}

CodePudding user response:

Do you mean, "How can I tell if the user is running as Administrator or not?"

If so, the following code should work.

$StartProcessSplat = @{
    FilePath = $setupx64
    ArgumentList = @(
        "/silent",
        "/install"
    )
}
$User = [Security.Principal.WindowsIdentity]::GetCurrent()
$isAdmin = (New-Object Security.Principal.WindowsPrincipal $User).IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)
If ($isAdmin) { $StartProcessSplat['verb'] = 'runas' }
Start-Process @StartProcessSplat
  • Related