How to run command line argument using PowerShell script - powershell

I am able to install the software from the Windows CMD with the following command
setup.exe -inputFile C:\my_installer_input.txt
However, I want to achieve the same above using the PowerShell script.
I tried same from the PowerShell like this
Start-Process -FilePath "C:\Matlab R2018b\setup.exe" -inputFile "C:\my_installer_input.txt" -ArgumentList "/S
and it fails to run with the obvious reason -inputFile parameter is not available for Start-Process in PowerShell.

PowerShell also runs the native commands directly from PowerShell prompt, which means your command
setup.exe -inputFile C:\my_installer_input.txt
should work directly from the PowerShell prompt.
If you are executing on a remote machine, you can run with Invoke-Command as below.
Invoke-Command -Session $session -ScriptBlock { <YOUR CODE HERE> }
or
Invoke-Command -ComputerName <remote-computername> -ScriptBlock { <YOUR CODE HERE> }

If this is on a remote machine, do something like:
Invoke-Command -Computername ‘x’ -Scriptblock {
Set-Location C:\path\to\file
cmd /c setup.exe /arg1 /arg2
}

Related

Run bat file via PowerShell

I am trying to run a batch file from remote computer but it also doesn't work locally
Invoke-Command -ComputerName $remoteComputer -ScriptBlock {
Start-Process "C:\Users\Administrator\Desktop\version\Installer\Installer.bat" -NoNewWindow -Wait
} -Credential $credentials -ErrorAction Stop
There is no errors but nothing happens
The installer.bat is located under C:\Users\Administrator\Desktop\version\Installer
The batch file contain setup.exe that also located at the same folder.
I am searching for hours the solution
For local I used this:
Start-Process "cmd.exe" "/c C:\Users\Administrator\Desktop\version\Installer\Installer.bat" -NoNewWindow
Seems that its working locally when i am going to the right folder (cd c:..)
and run
Invoke-Expression -Command 'cmd.exe /C C:\Users\Administrator\Desktop\version\Installer\Installer.bat'
So how can i do it remotely (I mean to go to the right path first)

How to make powershell wait for a batch file to complete the execution of all comand on remote-server

$storesess = New-PSSession -ComputerName marshy -Credential marshy001
Enter-PSSession -Session $storesess
Invoke-Command -ScriptBlock {start-process C:\Users\marshmellow\Documents\Some\xyz.bat }
Exit-PSSession
Above is the script which calls a bat file saved on remote server C:\Users\marshmellow\Documents\Some\xyz.bat
The bat file has two commands one which sets the working directory using "pushd" and another which stops a application process. The second command takes a couple of minutes to complete. I have found that the Start-Process doesn't wait for the second command to complete successfully, it just fires the command and closes the process.
Is there any way to make the Start-Process wait for the command to get completed successfully as I have already tried using -Wait which doesn't work.
If there's a way to even open a cmd session on the remote server and pass few commands in it saved in variables and that output is relayed to my PowerShell script even that is fine. can anyone please help?
Using cmd.exe might work?
Invoke-Command -ScriptBlock {
cmd /k "C:\Users\marshmellow\Documents\Some\xyz.bat"
} -ErrorAction Stop
If not, you could probably Start-Process -Wait on cmd.exe, then supply the batch commands as an -ArgumentList
What do you get if you try this?
Invoke-Command -FilePath $PathToBatchFile
You do not need the Invoke-Command cmdlet. Just use Start-Process with the -Wait parameter and pass the correct parameters to cmd:
$storesess = New-PSSession -ComputerName marshy -Credential marshy001
Enter-PSSession -Session $storesess
Start-Process cmd -ArgumentList "/C C:\Users\marshmellow\Documents\Some\xyz.bat" -Wait
Exit-PSSession

Using Invoke-Command to run Start-Process in an elevated session

