Why does my Powershell session sometimes refuse to run external commandline programs? - powershell

I'm having a weird problem where, when doing a lot of playing in the Powershell commandline, at some point it will stop executing any commandline application like cmd.exe or more.com, but will continue to execute Powershell functions and such. I can open up a new Powershell window
The situation
I'm using Powershell Community Extensions
I have a Powershell profile
I seem to encounter this when I'm trying to debug a script I'm writing (more on that later)
The problem by example
Normally I should be able to run ping and see normal output like this:
PS> ping google.com
Pinging google.com [74.125.227.78] with 32 bytes of data:
Reply from 74.125.227.78: bytes=32 time=30ms TTL=49
Reply from 74.125.227.78: bytes=32 time=38ms TTL=49
Reply from 74.125.227.78: bytes=32 time=31ms TTL=49
Reply from 74.125.227.78: bytes=32 time=32ms TTL=49
Ping statistics for 74.125.227.78:
Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 30ms, Maximum = 38ms, Average = 32ms
PS>
However, when I'm experiencing this problem, the PS prompt just returns immediately:
PS> ping google.com
PS>
I can verify in Task Manager that ping is not running during this time. Furthermore, for programs which should modify files on the filesystem (like takeown.exe or GnuWin32's wget.exe), the modifications do not occur. As far as I can tell, the command does not run at all.
This happens with every non-Powershell commandline app I can think of (more.com, GnuWin32's less.exe, ping.exe, cmd.exe, python.exe from the official Python distribution, StrawberryPerl's perl.exe, etc).
I can still run any Powershell function, cmdlet, or alias. Furthermore, I can run non-commandline apps like notepad.exe.
The (possible) culprit: my script
I have been trying to write a PS script over the last few days, when this problem started to crop up. It's too long to paste here (400 lines at the moment), but I've uploaded it to pastebin if you want to see it. (Currently not working very well.)
Right now I'm dealing with some permissions problems in the WinPE-AddExplorer function in that script, so a lot of the time I'm Ctrl-C'ing while that function is running... could something in there be causing the problem?

I had this exact problem. I'm guessing you're using Powershell V2. Please see this bug report (https://connect.microsoft.com/PowerShell/Feedback/Details/665809). Supposedly fixed in Powershell v3. (I have not seen this problem in v4 using the same script that does not work in v2)
The workaround is to put "[GC]::Collect()" in your loop that writes to the console.

Here are a few thoughts:
Try running PowerShell with -NoProfile. Import the PSCX module and run your script
Try running PowerShell without the PSCX module (adjust your profile script to NOT import, if necessary), then try running your script
Try not to use 3rd party modules in general
On another note, I had a look through your script, and nothing jumps out at me that would be permanently adjusting your PowerShell session. To debug this issue, try to think of any permanent, environmental changes that your profile (or your script) might be making. The developers of PSCX may or may not have code that makes permanent adjustments to your PowerShell environment also.
Use traditional troubleshooting techniques to help zero in on what area (your script, PSCX, or your profile) is causing you this problem.

Related

Trying to get a Powershell Script that will run in a 2nd window and monitor in real time other running scripts / report all Errors / ExitCodes

I am fairly new to writing code in Powershell. For my job I have to write multiple Powershell scripts to make changes in the Hardware and Software settings as well as the Registry and Group Policy Editor to get these applications to run. These applications are a little older. Upgrading these software applications or the hardware then run on is NOT an option. as an example, when Microsoft releases the new patches on like Patch Tuesday...when those patches are applied there is a high probability that something will be changed which is where I come in to write a script to fix the issue. I have multiple scripts that I run. When those scripts are ran they may end up terminating because of an Error Code or an Exit Code. A large part of the time I do not that the script has failed immediately.
I am trying to figure out a script that I can run in a 2nd PowerShell Console Window. I am thinking that the only purpose of this script is to just sit there on the screen and wait and monitor. Then when I execute a script or Application (the only file extensions that I am worried about are: EXE, BAT, CMD, PS1) if the script/application that I just ran ends with an exit code or an error code....then output that to the screen...in REAL TIME.
Below, I have a small piece of code that kind of works, but it is not what I am wanting.
I have researched online and read and read tons of stuff. But I just can't seem to find what I am looking for.
Could someone please help me with getting a script that will do what I am wanting.
Thank you for your help!!!!
$ExitErrorCode =
"C:\ThisFolder\ThatFolder\AnotherFolder\SomeApplication.EXE # (this
would
# either be an EXE or CMD or BAT or PS1)"
$proc = Start-Process $ExitErrorCode -PassThru
$handle = $proc.Handle # cache proc.Handle
$proc.WaitForExit();
if ($proc.ExitCode -ne 0) {
Write-Warning "$_ exited with status code $($proc.ExitCode)"
}
Possible duplicate of the approaches shown here:
Monitoring jobs in a PowerShell session from another PowerShell session
Monitoring jobs in a PowerShell session from another PowerShell session
PowerShell script to monitor a log and output progress to another
PowerShell script to monitor a log and output progress to another

Powershell hangs on Launch

When I run powershell ISE, I can execute commands/scripts without issue. When I launch either the 32 or 64 bit command line, the window takes about 30 seconds to load and then it is frozen. It will not accept inputs of any kind (keyboard or copy/paste). I have tried doing a system check, no errors. I even tried updating to powershell 4.0, the install was successful but the command line still locks on launch. Can anyone advise how to fix this? I am using windows server 2008 R2.
Update
It appears that the powershell is in fact accepting input, except at a glacial speed. I left the window open while I was writing this post initially, and then grabbed a coffee. Upon my return I found that what I have tried to copy/paste and type was now in the powershell command line. I have now attempted to execute $PSVersionTable.PSVersion, and going on 3 minutes now I still have no response. My guess is it will come back at some point but this is obviously not acceptable. Any ideas on how to debug/fix this?
Update2
As far as I can tell all the locations listed in $PROFILE | Select * don't exist. I also tried launching: powershell.exe -noprofile, but this did not help
After reading this post I decided to try that tool and see if I had a similar problem and discovered that there were literally hundreds of writes per second happening when powershell command line was running and they were all to the FusionLog. Disabling the Fusion logging fixed the issue completely (this was enabled a while ago to debug a different issue with an app and I must have forgotten to disable it). Everything else on the machine seemed to hum along just fine with FusionLogging in the background but powershell was horribly crippled. Hope this helps someone some day.

Benchmark A Page Via Windows Command Line and Powershell

I would like to benchmark the response time of a site as well as simulate a load via the windows command line (WIN+R)
Is this possible?
What is the windows equivalent of this linux command:
$ ab -n 1000 -c 5 http://yourpage.com
Extra Credit for Powershell 3 Equivalent
There is no equivalent built-in PowerShell cmdlet that does this. I see from ab's man page that your command sends 1000 requests, 5 at a time to the web server. You could roll your own solution in PowerShell using either the System.Net.WebClient or System.Net.HttpwebRequest classes, and implement concurrency with PowerShell jobs, parrallel for-loop workflows or asynchronous pipelines, however there are already solutions pre-built for this task like jmeter.

How to invoke an opened Exceed window to run a Perl script using a Schedule Task

TASK TO BE ACCOMPLISHED:
To schedule a perl script which is executed on a specific time / day in a week
THINGS I HAVE DONE:
In a schedule Tasks, I have created a new Task by which the Task will call a batch file with below contents
cd "DRIVE\FOLDER\Hummingbird\Connectivity\14.00\Exceed\"
ABCD.xs
cd mDrive/bin
perl baseline.pl -publish -location XXX -email
THINGS NOT WORKING FOR ME / CAUSING THE ISSUE:
Wen I run the scheduler, the prompt opens up the ABCD.xs exceed file window seperately file but the below commands are executed in the command pronpt itself
EXPECTED OUTPUT:
I want the commands
cd mDrive/bin
perl baseline.pl -publish -location XXX -email
to be executed in the exceed window
Any kind of solution wud be great
Thanks in advance.
Haresh
Sounds like you need to start getting into either SendKey stuff (Win32 packages) or else look into writing Exceed/Hummingbird scripts and just executing those.
Some other things to look into... does the remote server have a telnet or ssh server running? Or are there other methods of executing code on the remote server?
For example, my work's mainframe is accessed via a Hummingbird terminal emulator, but I can also telnet to the mainframe and execute commands as well as FTP batch job directly into the JES spool. So when I execute things on the mainframe by way of my PC (Perl scripts, etc.), I don't even fool with Hummingbird.
Good luck...

Calling a protected Windows 7 executable with Perl

I'm trying to write a perl script that determines which users are currently logged into Windows by using query.exe (c:\Windows\system32\query.exe). Perl is unable to access this file, unable to execute it, even unable to see that it exists, as I've found with the following code:
print `dir c:\\windows\\system32\\query*`;
This produces the following output:
07/13/2009 05:16 PM 1,363,456 Query.dll
1 File(s) 1,363,456 bytes
0 Dir(s) 183,987,658,752 bytes free
I've checked the user executing the script using perl's getlogin function, and it returns the name of a member of the local Administrators group (specifically, me). I've also tried adding read/execute permissions for "Everyone", but windows keeps giving me access denied errors when I try to modify this file's permissions. Finally, I've tried running perl.exe as an administrator but that doesn't fix the problem either.
Is this something I can solve by changing some settings in Windows? Do I need to add something to my perl script? Or is there just no way to grant perl access to some of these processes?
On my 64 bit machine I can find query.exe in Windows\System32 (the 64 bit system directory) but not in Windows\SysWOW64 (the 32 bit system directory).
I suspect that you are running 64 bit Windows and 32 bit Perl and so, under WOW64 emulation, the 32 bit Perl process redirects system32 to SysWOW64.
Since system32 should be in your path you ought to be able to execute it by executing query.exe without any path. If you absolutely have to refer to the 64 bit system folder then you can do so from a 32 bit process with C:\Windows\sysnative.
If my suspicions are correct I recommend you do some reading up on WOW64 because it can be pretty confusing until you get on top of it. On the other hand, if you already know all about it I apologise for being patronising.