Running a .bat File as Admin from Powershell - powershell

So I have been spinning my wheels on this one. I'm creating a small application in powershell gui that needs to run a .bat by passing credentials inputted from a textbox. What I have been trying to figure out is how pass these credentials when a button is clicked on the GUI.
I have two boxes on the GUI where the user passes there credentials.
$userTextBox.Text
$passwordTextBox.Text
Then when the button is clicked the .bat file needs to runas user\password. This below is more like psuedo code because I'm not sure how to do this at this point. I have looked online and on safari books but I can not find an example. I did find one but it was doing something different and I did not understand it.
$StartButton.Add_Click({Start-Process
runas $userTextBox.Text\$passwordTextBox.Textc:\temp\Scripts\MapCopyTuner.bat
})
Any help is much appreciate, as you can tell I'm very green here.

You would need to convert the username\password as PSCredential, and pass it to Start-Process
Here is a sample powershell snippet (you can make this less verbose this by inlining variables if you wish).
$password= convertto-securestring $passwordTextBox.Text -asplaintext –force
$credential = new-object -typename System.Management.Automation.PSCredential -argumentlist $userTextBox.Text,$password
$script = "c:\temp\Scripts\MapCopyTuner.bat"
Start-Process powershell -Credential $credential -ArgumentList "-noprofile -command &{Start-Process $script -verb runas}"

Related

Powershell Start-Process -NoNewWindow not working with -Credentials

I am looking to run an exe from powershell using a credential. I want the output to be in the same window. This is how my powershell looks.
Start-Process documentation: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/start-process?view=powershell-6
$username = 'user'
$password = 'password'
$credentials = New-Object System.Management.Automation.PSCredential -ArgumentList #($username,(ConvertTo-SecureString -String $password -AsPlainText -Force))
$pathNew = "c:\pathtomyexe\text.exe"
Start-Process $pathNew -NoNewWindow -Credential ($credentials) -PassThru -Wait
With -Credential ($credentials) a new window is launched.
When I run Start-Process without -Credential, I get result in the same window as expected.
Start-Process $pathNew -NoNewWindow -PassThru -Wait
What am I doing wrong? Any pointers?
Short answer, you aren't doing anything wrong. You just won't be able to do this.
When you run Start-Process -NoNewWindow without -Credential you say: with the current already authenticated credentials, run the executable, and return the results within the same console window.
When you run Start-Process with -Credential the first question is: how do you verify that the Credentials are valid? You can't just look at the username and assume that you can re-use the existing session (e.g. the password might be wrong). To validate the credentials, Start-Process launches a new process as the username/password provided in the Credential object. It performs an authentication check and get a new authentication ticket.
Since it's a new process, running under a completely new context, with new authentication ticket, it ignores the -NoNewWindow flag as there is no way for the current console to redirect the output of the new process, and launches it as a new window.

Execute bat file from powershell with administrative credentials? [duplicate]

So I have been spinning my wheels on this one. I'm creating a small application in powershell gui that needs to run a .bat by passing credentials inputted from a textbox. What I have been trying to figure out is how pass these credentials when a button is clicked on the GUI.
I have two boxes on the GUI where the user passes there credentials.
$userTextBox.Text
$passwordTextBox.Text
Then when the button is clicked the .bat file needs to runas user\password. This below is more like psuedo code because I'm not sure how to do this at this point. I have looked online and on safari books but I can not find an example. I did find one but it was doing something different and I did not understand it.
$StartButton.Add_Click({Start-Process
runas $userTextBox.Text\$passwordTextBox.Textc:\temp\Scripts\MapCopyTuner.bat
})
Any help is much appreciate, as you can tell I'm very green here.
You would need to convert the username\password as PSCredential, and pass it to Start-Process
Here is a sample powershell snippet (you can make this less verbose this by inlining variables if you wish).
$password= convertto-securestring $passwordTextBox.Text -asplaintext –force
$credential = new-object -typename System.Management.Automation.PSCredential -argumentlist $userTextBox.Text,$password
$script = "c:\temp\Scripts\MapCopyTuner.bat"
Start-Process powershell -Credential $credential -ArgumentList "-noprofile -command &{Start-Process $script -verb runas}"

