-
-
Save aachyee/ecea9a0a544410d2e352c6b9542f93d5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Template | |
| Set-StrictMode -Version Latest | |
| $ErrorActionPreference = "Stop" | |
| $here = Split-Path -Path $MyInvocation.MyCommand.Path -Parent | |
| # ArrayList | |
| $al = New-Object System.Collections.ArrayList | |
| # OrderedDictionary | |
| $od = [ordered]{ | |
| a = 1 | |
| b = 2 | |
| c = 3} | |
| # read xml | |
| $xmlDoc = [xml](Get-Content "foo.xml" -Encoding utf8) | |
| # SMB接続 | |
| $securePassword = ConvertTo-SecureString "password" -AsPlainText -Force | |
| $cred = New-Object System.Management.Automation.PSCredential "$computerName\$userName", $securePassword | |
| New-PSDrive -Name foo -PSProvider FileSystem -Root "\\$computerName" -Credential $cred | |
| # ダブルクォーテーションなし、列名なしのcsvに変換 | |
| $object | ConvertTo-Csv -NoTypeInformation -Delimiter ',' | Select-Object -Skip 1 | ForEach-Object {$_ -replace '"',''} |Out-File foo.csv -Encoding utf8 | |
| # COM経由でExcel操作 | |
| $excel = New-Object -ComObject Excel.Application | |
| $excel.Visible = $false | |
| $excel.DisplayAlerts = $false | |
| $book = $excel.Workbooks.Open($filePath) | |
| $sheet = $excel.Worksheets.Item(1) | |
| $text = $sheet.Cells.Item(1,1).Text | |
| # 10進表記のIPアドレスを16進表記に変換する | |
| # 戻り値 : 16新表記ドット区切りなしのIPアドレス文字列 | |
| function Get-IPAddressHex { | |
| [CmdletBinding()] | |
| param ( | |
| [parameter(Mandatory=$true, ValueFromPipeline=$true)] | |
| [int]$IP1decimal, # 10進表記IPアドレス第1オクテット | |
| [parameter(Mandatory=$true, ValueFromPipeline=$true)] | |
| [int]$IP2decimal, # 10進表記IPアドレス第2オクテット | |
| [parameter(Mandatory=$true, ValueFromPipeline=$true)] | |
| [int]$IP3decimal, # 10進表記IPアドレス第3オクテット | |
| [parameter(Mandatory=$true, ValueFromPipeline=$true)] | |
| [int]$IP4decimal # 10進表記IPアドレス第4オクテット | |
| ) | |
| PROCESS { | |
| $IP1hex = $IP1decimal.ToString("X").PadLeft(2, "0") | |
| $IP2hex = $IP2decimal.ToString("X").PadLeft(2, "0") | |
| $IP3hex = $IP3decimal.ToString("X").PadLeft(2, "0") | |
| $IP4hex = $IP4decimal.ToString("X").PadLeft(2, "0") | |
| return $IP1hex + $IP2hex + $IP3hex + $IP4hex | |
| } | |
| } | |
| # cp932文字列から指定バイト分切り出す | |
| # 戻り値 : 指定バイト分切り出したcp932文字列 | |
| function Get-SubStringBytes { | |
| [CmdletBinding()] | |
| param ( | |
| [parameter(Mandatory=$true, ValueFromPipeline=$true)] | |
| [string]$text, # 文字列 | |
| [parameter(ValueFromPipeline=$true)] | |
| [int]$startIndex = 0, # 開始位置 | |
| [parameter(ValueFromPipeline=$true)] | |
| [int]$length = 0 # 切り出すbyte数 | |
| ) | |
| PROCESS{ | |
| $encoding = [System.Text.Encoding]::GetEncoding("Shift_JIS"); | |
| $encoder = $encoding.GetEncoder(); | |
| [byte[]]$buff = New-Object byte[] $length | |
| [int]$charsUsed = 0 | |
| [int]$bytesUsed = 0 | |
| [bool]$completed = $FALSE | |
| $target = $text.ToCharArray(); | |
| $encoder.Convert($target, 0, $target.Length, $buff, 0, $buff.Length, $TRUE, [ref]$charsUsed, [ref]$bytesUsed, [ref]$completed); | |
| return $encoding.GetString($buff, $startIndex, $length); | |
| } | |
| } | |
| # サービスの名前、状態、スタートアップの種類、ログオンアカウント | |
| Get-CimInstance -Class Win32_Service -Property name,state,startmode,startname -Filter 'name="serviceName"' | |
| # 共有アクセス許可テスト | |
| It "fooの共有アクセス許可 Administratorにフルコントロールが許可されている" { | |
| $share = Get-CimInstance win32_LogicalShareSecuritySetting -Filter "name='foo'" | |
| $acls = $share.GetSecurityDescriptor().Descriptor.DACL | |
| $permission = "" | |
| foreach($acl in $acls) { | |
| if($acl.Trustee.Name -eq "Administrator") { | |
| switch($acl.AccessMask) { | |
| 2032127 {$permission = "FullControl"} | |
| 1245631 {$permission = "Change"} | |
| 1179817 {$permission = "Read"} | |
| } | |
| } | |
| } | |
| $permission | Should -Be "FullControl" | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment