Powershell hangs on Launch - powershell

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.

Related

Interactively call SCP from Powershell

I've been banging my head against a powershell shaped wall all day. For work reasons, I want to be able to upload a file from a windows machine to a remote linux box. There's some work to do before so I foolishly believed I could just drop in a call to SCP in my script. But it doesn't like it one bit:
Putting
scp api.zip ${user}#${currEnv}:/${uploadLocation}
where user is the remote login name, currEnv is the ip address and uploadLocation is /tmp (at least while I'm debugging)
in the code just causes the execution to hang completely - in fact the same thing happens if I run that command from the PS command line, even if I manually replace all the variables.
I tried replacing it with pscp which works better in that it actually connects, but then throws up the usual warning about this being the first time I've connected and should type 'y' to proceed (again, even from the command line iteself) - but that won't let me type anything in. I've also even tried using wsl scp ... to run it (don't tell the IT guy), but that also seems to hang - it shows me the server banner, but never gets to the bit where it should be asking for my remote password.
Am I missing something?

Powershell to change password not working

I have a long Powershell script that "builds" a PC for me, by adding everything I need to a Windows 7 (32 bit) PC. I have had this script for several years and it has worked fine. One of the first things I do is change the password. This line of code suddenly doesn't work in the script -
([adsi]"WinNT://ccm2756/Administrator").SetPassword("NewPassword")
I get "the following exception has occurred while retrieving member SetPassword. Network path not found."
Has worked just fine all this time. And on the same PCs. Haven't changed the script, haven't changed the PCs (I've burned the images back to saved based operating systems). Suddenly, I am getting this error? And yet, if I open a command line and type "Powershell" to get to a PS Command line and run that exact same command, it goes through without any errors.
What am I missing here?

Disconnecting OpenFiles

I've been researching and learning about windows batch files, PowerShell and cmd these past few days.
We're having issues with Open Files, so we manually go to the server and do it with a press of a button. But since there might be a possible way to automate it and do the script every 5 minutes.
Someone helped me already telling me that I should make a script of
openfiles /disconnect /a* /op "E:\SERVERNAME\"
& so I did and put it on the Windows Task Scheduler Action Tab - Start a Program and put the file path of the batch file that I made.
But It seems that it's not working and we're still having the same issue.
I hope I made it clear.

Powershell Script doesn't work when starting it by double-clicking

I got some strange behaviour when executing a powershell script.
When I run my script using the ISE it works just fine.
When I open Powershell.exe and run my script it works just fine.
When I open cmd, and start my script using powershell.exe -noexit
./myscript.ps1, myscript works just fine.
When I double-click myscript however, powershell opens for some milliseconds, I see that it shows some error (red font) and the powershell window closes. I'm unable to track down the error causing this problem since the powershell windows closes to fast.
I even tried one single big try-catch block around my hole script, catching any [Exception] and writing it down to a log file. However: the log file is not generated (catch is not called).
How can I track that issue? What could possibly be causing the trouble?
Please note that my execution-policy is set to unrestricted.
Before trying the suggestion invoke this to see your current settings (if you want restore them later):
cmd /c FType Microsoft.PowerShellScript.1
Then invoke this (note that you will change how your scripts are invoked "from explorer" by this):
cmd /c #"
FType Microsoft.PowerShellScript.1=$PSHOME\powershell.exe -NoExit . "'%1'" %*
"#
Then double-click the script, it should not exit, -NoExit does the trick. See your error messages and solve the problems.
But now all your scripts invoked "from explorer" keep their console opened. You may then
remove -NoExit from the above command and run it again or restore your
original settings.
Some details and one good way to invoke scripts in PS v2 is here.
Unfortunately it is broken in PS v3 - submitted issue.
by default, for security reason when you double clic on a .ps1 file the action is : Edit file, not Run file .
to execute your script : right-click on it and choose run with powershell
I also wasn’t able to run a script by double clicking it although running it manually worked without a problem. I have found out that the problem was in the path. When I ran a script from a path that contained spaces, such as:
C:\Users\john doe\Documents\Sample.ps1
The scipt failed to run. Moving the script to:
C:\Scripts\Sample.ps1
Which has no spaces, solved the problem.
This is most likely an issue with your local Execution Policy.
By default, Powershell is configured to NOT run scripts that are unsigned (even local ones). If you've not signed your scripts, then changing your default double-click 'action' in Windows will have no effect - Powershell will open, read the execution policy, check the script's signature, and finding none, will abort with an error.
In Powershell:
Help about_execution_policies
gives you all the gory details, as well as ways to allow unsigned scripts to run (within reason - you'd probably not want to run remote ones, only ones you've saved onto the system).
EDIT: I see at the tail end of your question that you've set Execution Policy to 'unrestricted' which SHOULD allow the script to run. However, this might be useful info for others running into execution policy issues.
If you would catch the error you will most likely see this
The file cannot be loaded. The file is not
digitally signed. The script will not execute on the system. Please
see "Get-Help about_signing" for more details.
Because you are able to run it from the shell you started yourself, and not with the right mouse button click "Run With PowerShell", I bet you have x64 system. Manually you are starting the one version of PowerShell where execution policy is configured, while with the right click the other version of the PowerShell is started.
Try to start both version x64 and x86 version and check for security policies in each
Get-ExecutionPolicy
I was in exactly the same situation as described in the question : my script worked everywhere except when double-clicking.* When I double-clicked a powershell windows would open but then it will close after a second or so. My execution-policy is also set to unrestricted.
I tried the selected answer concerning FType Microsoft.PowerShellScript.1 but it didn't change anything.
The only solution I found was a work around: create a bat file which start the powershell.
Create a file, copy this and modify the path : powershell.exe -File "C:\Users\user\script\myscript.ps1"
Save it as a .bat
Double-click the bat
I also used .ahk to start my powershell with a shorcut and it didn't work when pointing directly to the powershell. I had to point to the .bat

Why I get other result for the `get-Help about_*` expression?

PowerShell 4.0
I read the Getting Started with PowerShell book. I try author's code but I get the other result... The screen of the book page (with my comment):
Read my comment on the screen, please. I get such results:
Why I get other results?
I have seen this same issue on several machines. Update-Help appears to execute successfully when run from a normal PowerShell window, but does not actually update many of the help files.
The solution is to run Update-Help from an elevated PowerShell window (Run as Administrator). Once this completes you should have all of the expected help files.
You may also have to specify the -UICulture parameter since Update-Help may not be utilizing your localization settings as seen in this post.
You can use this command to update help for your language:
Update-Help -UICulture (Get-Culture).Name
I found the reason of my problem...
I had launched Updated-Help with admin rights, of course, but I didn't restart the PowerShell session and got that unexpected result. Later I restarted PowerShell and saw that all works fine after restarting.
I didn't think that application restarting can be necessary at this case.