powershellメモ

# add $PATH temporary
```powershell
$Env:Path += ";C:¥hoge¥fuga¥"
```

# service
```powershell
Get-Service | Format-Table -AutoSize
```

# regexp
## match
```powershell:
$cont = (Get-Content [file])
if ($cont -match '^#endif') {echo "fuga"}
```
## replace
http://naichilab.blogspot.in/2015/02/powershell.html

```powershell:
$cont = (Get-Content [file])
$ptn = '^#endif.*'
$replace = ''
$conv = ($cont -replace $ptn, $replace)
```

# cat a.txt >> hoge.txt
`Get-Content a.txt | Add-Contents hoge.txt`

# Read JSON

```powershell:
Get-Content $SENSU_CLIENT_CONF -Encoding UTF8 -Raw | ConvertFrom-Json
```

Without `-Raw` CAN NOT parse json contains `NL new-line`. 

# Use Row-Socket(TCP)
Virus scan treat `nc.exe` as a backdoor.
Windows built in `telnet` not support non intaractive mode, powershell. 

## General modules
https://www.powershellgallery.com/packages/PowerShellCookbook/1.3.6

## Write myself
Send only ...

```powershell
$msg = '{"name": "hogefuga"}'

$socket = New-Object System.Net.Sockets.TcpClient("localhost", 3030)
$stream = $socket.GetStream()
$writer = New-Object System.IO.StreamWriter $stream
$writer.Write($msg)
$writer.Flush()

Start-Sleep -m 100

$writer.Close()
$stream.Close()
```

Read bellow and more study hard!

http://www.leeholmes.com/blog/2009/10/28/scripting-network-tcp-connections-in-powershell/

# Hot-to expand archive-files
It support not only `zip` but also `7z` and `tar.gz`...

1. Install Pscx
2. use `Expand-Archive` cmdlet

# Pscx
```powershell:
wget http://pscx.codeplex.com/downloads/get/923562 -Outfile Pscx-3.2.0.msi
Start-Process .\Pscx-3.2.0.msi /quiet -Wait
```

# Instll aws tools
```powershell:
wget http://sdk-for-net.amazonwebservices.com/latest/AWSToolsAndSDKForNet.msi -Outfile AWSToolsAndSDKForNet.msi
Start-Process .\AWSToolsAndSDKForNet.msi /quiet -Wait
```

# comparison ops
https://technet.microsoft.com/en-us/library/hh847759.aspx
`-eq` , `-ne` etc...

> By default, all comparison operators are case-insensitive. To make a 
  comparison operator case-sensitive, precede the operator name with a "c".

```powershell:
PS C:\Users\test> "hello" -eq "Hello"
True

PS C:\Users\test> "hello" -ceq "Hello"
False
```
# File IO
## Output UTF-8 without BOM
http://stackoverflow.com/questions/5596982/using-powershell-to-write-a-file-in-utf-8-without-the-bom

```powershell:
$MyFile = Get-Content $MyPath
$Utf8NoBomEncoding = New-Object System.Text.UTF8Encoding($False)
[System.IO.File]::WriteAllLines($MyPath, $MyFile, $Utf8NoBomEncoding)
```

# task scheduler
This is traditional task scheduler, not powershell taskjob.
## run powershell script
You should ___NOT___ set `hogehoge.ps1` at `Program/Script`. When you set Powershell script directory, it means `notepad.exe hogehoge.ps1`...

__Program/Script:__ C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
__Args:__ -file hogehoge.ps1

## runas ..
2 ways

1. Set `System` user.
2. Check `Run with higest privileges`


# 実践メモ
## limit event log capacity
https://technet.microsoft.com/en-us/library/hh849843(v=wps.630).aspx


#概念
コマンドの戻り = `stringという考えは誤り`
なんでもかんでも`オブジェクト`

##pwdを変数に取りたい
```ps1:
PS C:\Users\Administrator> Get-Location

Path                                                                                                                                
----                                                                                                                                
C:\Users\Administrator 
                                                                                                                                                                                                                          

PS C:\Users\Administrator> $cp = Get-Location

PS C:\Users\Administrator> $cp

Path                                                                                                                          
----                                                                                                                          
C:\Users\Administrator   

PS C:\Users\Administrator> $cp.GetType()

IsPublic IsSerial Name                                     BaseType                                                           
-------- -------- ----                                     --------                                                           
True     False    PathInfo                                 System.Object  

PS C:\Users\Administrator> Get-Location | Get-Member


   TypeName: System.Management.Automation.PathInfo

Name         MemberType Definition                                               
----         ---------- ----------                                               
Equals       Method     bool Equals(System.Object obj)                           
GetHashCode  Method     int GetHashCode()                                        
GetType      Method     type GetType()                                           
ToString     Method     string ToString()                                        
Drive        Property   System.Management.Automation.PSDriveInfo Drive {get;}    
Path         Property   string Path {get;}                                       
Provider     Property   System.Management.Automation.ProviderInfo Provider {get;}
ProviderPath Property   string ProviderPath {get;}  

PS C:\Users\Administrator> $cp.Path
C:\Users\Administrator

PS C:\Users\Administrator> (Get-Location).ToString()
C:\Users\Administrator

```

##覚えとくといい
Get-Member  まんま、メンバ一覧
<オブジェクト>.GetType() 型確認
<オブジェクト>.ToString() 言わずもがな
[String]<オブジェクト> キャスト(ちゃんと動くんかな？)