As a precursor to running an installation file on several remote servers, I need to update the Powershell setting MaxMemoryPerShellMB. This requires running a PS session as Administrator on the remote server. I have been trying to run Invoke-Command which then runs a ScriptBlock consisting of a Start-Process command which includes the -Verb RunAs parameter. Nothing seems to work, however.
I have tried with various quoting schemes, single, double, triple, but nothing seems to work.
I've tried running the Start-Process from an Enter-PSSession, with the same results.
Following is the code I'm testing now:
$creds = Get-Credential -Username 'DOMAIN\userID' -Message "Enter Username and Password to access the remote servers."
$ScriptBlock = {
Start-Process -FilePath Powershell.exe -ArgumentList """Set-Item WSMan:\localhost\Shell\MaxMemoryPerShellMB 1024""" -Verb RunAs -Wait
}
Invoke-Command -ComputerName testsvr01 -Credential $creds -ScriptBlock $ScriptBlock
I should be able to RDP to the remote server and run Get-Item WSMan:\localhost\Shell and have it show the updated value, but the value isn't changed.
When running the code it pauses for a second when the Invoke-Command runs, but other than that, there is no feedback in Powershell.
On the remote server I see the following two Kerberos errors in the System Event log.
0x19 KDC_ERR_PREAUTH_REQUIRED,
0xd KDC_ERR_BADOPTION
Any help is greatly appreciated.
> powershell.exe -?
...
EXAMPLES
...
PowerShell -Command "& {Get-EventLog -LogName security}"
-Command
...
To write a string that runs a Windows PowerShell command, use the format:
"& {<command>}"
where the quotation marks indicate a string and the invoke operator (&)
causes the command to be executed.
So you could try to call Set-Item in the following way:
$ScriptBlock = {
Start-Process -FilePath Powershell.exe -ArgumentList "-Command"," &{ Set-Item WSMan:\localhost\Shell\MaxMemoryPerShellMB 1024 }" -Verb RunAs -Wait -PassThru
}
$process = Invoke-Command -ComputerName testsvr01 -Credential $creds -ScriptBlock $ScriptBlock
$process.ExitCode
I'm also returning a process object via -PassThru on which you might check the `ExitCode``
Hope that helps

How do I display the output of a remote exe locally when using Invoke-Command in Powershell?

I have the following script block:
$scriptBlock = {Start-Process ping.exe -ArgumentList localhost -Wait -NoNewWindow -PassThru}
Note: I am using a process as I want to be able to set the working directory the exe is executed in.
If I invoke it locally like this:
Invoke-Command -ScriptBlock $scriptBlock
I get the full ping output displayed. But when I invoke it remotely like this:
Invoke-Command -ComputerName RemoteComputerName -ScriptBlock $scriptBlock
I don't see any of the ping output. How can I get the remote output to display locally?
Drop the Start-Process and invoke the command directly:
$scriptBlock = {ping.exe localhost}
or via the call operator:
$scriptBlock = {& ping.exe localhost}
If you need to run the command from a particular directory, simply change to that directory before running the command:
$scriptBlock = {
Set-Location 'C:\some\folder'
& ping.exe localhost
}

Invoke-Command with script block is not working on remote machine with no error

I am trying to call a batch file located in local machine executing the below PowerShell command from remote computer.
Invoke-Command -ComputerName XXXXXX -ScriptBlock {
Start-Process "c:\installagent.bat"
} -Credential abc\XXX
It's not giving any error but nothing happened on the remote computer.
If I run the batch file from local machine, it's working fine.
You can't run a local file on a remote host like that. If the account abc\XXX has admin privileges on your local computer (and access to the administrative shares is enabled) you could try this:
Invoke-Command -ComputerName XXXXXX -ScriptBlock {
param($myhost)
Start-Process "\\$myhost\c$\installagent.bat"
} -ArgumentList $env:COMPUTERNAME -Credential abc\XXX
Otherwise you'll have to copy the script to the remote host first:
Copy-Item 'C:\installagent.bat' '\\XXXXXX\C$'
Invoke-Command -ComputerName XXXXXX -ScriptBlock {
Start-Process "c:\installagent.bat"
} -Credential abc\XXX
Also, I'd recommend using the call operator (&) instead of Start-Process for running the batch file:
Invoke-Command -ComputerName XXXXXX -ScriptBlock {
& "c:\installagent.bat"
} -Credential abc\XXX
That way Invoke-Command should return the output of the batch file, giving you a better idea of what's going on.
Or, you could simply use psexec:
C:\> psexec \\XXXXXX -u abc\XXX -c installagent.bat