How to pass argument to Start-Process - powershell

I'd like to run this command
net start "PTV LOXANE xDataServer 1.4.1.067" using Start-Process in powershell with admin rights.
My problem is how to give the quote to ArgumentList.
I've tried this but it doesn't work
Start-Process net -ArgumentList "stop \"PTV LOXANE xDataServer 1.4.1.067\"" -Verb runas -wait -NoNewWindow -PassThru

I've found how to do it. You must double the quotes:
Start-Process net -ArgumentList "start ""PTV LOXANE xDataServer 1.4.1.067""" -wait -PassThru -Verb runas
Now I've got a second question. How can I run this command when calling powershell ?
This doesn't work:
powershell -Command 'Start-Process net -ArgumentList "start ""PTV LOXANE xDataServer 1.4.1.067""" -wait -PassThru -Verb runas'

Related

run multiple bat-files as admin without entering credentials for every bat-file

I have several batch files that I need to run sequentially; every one must be run as admin. This will force me to enter my credentials for every bat-file:
start-process C:\test\test.BAT -verb runas
start-process C:\test\test1.BAT -verb runas
start-process C:\test\test2.BAT -verb runas
Is there a way to run every bat-file, but to enter my credentials just the very fist time?
A nested Start-Process should work. Lasse V. Karlsen suggestion would be the recommended way to go.
Start-Process powershell -Verb RunAs -ArgumentList #'
Start-Process 'C:\test\test.bat'
Start-Process 'C:\test1\test.bat'
Start-Process 'C:\test2\test.bat'
'# -Wait
Or with a loop:
Start-Process powershell -Verb RunAs -ArgumentList #'
'C:\test\test.bat','C:\test1\test.bat','C:\test2\test.bat' |
ForEach-Object { Start-Process $_ }
'# -Wait

How to pass args to aspnet_regiis via powershell

I have 2 scripts:
Launch.ps1
Deploy.ps1
Launch simply runs deploy as administrator:
clear
$scriptPath = split-path -parent $MyInvocation.MyCommand.Definition
$scriptPathToRun = "$scriptPath\Deploy.ps1"
Start-Process -Verb runAs PowerShell -ArgumentList '-noexit','-File', $scriptPathToRun
I am trying to pass arguments to aspnet_regiis, I have tried the following:
Start-Process -NoNewWindow "$env:windir\Microsoft.NET\Framework64\v4.0.30319\aspnet_regiis" -ArgumentList '–ga', 'domian\serviceAccount'
Start-Process -NoNewWindow "$env:windir\Microsoft.NET\Framework64\v4.0.30319\aspnet_regiis" -ArgumentList '–ga domian\serviceAccount'
Start-Process -NoNewWindow "$env:windir\Microsoft.NET\Framework64\v4.0.30319\aspnet_regiis" -ArgumentList #('–ga', 'domian\serviceAccount')
& "$env:windir\Microsoft.NET\Framework64\v4.0.30319\aspnet_regiis" '–ga domian\serviceAccount'
& "$env:windir\Microsoft.NET\Framework64\v4.0.30319\aspnet_regiis" '–ga', 'domian\serviceAccount'
& "$env:windir\Microsoft.NET\Framework64\v4.0.30319\aspnet_regiis" #('–ga', 'domian\serviceAccount')
In all these attempts, aspnet_regiis is run but it appears no args are passed to it because the output is just a listing of available aspnet_regiis parameters.
Can someone point out what I'm missing? Thanks.
The simplest answer is probably to just run the command using the call/invocation (&) operator:
& "$env:SystemRoot\Microsoft.Net\Framework64\v4.0.30319\aspnet_regiis" -ga domain\serviceAccount
If you really wanted to use Start-Process, you should be able to write it this way:
Start-Process "$env:SystemRoot\Microsoft.Net\Framework64\v4.0.30319\aspnet_regiis" "-ga","domain\serviceAccount" -NoNewWindow
The first token on that command line is the executable to run (i.e., -FilePath). The -ArgumentList parameter is an array (i.e., "-ga","domain\serviceAccount").

Call variable from within a string

