Piping not working inside powershell script - powershell

I am trying to automate the restart process of some applications with the help of PowerShell.
Basically, my main goal is to execute this line within the script:
'passwordOfCommand' | .\mycommand restart
When I just paste it to the PowerShell command prompt it works fine. But when I add this line within the script (for example script.ps1 ) it does not work.
Is there any solution for that?

Related

Command prompt not executing powershell scripts

I tried running a build today and found that my command prompt is not able to execute powershell scripts. To confirm this, I created a test powershell script which simply writes to console. The script works fine when run from powershell, but is not recognizable by the command prompt. Screenshots below.
test script working from powershell
test script not working from command prompt
Any suggestions/explantion as to why this is happening?
Resolved it. I created a profile.ps1 script under ..\Documents\WindowsPowerShell folder to load custom settings for powershell. Removing profile.ps1 from the folder or adding a -NoProfile switch parameter solves this.

How to run an executable through PowerShell without batch files or cmd?

I am currently using batch files to run a set of simulations. Each line in the batch file reads:
"filepath\program.exe" "filepath\simulation.file"
The quotation marks exist to bound any spaces that exist within the file paths. Without any spaces in the file paths, the quotation marks can be removed.
When I run the batch file through PowerShell, using the following command, it works fine:
.\batch.bat
The executable is run and the output is written to the host, as if I was running the same batch file in cmd.
However, I want to ditch the batch files and run the command directly through PowerShell. When I run the following, I get the program to execute, though it doesn't run properly and I don't get anything written to host. It also appears to hang until I use Ctrl+C to cancel.
& "filepath\program.exe" "filepath\simulation.file"
Could you please help me with the following?
Any resources discussing how PowerShell executes batch files.
How to run an executable through PowerShell without using cmd or a batch file and have it write to host.
Have you tried Start-Process?
Start-Process -FilePath "filepath\program.exe" -ArgumentList "filepath\simulation.file" -Wait
PS: The Parameter -Wait is optional.
Although I'm not quite sure why yet, I found out that this issue only occurred while working remotely. Whenever I was connected to the network locally the command ran just fine.
Since I plan to execute the command on PCs that are situated locally on the network, I'll leave it at this for now.
Thanks to everyone who commented!

Batch File - Commands not executing after Powershell command

I have written simple batch script to replace some text in a file, when executed via command line but when those commands are copied to .bat file execution stops after powershell command. Any idea how to execute powershell commands in batch file?
arco444's answer worked for me in comments:
You just call powershell.exe with no parameters so it starts an interactive session. If you were to type exit your batch script would continue. Use the -command switch to execute whatever it is you want to. And please edit your question and include the code there. Also You need to put the powershell command in quotes. i.e. powershell -command "get-content file.cs"

How to Make Objects Persist after Running Powershell ISE Script

This is my script:
Add-Type -Path C:\MyClassLibrary\HidClassLib.dll
$hidDevice = New-Object HidClassLib.CAN_USBPort("test")
$hidDevice.openPort()
When I run this script from the command line in PowerShell ISE it runs properly and adds the HidClassLib.dll to my path.
The $hidDevice variable doesn't persist however, and before I can use it I have to retype those two lines. Is there any way to make it so that the $hidDevice would persist after running the script?
You mean that it doesn't persist between sessions? In that case, add it to your PowerShell profile!
Here's a great article on how to do that:
http://msdn.microsoft.com/en-us/library/windows/desktop/bb613488(v=vs.85).aspx
Just add the line(s) to the applicable profile and it will run every time a shell opens.

Unable to load .ps1 powershell script from Powershell command (console)

I am stuck in this weird problem where I am trying to execute a powershell script from the powershell command prompt. But neither do I get any errors nor the script is loaded.
I have script in C:\temp\myFunction.ps1 (which has a method getMyName() )
I open the powershell command and navigate to this directory and execute
./myFunction.ps1
then there are no errors and return back to the next line in the prompt. But when I try to call the function getMyName - I get error getMyName is not recognised.
I have set the Execution-Policy to Unrestricted, I am running the powershell as Administrator
Try dot sourcing your script:
. .\myFunction.ps1
It's basic problem of Powershell script. Set the Path where you physically saved your file and then execute the Powershell script. One more thing
1. start your command window run as admin.
2. set the Powershell script policy for execution.