Showing posts with label PowerShell. Show all posts
Showing posts with label PowerShell. Show all posts

Saturday, August 13, 2016

You Should Be Using PowerShell, Here are the Bare Minimums

I see most people avoiding PowerShell. I say if you just learn a few essential usage, it is a much easier environment to use, especially switching between MacOS and Windows.

Launching

Go to the start menu and type in powershell. If you frequent your Windows environment, I would "pin" this to Start or the Taskbar, and also start avoiding Cmd.

Some Basic Coolness

If you are going back and forth between IOS and Windows, it would be a bit more comfortable to use the PowerShell as it comes "out of the box" without the need for installing the "bash" shell and many of the Unix like commands are supported as "alias".

  • Like on the Unix shell you no longer have to type in /Users/myname/<wheatever> instead you'd be like in the familiar Unix environment like..
  • .
    • cd ~  works auend gret ysou tyoo /ura/<ccount>
    • cd ~/Downloads works!
    • cd~/Desktop works
    • ls works (this is an alias so cannot go too fancy like ls -lR)
    • From there if you do "open ." then you can open the Explorer at that directory.
  • You can now Ctrl-C and Ctrl-V if you just enable them. Right click the title bar of the PowerShell window for the Properties. Then open the Options menu and look for "Enable Ctrl key shortcuts." This was added in Windows 8

Some Peculiar Stuff

  • If you want to see environment variables, use "printenv"
  • One issue with environment variable. For example, let's say you want to use $JAVA_HOME as in MacOS, then you need to do $env:JAVA_HOME   I don't really like this, but so there

The Basic Idea

Like most anything, understanding the architecture and the basic philosophy of the design is the key to understanding whatever you are running into.

In terms of PowerShell, they tried to standardize basically on the REST type API Call style. So this is why they have commands like Get-This or Set-That.  So if you master those, then you can chain them together like you do with Unix pipes with a much more consistent "schema" they set up. 

The "help" is quite extensive and comprehensive as a result, so if you type in just about any phrase you think about, as "help something", it is a good way to move forward with more stuff.





Friday, January 17, 2014

Windows PowerShell Script to Restart Memory Leaking Services

Background:

You have a service or two that has a modest memory leak and you would want to restart right when it has consumed a certain amount of memory.

This approach has an advantage over scheduled restarts because it will restart the service right when it is needed, or otherwise it leaves "it" alone.

This requires the use of Microsoft PowerShell which is available for XP (as download) and built-in on Windows Vista/7 and Server 2003 or later.

First time you try to run it, it is very likely that it won't run the script locally because out of the box, the script file is disabled. You will need to manually type in after manually opening PowerShell

Set-ExecutionPolicy RemoteSigned

From that point on it should permit running.

#
# This script detects the memory usage threshold and then restarts a service named in this script. You can
# run this script periodically in Scheduled Task to restart memory leaking service.
#
# Remember to do 
# Set-ExecutionPolicy RemoteSigned 
# so that this script can run
# To run this script from a BAT, 
# powershell -File RestartService.ps1
#
# First comment out the following two lines so that you can get the basic idea of the content of this stuff by
# looking at the proc.txt output file in your editor to hunt for the process name etc.
# Then it's easier to figure out the filtering for the name and the threshold.
#
#$Processes = Get-WmiObject -ComputerName "localhost" -Class Win32_PerfFormattedData_PerfProc_Process
#echo $Processes > C:\temp\proc.txt
#
# First do the Task Manager then find the EXE that is hosting the process or its subprocess then take the .exe out of that name
# If the task manager is showing *32, do not include that either.
#
$ServiceExe="MyServer"
#
# This is the name of the service to restart, this is the 'short' service name you can find in the Service's property
#
$Service="MyServerService"
#
# This is the working set threshold. For example, 3,500,000,000 would be 3.5 GB
#
#$Threshold = 3500000000
$Threshold = 500000000
#
# Real Meat of this program
#
$Process = Get-WmiObject -ComputerName "localhost" -Class Win32_PerfFormattedData_PerfProc_Process -Filter "Name='$ServiceExe'"
$wsm = $Process.WorkingSet /1024/1024/1024;
$thgb = $Threshold/1024/1024/1024
#
# Format the numbers so they are easier to read in GB units and round up to a
# decimal places.
#
$Fwsm = $("{0:0.000}" -f $wsm);
$Fthgb =$("{0:0.000}" -f $thg
b










);
e











cho $("Worksing Set is "+ $Fwsm + " gb and threshold is " + $Fthgb  + " gb");
if($Process.workingset -gt $Threshold)
{
    Restart-Service $Service; 
    echo "Restarted
"







;
}
e








lse
{
    $delta = $("{0:0.000}" -f ($thgb - $wsm));
    echo $("Still " + $delta + " gb left. Did not restart"
);

}
sl

eep 10