- Prison Code Breaker Diary -

=> aka: Nhật Kí Code Tù

Categories

Here's the trick, using the -o, and L option we can search for what we need to print out

ps -opid -A

The End!

Oh yes, it took me time to search for this.
Here's the trick:


ps -o"%C" -p #PID

or simplified version

ps -opcpu -p #PID

The End :D

Commonly, it's very hard for beginners to learn a new thing, especially this case, it's PowerShell. However, if you can see through its core, know the basics, and know how to search for helps or reference, it will be getting easier.
It took me about a week to learn the basis and understand the PowerShell concepts.
1. Using MSDN or MS Technet for PowerShell references.
2. Make use of some functions that provide helps.

There are two commands I use: Get-Command and Get-Help.

1. Get-Command: this function shows a list of commands being available at current session.
You can try by just typing: Get-Command, the list will show itself like this:

CommandType     Name                                 Definition                          
-----------     ----                                 ----------                          
Alias           %                                    ForEach-Object                      
Alias           ?                                    Where-Object                        
Function        A:                                   Set-Location A:                     
Alias           ac                                   Add-Content                         
Cmdlet          Add-Computer                         Add-Computer [-DomainName] 
Cmdlet          Add-Content                          Add-Content [-Path]  [-...
Cmdlet          Add-History                          Add-History [[-InputObject] 
Cmdlet          Add-Member                           Add-Member [-MemberType] 
Cmdlet          Add-PSSnapin                         Add-PSSnapin [-Name]  [...
Cmdlet          Add-Type                             Add-Type [-TypeDefinition] 

As you can see, there are 3 properties shown up: CommandType, Name, and Definition.
There are 3 types of commands: Alias, Cmdlet and Function.
+ Cmdlet: these are the core of PowerShell, it is the real command created to use.
+ Alias: these are just the other names of Cmdlets for instant call.
+ Function: these are the already written Cmdlets for instant access when you don't want to re-type the Cmdlet that are long.
Example, list all of commands that are Cmdlets:
PS> Get-Command -CommandType Cmdlet

2. Get-Help: this command provides help about PowerShell Cmdlets and concepts.
Example, if I want to get information about Get-Command, I  type
PS> Get-Help Get-Command

To get examples about the command,

PS> Get-Help Get-Command -examples

To get details about the coomand,

PS> Get-Help Get-Command -detailed

To get full info about the command,

PS> Get-Help Get-Command -full

Hope this post may help someone finding PowerShell a bit easier to learn!

All information about the environment variable is stored in '$env'.
When you want to retrieve info about any variable, you can follow the syntax

$env:<variable_name>

Here's an example how to use it:


PS C:\> $env:windir
C:\Windows

PS C:\> $env:path
C:\Perl\site\bin;C:\Perl\bin;C:\Program Files\ActiveState Perl Dev Kit 8.0\bin;
C:\Program Files\Borland\Delphi7\Bin;C:\Program Files\Borland\Delphi7\Projects\
Bpl\;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;c:\Program Files\M
icrosoft SQL Server\90\Tools\binn\;C:\Web\PHP;C:\Web\PHP\ext;C:\Web\MySQL\bin;C
:\Program Files\QuickTime\QTSystem\;C:\GTK\bin;C:\Program Files\Microsoft Visua
l Studio 8\VC\bin;C:\Program Files\Java\jdk1.6.0_18\bin;C:\GTK2-16\bin;C:\Windo
ws\System32\WindowsPowerShell\v1.0\;C:\Program Files\GTK2-Runtime\bin;C:\BC5\BI
N;C:\Program Files\Nmap;C:\Program Files\Common Files\Nero\Lib\

It's a damn cool thing when you want to save something into HTML format.
Windows PowerShell provides this function through [ConvertTo-HTML] Cmdlets.

Example, if I want to save my list of current processes into HTML format

PS > Get-Process | ConvertTo-HTML | Out-File ProcessHTML.html

Another great thing is that it provides the way how you can format the output the HTML file through these 3 arguments: -head, -body, and -title.
Here, another example of formatting the output

$format_head = "<title> PROCESS LIST </title>"
$format_head += "<style>
"
$format_head += "TABLE { border-width:2px;border-style:solid}"
$format_head += "TH {border-width:1px;border-style:solid}"
$format_head += "TD {border-width:2px;border-style:solid}"
$format_head += "</style>"

$format_body = "<center> PROCESS LIST </center>"

Get-Process | ConvertTo-HTML -head $format_head -body $format_body | Out-File processHTML.html

It's been a while since MSDN get a new face. However, I prefer the old one because it's easier to navigate to what I'm looking for.

For PowerShell reference, it's right here: http://technet.microsoft.com/en-us/library/bb978526.aspx

There're 2 ways to run your VBScript or JScript under Windows, either through CScript.exe or WScript.exe
What's the difference about those two?
- They are all almost the same, just different in the way displaying the output. While CScript.exe displays output in a Console, WScript.exe display the output in a Dialog.

Well, when you run the script, the default host will be chosen certainly. But you can switch between them.

If you want CScript.exe to be the default host

cmd> CScript //H:CScript

or WScript to be the default one

cmd> WScript //H:WScript

By default, you cannot execute any remote PowerShell script (extension: PS1).
However, you can set execution right to run any script on your system by calling the Set-ExecutionPolicy command

PS C:\Data> Set-ExecutionPolicy RemoteSigned

Execution Policy Change
The execution policy helps protect you from scripts that you do not
trust. Changing the execution policy might expose you to the security
risks described in the about_Execution_Policies help topic. Do you want
 to change the execution policy?
[Y] Yes  [N] No  [S] Suspend  [?] Help (default is "Y"): Y
PS C:\Data>


After that, you can run any .ps1 script as you like.

It's included inside Windows Management Framework RC (released on 8/13/2009).

You can download it from here:
https://connect.microsoft.com/windowsmanagement/Downloads/DownloadDetails.aspx?DownloadID=21267

But I'm not sure this is the latest.

On exponentiation, don't take the number too big like: 10 ** 99999 ... otherwise, u will get unexpected result or 1.#INF

The remainder operator doesn't apply for floating-point number, but if you do then they all will be round-off into the closest integers, then apply the operator.

In Perl, the floating-point number can be written in the form like this: 1E+01, 23.45e-02, 8.567E+32 ...
The letter 'E' or 'e' denotes the exponent.

Here the case:

my $var_1 = 11E+30;
my $var_2 = 11e30;
my $small_number = 0.00001; # or 1e-05

print "First case: $var_1 - $var_2 + $small_number = ".($var_1 - $var_2 + $small_number)."\n";
print "Second case: $var_1 + $small_number - $var_2 = ".($var_1 + $small_number - $var_2)."\n";
Here the result:
First case: 1.1e+031 - 1.1e+031 + 1e-005 = 1e-005
Second case: 1.1e+031 + 1e-005 - 1.1e+031 = 0
So how does it actually work?
I can explain it like this.
At line 5, $var_1 and $var_2 is eliminated down to 0 since they're equal in value, thus, adding a small value will be kept.
At line 6, $var_1 will be adding the $small_number, but because the value is too small compared to the other side, then it is considered as 0; so, the next substract to $var_2 will be 0 in result.

Check another example to see if the value is too small
my $too_small = 1e-999;
print "value is $too_small\n";
The result is 0 certainly, as I explained above.

I'm talking about the conditional statement IF.
Here is the definition:

IF (exp) {
   (statement)
}
Try this example:
$age = 23; 
if ( $age == 23 ) {
   print "I am 23 years-old", '\n'
}
Certainly perl evaluates the expression is TRUE since $age is assigned to the value '23'.
How about this case:
if ( $age = 23 ) {
   print "I am 23 years-old", '\n'
}
Still you've got the result printed. But how do you explain what perl does to this evaluation?

1. First, $age is assigned to value '23'.
2. The expression is evaluted whether it is TRUE or FALSE, which means, $age is ZERO or non-ZERO. So, it's TRUE right here because '23' is definitely not '0'.
3. Statement 'print' is called.

Rewrite the explanation into the code itself:
$age = 23;
if ($age) {
   print "I am 23 years-old", '\n'
} 

If u try with constants assignment, u'll get error
if ( 20 = 55 ) {    
    print "is it ok?", '\n'
}

Found = in conditional, should be ==
(W syntax) You said

if ($foo = 123)

when you meant

if ($foo == 123)

(or something like that).

Can't modify constant item in scalar assignment
A fatal error (trappable)
You aren't allowed to assign to the item indicated, or otherwise try
to change it, such as with an auto-increment.

Same results but different operators mean differences.

I'm mentioning about the '=' and the '==' operators with different means, applied to all logical cases.

I've got no more busy right now.
Happily I can go back to my normal life and continue to update my blog with new entries, new challenges, new tricks, new tips, new tutorials ...

Hahah..so enjoy my life !