Why executing the powershell script first time in a session takes much time? - powershell

I am loading Powercli snapin in my script. If i execute a script first time in a session it takes more time. When i am executing another script which uses the same snapin ,it takes less time.[I measured the performance using Measure-Command cmdlet]
So i think whenever new session is created , it takes more time to execute the script.
In our case , as users will execute powershell by right clicking the file and run with powershell it will always run in new session. So every time they are facing slowness.
Why executing the powershell script first time in a session takes much time?
Is there any way to avoid this slowness? [I am using Powershell 2.0]

You're loading the snap-in. When you run your script the second time in the same session it is already loaded so Add-PSSnapIn returns quickly as it notices there is nothing to do.
If you want to always load the snap-in, yu can do so in your profile. Simply do
notepad $profile
in PowerShell and append the Add-PSSnapIn call.

Related

Configure Windows logon script with PowerShell

I want to make a powershell script that creates another powershell script that is always executed at userlogin from windows.
The purpose is that I would like to change the number of files taken at each user login.
My question how can I put a script via powershell and tell windows to run this script every time a user logs in.
I would appreciate an answer because I am really desperate.

PowerShell WMI query failure over time

I am performing PowerShell (4.0) WMI query as below to obtain a specific process ID for java processes, the process variable is effectively 'java' and command is part of the path to a know part of the command line value (java command line options).
(Get-WmiObject win32_process -Filter "name like '%$process%' and commandLine like '%$command%' and not commandline like '%shutdown%'")
This, given a unique command line should always return a single process/process id value.
The PowerShell is called originally from a Scheduled Task that then enters into an infinite loop calling the WMI query every X minutes, this works for ~1 hour after I log off from the server and then simply stops returning the process. Logging back onto the server I can then open a new PS shell and run the command manually - it works, the task running in task manager continues to show no values.
If I am logged onto the server with a PowerShell ISE window open and configure an infinite loop to do the same while remaining logged in this does not suffer the same situation and the loop seems to continue without failure - but the key difference here is I can not log off from the server as I have an interactive shell open - I think from testing the scheduled task also does not fail while the interactive session is active.
The scheduled task is called as a local admin account running with highest privs with the password stored.
Any ideas why the WMI query stops after a period of time and how this may be corrected as I can not rely on keeping my RDP session active?
Thanks in advance.
I have worked around this with a simple 'if the WMI query fails' check for (Get-WmiObject -Query "select * from Win32_process").count if this is 0 then it restarted the entire process (as this should never be 0)

Script not running first time in console, does afterwards

Hoping you can help as I have run out of ideas and at this point I think it is because I don't understand PowerShell properly.
I read this series; https://foxdeploy.com/2016/05/17/part-v-powershell-guis-responsive-apps-with-progress-bars/ and tried to do the same thing, then I discovered that I could not call my script by opening a normal PowerShell console (Not ISE) and doing .\script.ps1
I also noticed that I could not do the same with the example script located here;
https://github.com/1RedOne/BlogPosts/blob/master/GUI%20Part%20V/PowerShell_GUI_Template.ps1
It does run the SECOND time however and every time after that? For example if you do
.\script.ps1 (doesn't work)
.\script.ps1 (works)
.\script.ps1 (works)
etc...
But if you do
powershell .\script.ps1 (doesn't work)
powershell .\script.ps1 (doesn't work)
However if you close ISE, open it again and run script it works first time?
I don't really know what is causing this.
Does anyone have any ideas?
If you would like to see this just copy from github link as that one has same problem as mine.
The issue is due to line 39-41 in the script on GitHub:
$syncHash.Window=[Windows.Markup.XamlReader]::Load( $reader )
[void][System.Reflection.Assembly]::LoadWithPartialName('presentationframework')
As you can see, presentationframework.dll is not being loaded into the appdomain until after XamlReader.Load() is being called.
The entire rest of the script is dependant on the output from this call, so the entire thing fails.
Second time around, the presentationframework assembly has already been loaded, so the call succeeds and the Window is created correctly.

Powershell config to force a batch file to run within the powershell window?

I've got a powershell script that eventually passes a stack of arguments into a batch file via invoke-expression command.
However, on one server, when the powershell scripts executes that batch file, that batch file opens in a new window, but on the other server, the batch file executes within the powershell window.
What that means, is that I've got a sleep interval that is starting once the batch file begins executing in the new window, and thus screwing up my timings, unlike the other server, where the sleep interval doesn't begin until after the batch file has finished executing.
So my question is... does anybody know why the behaviours are different between the two servers, and how to get the batch file to execute in the powershell window? I'm thinking it's a configuration thing, but can't actually find anything that tells me how to make it do what I want it to do.....
Thanks!
--edit--
I'm currently just piping the line straight through like this:
E:\Software\ibm\WebSphere\AppServer\bin\wsadmin -lang jython -username $($username) -password $($password) -f "F:\Custom\dumpAllThreads.py" $($servers)
Previously, it was
$invokeString = 'E:\Software\ibm\WebSphere\AppServer\bin\wsadmin -lang jython -username $($username) -password $($password) -f "F:\Custom\dumpAllThreads.py" $($servers)'
$output = invoke-expression $invokeString
Both had the same behaviour.
So my question is... does anybody know why the behaviours are different between the two servers
Most often I've seen this sort of thing related to how a scripts is called. If the same user is logged on multiple times on the same server (i.e., console and RDP) then the window might appear in a different session. Similarly, if the script runs as a scheduled task and the user that runs the task isn't the user logged on, the window will never be visible. If the same user is logged on, it might be visible.
how to get the batch file to execute in the powershell window?
You could try Start-Process with -NoNewWindow, as #Paul mentions.
However....
What that means, is that I've got a sleep interval that is starting once the batch file begins executing in the new window, and thus screwing up my timings, unlike the other server, where the sleep interval doesn't begin until after the batch file has finished executing.
It sounds like your actual problem is that your code has a race condition. You should fix the actual problem. Use Start-Process with the -Wait parameter, or use the jobs system in PowerShell.

powershell seemingly forgets ntfssecurity module after a script finishes

the module is at http://gallery.technet.microsoft.com/scriptcenter/1abd77a5-9c0b-4a2b-acef-90dbb2b84e85
when i run a script which uses the get-ace cmdlet in a Windows Powershell enviroment and finishes, exit it using ctrl-c or using exit command in the script, i have to restart my Powershell environment to use my script again. Otherwise i end up getting errors that it doesn't know what get-ace is. yet during the whole time the script runs for the first time, it can run for days and finish fine.
any ideas on what the issue is?