I have a powershell script that takes an input parameter (int). the script then updates the status of the service based on this input parameter.
I have been working on this task using the powergui editor .
Screencap from powergui window
Whenever I try to run the script from the command line of powershell file, there is nothing that's being reported by powershell . No output .. nothing.
Screencap from powershell window
Can you please let me know what might be happening here.
Thanks
Maybe you called the programm services.exe from the command line. You might try to rename your function.
You might know the following allready, but did you parse your powershell script before you called your function? This is nessesary to include your function into the shell. You have to invoke D:\ps>.\myScript.ps before you can use the function.
Related
I think that my question is something too easy that you guys will solve in 1 minute.
I'm trying to run a script that have multiple lines of code. But, when I write the first line and hits SHIFT+ENTER it runs the code. I need to write a new line, instead of running what I've wrote.
Anybody knows what should I do (instead killing myself because I'm too dumb) ?
In powershell console there are a few ways to make a new line
A. Shift + Enter : Use this at any point to make a new line
B. The opening of a string " or ' until the closing of the string " or ' : use this when you have a string that you wish to span many lines
C. A pipe | : Use this if you have output that you would like to pass to another command
D. The Back tick (escape char) ` : use this to separate lines for a new command or splitting a command into other lines
If you are new to powershell, I would suggest using Powershell ISE. If its installed you can go to the powershell console and type ISE or go to start and type Powershell ISE. This will be a good place to run scripts and debug as you can add breakpoints to your scripts.
The easiest and best way to do this would be to create the script inside of the PowerSheell ISE program. You can then reference this script and run it in the console by preceding it with a .\script.ps1.
If needed you can create script on the command line by creating and writing to the file from the console.
Open the PowerShell console
Run the following command to create a file New-Item script.ps1
Run the next command as many times as it takes to populate the file Add-Content script.ps1 "My code line here"
Run the code using the script run command .\script.ps1
Now let it be known that the ISE is a much better tool because it allows for debugging of files and testing them on demand. The only downside is it will cache whatever it uses or creates (such as variables or references). If you aren't getting the expected result trying closing and reopening to clear the cache run it from the console in tandem. One last thing to note is that if you use the ISE and it successfully runs there that doesn't mean it will run in the console. Be sure to test thoroughly.
I've been putting my first powershell scripts together for the last couple of hours and I keep seeing an error that I can't seem to get to the bottom of.
I'm using the Powershell ISE tool to write and run the scripts.
To see if its something in my script I've created a super simple test script and I'm seeing the same problem. The entire test script is:
Test;
function Test
{
New-Item C:\Users\jgreen\Desktop\jammer\ -type directory
}
When I hit the Run Script button the error produced is:
Test : The term 'Test' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct
and try again.
If I simply hit the Run Script button again, it work and does the job. I simply do not understand what is wrong. I simply don't get it. Is there a problem with my script or not?
Why would a script that works bomb out the first time after opening the script in PS ISE?
You are calling the function before it is defined. The reason it works the second time, is a result of the first run. When it is ran the first time it's defining the function, so when you run the script the second time it knows what the function is.
You need to declare your function before you invoke it. It works the second time because then it's been declared. Think of how this would work if you were just at a powershell command line and you typed: "Test;" What would you expect to happen?
I am trying to run a cmd file from MATLAB but unable to execute it. Can anybody see nay problem in the below code?
this is what I have inside my cmd file:
echo on
>test.log 2>&1 (
C:/testProj/Make/makeit.cmd param1
)
And this is the MATLAB code:
Out = 'C:/testProj/test.cmd';
system(Out);
But this actually does not run the cmd file.
Well for somereason it would not run if i would give the complete path of the cmd in bat file. so I had a cd command to change the directory and then run. now it runs fine, Thanks all appreciate your help!
What about using eval, like this:
eval(['!test.cmd']);
I have succesfully used this to run .bat files (and this output of the .bat script showed in my matlab command line). I also found this dos command, but I am not sure if it works allright:
You can just type the following strings to get things down:
!(c:/testProj/test.cmd)
This is actually no different from
system('c:/testProj/test.cmd')
I think you should check if the path is wrong. As to your code in the cmd file, that's beyond my ability to help.
I downloaded the following PowerShell script from this page. This script is supposed to generate random passwords:
http://blog.simonw.se/powershell-generating-random-password-for-active-directory/
The issue is that when I run that script from PowerShell, I get no output even after I use the parameters as shown in the example. Any help would be appreciated. Thank you.
Are you running the script or the function? That script just defines a function. If you want to use it try:
. pathtoscript
New-SWRandomPassword
Is there any way a batch script can know if it's called from PowerShell (without extra parameters feeded)?
Need something like..
if (%THIS_BATCH_CALLED_FROM_POWERSHELL%)
... warn the user or drop back to powershell to execute the proper instructions...
Question related with this question - virtualenv-in-powershell.
You could use a tool like "tlist.exe /t" or this one to display the PIDs of the current process and all parent processes. You could check each one of those PIDs to see if any correspond to PowerShell.exe.
You could add a default warning in the script and pass it a flag that tells it not to show the warning. When you call it from power shell pass it that flag.
In my Powershell environment (a PS 2.0 CTP), I seem to have an environment variable PSMODULEPATH which is not set by the normal command line environment, but still exists when Powershell has a child CMD.exe shell.
I think you might be able to "reliably enough" check for the existence of PSMODULEPATH in your batch script.