Start-Process powershell credentials don't work but when using cmd it does

I am trying to run a powershell script from another powershell script passing in the credentials of a different user and then using the credentials:
Start-Process powershell.exe -Credential "LON\my-user" -NoNewWindow -ArgumentList "-file C:\DevopsScripts\stuckApps.ps1"
I have this is numerous different ways all get the same error. I have tried setting the username and password before the command:
$username = "LON\my-user"
$password = "pass"
$PSS = ConvertTo-SecureString $password -AsPlainText -Force
$cred = new-object system.management.automation.PSCredential $username,$PSS
$env:USERNAME
Start-Process powershell.exe -Credential $cred -NoNewWindow -ArgumentList "-file C:\DevopsScripts\stuckApps.ps1"
But everything I try gets the error:
Start-Process : This command cannot be run due to the error: The user name or password is incorrect.
I know the username and password are correct as they have been tested on the cmd which it works fine:
C:\Users\ADM-me>runas /noprofile /user:LON\my-user"powershell.exe C:\DevopsScripts\stuckApps.ps1"
What am I doing wrong here and how could I fix this, preferably by setting the password beforehand, so this can be automated. Also this does not need to be done using Start-Process, just this is the closest thing I could find to working.
I think the problem I am having is this, in stuck apps it has this:
$conn = New-Object System.Data.SqlClient.SqlConnection
$conn.ConnectionString = "Server = mssql.co.uk; Database = mydata; Integrated Security = true;"
$conn.Open()
I need this to run the credentials that I am trying to pass through it or else I get this error.
`Exception calling "Open" with "0" argument(s): "Login failed. The login is from an untrusted domain and cannot be used with Windows authentication."
But I can't pass the credentials through as the only ones that work are admin ones, (which I have but then that will throw the error above). Is it possible for me to use the admin logins to access stuck apps then use the logins needed to connect on stuck apps as an AD login.
Your first attempt with -Credential "LON\my-user" can't work, but your second attempt is correct, building the object of class PSCredential, as required (see the type in Get-Help Start-Process -Parameter Credential, it is PSCredential and not String). I tried the same with some reused code here, and it works here both or CMD and PS1 calling a PS1 test script via Powershell.exe, using a local test account (sorry, no domain #home).
So even though my code ist not identical and the domain of the user is the local machine, the approach is the same compared to yours and - sorry that this does not solve your problem - I don't see that you are doing sth. wrong.
To play safe, please make sure though to test with the same Powershell version, the below scripts executed under W10 1607 (so Powershell 5.1.14393.1198), all scripts in the same directory.
testscript.ps1
write-host "Testscript is run with user: $($env:USERNAME)"
Start-Sleep 2
testrun.cmd
runas /noprofile /user:%COMPUTERNAME%\myaccount "powershell.exe -NoProfile -ExecutionPolicy ByPass -file %~dp0testscript.ps1"
testrun.ps1
$Username = "$($env:COMPUTERNAME)\myaccount"
$Password = 'mypassword'
$SecurePassword = ConvertTo-SecureString -String $Password -AsPlainText -Force
$ScriptFile = Join-Path -Path $PSScriptRoot -ChildPath 'testscript.ps1'
$Credential = New-Object System.Management.Automation.PSCredential( $Username, $SecurePassword)
$StartOpts = #{ 'FilePath' = 'powershell.exe'
'Credential' = $Credential
'NoNewWindow' = $false
'ArgumentList' = #( '-f', $ScriptFile,
'-ExecutionPolicy', 'Bypass',
'-NoProfile'
)
}
Start-Process #StartOpts
Some remarks on testrun.ps1
Don't mind the parameters for Start-Process being passed as a hashtable, it's just better readable for me, otherwise it makes not difference
The ArgumentList is being passed as a string array here - I prefer it this way so that it is automatically taken care for double qouting parameters, e.g. when the pathname of the script directory would contain spaces
The parameter -NoNewWindow passed to Start-Process seems not to have any effect here - a new window is opened
I always recommend to add the parameters -Noprofile and -ExecutionPolicy Bypass when using Powershell.exe to launch scripts or execute commands, just to make sure it works despite of the Execution Policy set or any present user or machine profile scripts.
However, at least the parameter -NoProfile seems not to work the same when Powershell.exe is being called fom the above CMD or PS1. Called from PS1, my machine profile gets nevertheless executed, but not fom CMD... interesting! The MSDN: PowerShell.exe Command-Line Help just says about this parameter: "Does not load the Windows PowerShell profile." Funny! There are six of them, see Technet: Understanding the Six PowerShell Profiles. I use "Current User, Current Host – console" and "All Users, Current Host – console". Lesson learned, but I am not sure if it's a bug or a feature.

Calling another powershell script from first one

I am a very newbie to powershell.
I just learnt scripting hello world in power shell.
But, I have a task which i need to complete.
From my hello world powershell (Say 'Script One') , I want to call another powershell (say 'Script Two'), which I am able to do.
But in Script Two, I want to pass different credentials.
So Script One should call Script Two with the credentials I mention.
Can anyone please help me up.
Script One (My first script script) :
Write-Host “Hello, World!”
invoke-expression -Command "C:\Scripts\Script Two.ps1" **[BUT CALL WITH THE CREDENTIALS I WANT]**
Try this in your first script :
$credential = New-Object System.Management.Automation.PsCredential("OTHERLOGIN", (ConvertTo-SecureString "OTHERPASSOWRD" -AsPlainText -Force))
Start-Process powershell.exe -Credential $credential -NoNewWindow -ArgumentList "-File 'C:\Scripts\Script Two.ps1'"
But storing credendials in a script is a bad habit, so you could use this instead (will prompt you for login/password for the second script) :
$credential = Get-Credential

Run ScriptBlock with different credentials

I have a script, that determines a userid; once I have that userid, I want to run a script block against that userid using different credentials. Is this possible? Can anyone show me examples of this?
I got it, thanks to Trevor Sullivan for pointing me in the right direction. I ended up just putting my second ps1 file into a scriptblock, and running it as a job, and passing it the arguments from the main script, like this
$job = Start-Job -scriptblock {
param ($username)
some code to run against the variable that was passed in
} -Args $target -credential $Cred
$target being the variable I want to pass to my scriptblock.
$username being the parameter that the scriptblock accepts Thanks.
I know this was answered a long time ago, but I thought I'd add another option for those looking that returns data without having to retrieve it.
We can create a helper script that creates a pscredential and then uses it to start a local PSSession to run a script or scriptblock in a different user's context. You need to get the user password from somewhere, preferably entered as a secure string or retrieved from a Key Vault, but for the example our helper script will take it as a string parameter.
Script contents:
param ([string]$username,[string]$password)
$Username = 'username#domain.com'
$Password = ConvertTo-SecureString -String $password -AsPlainText -Force
$Credential = New-Object -Type PSCredential($Username,$Password)
$Session = New-PSSession -Credential $Credential
Invoke-Command -Session $Session -FilePath C:\Path\to\some\script.ps1
You can also use -ScriptBlock instead of -FilePath if you have a simple chunk of code to run or you have converted a script to a script block.
Hope this helps somebody out!
Security context for a session is established when the session is initialized. You can't arbitrarily run commands under a different context within the session. To run under a different security context (set of credentials) you'll need to initialize a new session under those credentials and run it there.
If you look at the help for Invoke-Command, you'll note that the -Credential parameter is only valid in parameter sets that specify a remote session by computername, uri, or session. You can also use -credential with Start-Job, which will run the command in a new session on the local machine.
This code will launch PowerShell in Administrator mode using the credentials provided and then run the code in the script block. There might be others ways but this works for me.
$account= # AD account
$password = # AD user password
$passwordSecure = ConvertTo-SecureString ($password) -AsPlainText -Force
$Cred = New-Object System.Management.Automation.PSCredential ($account, $passwordSecure)
$ScriptBlock = {
whoami
start-sleep 3
}
 
# Run PowerShell as Administrator with Custom Crednetails
start-Process powershell.exe -Credential $Cred -ArgumentList "-Command Start-Process powershell.exe -Verb Runas -ArgumentList '-Command $ScriptBlock'" -Wait