Doesn't work execute arguments - powershell

I write powershell script, which extract files from sharepoint installation. When I execute this script, I see fileDialog, in which I need choose path.
$path="C:\Script\Download\SharePointServer.exe "
$arguments = "/extract "
start-process $path $arguments -wait -verb runas
But when I execute this script, I see error: Usage: /extract:path, extract the content of the package...
$path="C:\Script\Download\SharePointServer.exe "
$arguments = "/extract ""C:\Script\Download"""
start-process $path $arguments -wait -verb runas
So how it's fix?

You are almost there:
$path="C:\Script\Download\SharePointServer.exe"
$arguments = "/extract C:\Script\Download"
& $path $arguments;
Hope it works ;)

Related

Powershell: Start-Process with an argument

I want to start a setup.exe with one install parameter => /download example.xml
When I tpye in "C:\Temp\folder\setup.exe /download example.xml" in Windows Explorer Address Bar the setup.exe starts correctly.
How do I do that with Powershell?
I've tried the following:
$setup="C:\Temp\folder\setup.exe "
$Argument = "/download example.xml"
Start-Process $setup -ArgumentList $Argument
What am I doing wrong?
Thanks!
I think it wants a list of arguments. This might do the job
$setup="C:\Temp\folder\setup.exe "
$ArgumentLst = #("/download example.xml")
Start-Process $setup -ArgumentList $ArgumentLst

Powershell-Command to download a file with admin rights

I try do download a file via a powershell command. The command I use is simple:
Start-BitsTransfer -Source 'https://download.com/file.zip' -Destination 'E:\test\file.zip'
I can run the command in PS succesfully. But now I want to run it with elevated rights. So I gooogled and found this solution:
There it says the command should be:
Start-Process powershell.exe -Verb Runas -ArgumentList "-Command & {get-process}"
So I tried adjusting it for my use case:
Start-Process powershell.exe -Verb Runas -ArgumentList "-Command & {Start-BitsTransfer -Source 'https://download.com/file.zip' -Destination 'E:\test\file.zip'}"
But all is does is open a new PS-Window and closing it right after. Where is my mistake?
You can change to this
Start-Process powershell.exe -Verb Runas -ArgumentList "& {Start-BitsTransfer -Source 'https://download.com/file.zip' -Destination 'E:\test\file.zip'}"
Note the window will close after the execution completes. If you would like to see the output/errors (such as what would be shown in your non working example) just add another command to pause.
Start-Process powershell.exe -Verb Runas -ArgumentList "& {Start-BitsTransfer -Source 'https://download.com/file.zip' -Destination 'E:\test\file.zip';pause}"
& is used to invoke a command. It's useful for executing strings or scriptblocks. It runs in a child runspace.
& 'Get-Host'
& 'Write-Host' Hello -Fore Green
& {Write-Host Goodbye -Fore Cyan}
; is used to separate different commands on the same line.
& {$name = 'Doug';Write-Host Hello $name}
You can also use a period to invoke a scriptblock in the current runspace. In the previous command the $name variable would be empty in the callers scope where the following command would leave the variable defined.
& {$name = 'Doug';Write-Host Hello $name}
$name # empty as it all happens in the child scope
vs
. {$name = 'Doug';Write-Host Hello $name}
$name # populated because it's brought into the caller's scope

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").

How to handle spaces in external process

I am trying to start a powershell process with spaces, see the below examples:
Working:
$path = "C:\Windows\System32\notepad.exe"
Start-Process -FilePath powershell.exe -ArgumentList "& $path"
Not working (with spaces):
$path = "C:\Program Files (x86)\CustomApp\Server.exe"
Start-Process -FilePath powershell.exe -ArgumentList "& $path; Read-Host"
Using Read-Host the error message is displayed before the console exits: The term 'x86' is not recognized as the name of a cmdlet, function, script file, or operable program.
How can I handle the spaces with Start-Process?
Place your path also in single quotes:
$path = "'C:\Program Files (x86)\CustomApp\Server.exe'"
Start-Process -FilePath powershell.exe -ArgumentList "& $path; Read-Host"

Starting an admin shell then executing multiple commands

I have a powershell script and a bat file that launches it. I want the bat file to open powershell, then have powershell start another shell with elevated privileges, then run two commands. First command is change directory, second command is start a powershell script.
So far I have this:
powershell -NoProfile -ExecutionPolicy ByPass -Command "& {Start-Process PowerShell -Verb RunAs -ArgumentList '-NoExit -NoProfile -ExecutionPolicy Bypass cd %~dp0 .\App\Deploy-Application.ps1}'"
This is the section I'm having problems with:
cd %~dp0 .\App\Deploy-Application.ps1
I want to run these two commands but I'm not sure how. It runs a single command. I tried adding a semicolon between the commands but it didn't work.
Made a quick test and this is what i got working:
Test.bat
cd %~dp0
powershell -NoProfile -Command ".\test.ps1"
Test.ps1
If (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator"))
{
$arguments = "-noprofile & '" + $myinvocation.mycommand.definition + "'"
Start-Process powershell -Verb runAs -ArgumentList $arguments
Break
}
Write-Host "Rawr"
Pause
If i run the batch file, it opens the powershell script that then checks if the current window is being run as an administrator and if not, reopens the script as an administrator.
After which it displays Rawr on my screen.
In your case instead of the Write-Host you could put
If (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator"))
{
$arguments = "-noprofile & '" + $myinvocation.mycommand.definition + "'"
Start-Process powershell -Verb runAs -ArgumentList $arguments
Break
}
cd <Your directory to change to here>
<run command here>
Pause