I want to run a PowerShell command inside itself, after opened the PowerShell console. For example as we create desktop shortcut by DOS command line by CMD. Similarly opened the PowerShell command line console, another PowerShell cmdlet would execute inside that.
Ex:
PS C:> Test-Connection -ComputerName 8.8.8.8 -Count 1000
I want to run a PowerShell command inside itself, after opened the PowerShell console. For example as we create desktop shortcut by DOS command line by CMD. Similarly opened the PowerShell command line console, another PowerShell cmdlet would execute inside that.
Ex:
C:> PowerShell && Test-Connection -ComputerName 8.8.8.8 -Count 1000
PS C:> Test-Connection -ComputerName 8.8.8.8 -Count 1000
powershell.exe, the Windows PowerShell CLI, allows you to:
pass a command to execute via the (positionally implied) -Command parameter.
keep the resulting session open with -NoExit.
Therefore:
PowerShell -NoExit -Command "Test-Connection -ComputerName 8.8.8.8 -Count 1000"
Related
I have a task to remotely run Acrobat Distller (AD) remotely.
I was able to locally run AD in the command prompt:
"C:\Program Files (x86)\Adobe\Acrobat DC\Acrobat\acrodist.exe" /O $OutputFolder $InputFolder\test.ps
I tried invoking the same command using powershell:
powershell.exe -NoExit -Command Invoke-Command -ComputerName $server -ScriptBlock {'"C:\Program Files (x86)\Adobe\Acrobat DC\Acrobat\acrodist.exe" /O $OutputFolder $InputFolder\test.ps'}
When ran, the powershell command did prompt any error BUT it did not generate the expected PDF output as well.
Can I get some assistance on what I am doing wrong here?
Thanks
You probably need to use $using:OutputFolder and $using:InputFolder.
Normally, powershell variables are only set for your session and don't carry over to remote servers. The $Using: format allows you to do this with Invoke-Command.
Execute the remote server parametrized batch file from PowerShell.
Doesn't throw an error nor executed command on remote machine.
$path = "D:\run\test-5.2.bat";
Invoke-Command -ComputerName testserver -Scriptblock { "$path" }
Script inside the bat file is msiexec with parameters, which shall execute through Command Prompt only.
Based on this msdn link, you can run a ps1 script file on remote computers. So if it is possible to "port" the content of the bat file in a ps1 it should work. Here is the msdn example:
Example 11: Run a script on all the computers listed in a text file
PS C:\> Invoke-Command -ComputerName (Get-Content Servers.txt) -FilePath C:\Scripts\Sample.ps1 -ArgumentList Process, Service
This example uses the Invoke-Command cmdlet to run the Sample.ps1 script on all of the computers listed in the Servers.txt file. The command uses the FilePath parameter to specify the script file. This command lets you run the script on the remote computers, even if the script file is not accessible to the remote computers.
When you submit the command, the content of the Sample.ps1 file is copied into a script block and the script block is run on each of the remote computers. This procedure is equivalent to using the ScriptBlock parameter to submit the contents of the script.
Hope that helps
$path is a string. PowerShell simply echoes bare strings instead of executing them, unlike CMD or bash. Use the call operator (&):
& "$path"
or Start-Process:
Start-Process cmd.exe -ArgumentList '/c', $path -NoNewWindow -Wait
to have PowerShell execute a string as a command. Since you say you're running msiexec.exe from the batch script using the latter may be required.
On top of that you have a scope issue. The variable $path inside the scriptblock is not the same as the one in the global scope. You can mitigate that via the using: scope qualifier:
Invoke-Command -Computer testserver -Scriptblock { & "$using:path" }
or by passing $path as an argument to the scriptblock:
Invoke-Command -Computer testserver -Scriptblock { & "$($args[0])" } -ArgumentList $path
My PowerShell script runs a .bat file to install an .msu file. But I need to run this .bat file with Administrator rights.
The .bat file is:
WUSA C:\temp\Win8.1AndW2K12R2-KB3191564-x64.msu /quiet /norestart
I have Domain Controller and a lot of clients. With PowerShell PS session I interactively connect to every client. I need to use this bat file with Domain Admin credentials, how can I do this?
You could use Invoke-Command
You could save the servers in list in a text file and then use the Get-Content command to save the array in a variable:
$clients = Get-Content C:\ExampleClientList.txt
Then use the variable for the ComputerName parameter of Invoke-Command. Then in the scriptblock parameter is where you run the command, since you can run executables in PowerShell there isn't any need for the bat file. Last the Credential parameter will allow you run this as the Local administrator.
Invoke-Command -Computername $clients -ScriptBlock {WUSA C:\temp\Win8.1AndW2K12R2-KB3191564-x64.msu /quiet /norestart} -Credential (Get-Credential)
I am not sure i understand your question.
To start batch file from powershell you can use start-proccess command:
powershell start-process <path to your file> -verb RunAs
I am trying to execute a WSF script file from a PowerShell command with an argument and can not get it to work.
The powershell script I am attempting to use that is failing is:
$rSession = New-PSSession -ComputerName server01
Invoke-Command -ScriptBlock { cscript "\\server1\filepath\script.wsf ARGUMEMT" ; return $LastExitCode} -Session $rSession
Remove-PSSession $rSession
Now, if I run the PowerShell script above with out an argument the script executes as expected Also, if I just run the Cscript portion in cmd.exe with the argument it also works as expected.
Any thoughts on what I am doing wrong when trying to call the WSF file through powershell with the argument?
Thanks,
Phil
I'm having big issues with powershell, web deploy and escaping command paths. I'm trying to run a powershell script before the deployment using the runCommand provider in Web Deploy. However, powershell seems to interpret paths with spaces as seperate parameters even when I encase in double-quotes.
This is what is output if I run
web.deploy.cmd /y
Info: Updating runCommand (powershell.exe -ExecutionPolicy Bypass invoke-command "C:/local dev/mobile/Core/Web services/wsAuthUser/mobilestability/src/wsAuthUser/obj/Debug/Package/PackageTmp/install/installboot.ps1" -computername server01 -argumentlist Mobile2/AuthUser, pre).
But the error is:
Warning: Invoke-Command : A positional parameter cannot be found that accepts a
gument '
Warning: dev/mobile/Core/Web'.
At line:1 char:15
Why is it complaining when the whole path is in quotes?
Update:
If I paste the following into Powershell ISE:
invoke-command "C:\group dev\mobile\Core\Web Services\wsAuthUser\mobilestability\src\wsAuthUser\obj\Debug\Package\PackageTmp\bin\install\installboot.ps1" -computername dev-mob2iis01 -argumentlist Mobile2/AuthUser, pre
It runs fine with no errors.
If I then open a Command prompt, past the same command but pre-pend with powershell and the execution policy parameters like so:
powershell.exe -ExecutionPolicy Bypass invoke-command "C:\localdev\mobile\Core\Web Services\wsAuthUser\mobilestability\src\wsAuthUser\obj\Debug\Package\PackageTmp\bin\install\installboot.ps1" -computername server01 -argumentlist Mobile2/AuthUser, pre
It fails again.
Because it is windows, not unix. So you would use backslashes for file paths, not regular slash.
Update:
You just gotta play around with the quotes to make it work through cmd
powershell.exe -ExecutionPolicy Bypass -command "invoke-command 'C:\localdev\mobile\Core\Web Services\wsAuthUser\mobilestability\src\wsAuthUser\obj\Debug\Package\PackageTmp\bin\install\installboot.ps1' -computername server01 -argumentlist Mobile2/AuthUser, pre"