- Prison Code Breaker Diary -

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

Categories

Showing posts with label PowerShell. Show all posts

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

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.