I'm trying to create a PS1 script to restart a service in a computer, where the user has no admin right, so i'm trying to execute as de admin user, now i'm trying first to open Notepad, but when I execute this script:
# Define el nombre del servicio, usuario y contraseña
$ServiceName = "ApacheDtdlIdServer"
$AdminUser = "User"
$AdminPassword = "C0ntraseña"
$startWithElevatedRights = "notepad"
# Convierte la contraseña en un SecureString
$SecurePassword = ConvertTo-SecureString $AdminPassword -AsPlainText -Force
# Crea el objeto de credenciales
$credentials = New-Object System.Management.Automation.PSCredential ($AdminUser, $SecurePassword)
$ps = Start-Process -PassThru -FilePath powershell -Credential $credentials -ArgumentList '-noprofile -command &{Start-Process ', $startWithElevatedRights, ' -Wait -verb runas}'
$ps.WaitForExit()
I get the error:
Start-Process: This command cannot be executed due to the error: The directory name is > invalid. At C:\Users\IsaacSanzIT\Desktop\Rider\RestartScanner.ps1: 13 Character: 7
I've tried the things that says in other questions in StackOverflow, like this one using the Runas, anyone know how to execute the notepad, or even restart a a service using the admin account? Thanks!
"& { ... }"in order to invoke code passed to PowerShell's CLI via the-Command(-c) parameter - just use"..."directly. Older versions of the CLI documentation erroneously suggested that& { ... }is required, but this has since been corrected.