I'm trying to use Powershell to open AD U&C in another domain (/domain switch) using a variable.
No domain switch used, works (just not the domain I want):
Start-Process powershell -Credential $domvar'\'$id -ArgumentList '-command &{Start-Process mmc -verb Runas -ArgumentList "C:\Windows\System32\dsa.msc"}'
Defining a specific domain in an array works:
Start-Process powershell -Credential $domvar'\'$id -ArgumentList '-command &{Start-Process mmc -verb Runas -ArgumentList #("C:\Windows\System32\dsa.msc" /domain=domain.com)}'
BUT
Adding /domain=$domain does not (double quoting everything in the ArgumentList did not help):
Start-Process powershell -Credential $domvar'\'$id -ArgumentList '-command &{Start-Process mmc -verb Runas -ArgumentList "C:\Windows\System32\dsa.msc" /domain=$domain}'
NOR
Changing domain.com to my variable $domain:
Start-Process powershell -Credential $domvar'\'$id -ArgumentList '-command &{Start-Process mmc -verb Runas -ArgumentList #("C:\Windows\System32\dsa.msc" /domain=$domain)}'
Also, a new window pops up and I see the red error font but I'm not sure how to grab that information. Tried using debugging and stepping into but didn't work.
In powershell single quotes do NOT perform variable substitution. In your case you can probably just change the single quotes around your command to double quotes. For the double quotes inside the quoted string you can include the $ as a literal by preceding it with a backtick.
"-command &{Start-Process mmc -verb Runas -ArgumentList `"C:\Windows\System32\dsa.msc`" /domain=$($domain)}"

Can't start PowerShell script file with credentials of other user

I have a GUI that has been created with PowerShell Studio and exported as a PS1-file. I'm now trying to launch this GUI by calling it with another user's credentials.
When I run the code it doesn't even give an error message. PowerShell pops-up and closes again in seconds and nothing is launched. Follwoing this thread, I think I followed the correct syntax.
$Script = 'S:\Prod\Script center\GUI Script.ps1'
$Credentials = Get-Credential
$powershellArguments = "-file '$Script'", "-noexit", "-verb runas"
Start-Process powershell -Credential $Credentials -ArgumentList $powershellArguments
These ones doesn't work either:
Start-Process powershell -Credential $Credentials -ArgumentList "-noprofile -command &{Start-Process powershell -verb runas -File 'S:\Prod\Script center\GUI Script.ps1'}"
Start-Process powershell -Credential $Credentials -ArgumentList "-noprofile -command &{Start-Process $script -verb runas}"
And this one is asking me the credentials, although they are already saved in the variable $Credentials. However, the powershell console launched is not launched as the user in the Credentials :(
$cmd = 'powershell.exe'
$arguments = "-NoExit", "-NoProfile", "-WindowStyle Maximized", '-NoLogo', "Credential $Credentials", "File '$script'"
Start-Process $cmd -ArgumentList $arguments -Verb runAs
I'm sure it's not related to the GUI script, because this works perfectly fine:
& 'S:\Prod\Script center\GUI Script.ps1'
Any help is greatly appreciated.
Maybe your error is only on argument single quotes $powershellArguments = "-file '$Script'"; double quotes should be used.
Start-Process -FilePath "powershell" -Credential $cred -ArgumentList #("-file 'cred.ps1'") # doesn't work
Start-Process -FilePath "powershell" -Credential $cred -ArgumentList #("-file ""cred.ps1""") # works

argument list command not correct

I used the below that works for wusa.exe but it is not working for pkgMGr.exe - I get an error of "The pkgmgr.exe command is incorrect"
I think it is because of the
WORKS:
Start-Process c:\windows\system32\wusa.exe -ArgumentList "$ItalianHyphenationHotfix /quiet /norestart /log" -Wait
NOT WORKING:
Start-Process c:\windows\system32\pkgmgr.exe -ArgumentList "/ip /m: $ItalianKB2841134Hotfix /quiet /norestart /l:C:\buildlog\complogs\LangPack" -Wait
Any ideas how I can fix this? I tried to put the /ip /m before the -Argument list but that did not work as well. I think it is because of these two commands that is causing the issue.
-ArgumentList should be a string array, so you need to format your parameters as such. Try:
Start-Process c:\windows\system32\pkgmgr.exe -ArgumentList "/ip", "/m:`"$ItalianKB2841134Hotfix`"", "/quiet", "/norestart", "/l:`"C:\buildlog\complogs\LangPack`"" -Wait