8
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

powershellメモ

Last updated at Posted at 2015-08-11

add $PATH temporary

$Env:Path += ";C:¥hoge¥fuga¥"

service

Get-Service | Format-Table -AutoSize

regexp

match

$cont = (Get-Content [file])
if ($cont -match '^#endif') {echo "fuga"}

replace

$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

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

Write myself

Send only ...

$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!

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

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

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".

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

PS C:\Users\test> "hello" -ceq "Hello"
False

File IO

Output UTF-8 without BOM

$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

概念

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

pwdを変数に取りたい

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]<オブジェクト> キャスト(ちゃんと動くんかな?)

8
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?