Lazy Diary @ Hatena Blog

PowerShell / Java / miscellaneous things about software development, Tips & Gochas. CC BY-SA 4.0/Apache License 2.0

Run a command with specific user priviledge in Windows (like sudo in Unix)

Problem:

You can use runas command to run a command with specific user privilege. (Note: You have to type password into command prompt):

C:\>runas /user:domain\username cmd
Enter the password for domain\username:
Attempting to start cmd as user "domain\username" ...

But, you will get error with a command with arguments:

C:\>runas /user:domain\username net start WebClient

RUNAS USAGE:

RUNAS [ [/noprofile | /profile] [/env] [/savecred | /netonly] ]
        /user:<UserName> program

You can also use Start-Process -Verb runas with the same aim without typing password (UAC dialog will pop up for privileges):

PS > Start-Process -Verb runas cmd

But you will get an error when you want pass arguments to the command:

PS > Start-Process -Verb runas net start WebClient

Start-Process : A positional parameter cannot be found that accepts argument 'WebClient'.
At line:1 char:1
+ Start-Process <<<<  -Verb runas net start WebClient
    + CategoryInfo          : InvalidArgument: (:) [Start-Process]、ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.StartProcessCommand

Or you will get another error when you quote the command and arguments:

PS > Start-Process -Verb runas "net start WebClient"
Start-Process : This command cannot be executed due to the error: The system ca
nnot find the file specified.
At line:1 char:14
+ Start-Process <<<<  -Verb runas "net start WebClient"
    + CategoryInfo          : InvalidOperation: (:) [Start-Process]、InvalidOperationException
    + FullyQualifiedErrorId : InvalidOperationException,Microsoft.PowerShell.Commands.StartProcessCommand

Solution:

You should quote the command and arguments in runas command:

C:\>runas /user:domain\username "net start WebClient"
Enter the password for domain\username:
Attempting to start net start WebClient as user "domain\username" ...

You should use -ArgumentList option to pass argument in Start-Process cmdlet:

Start-Process -Verb runas -ArgumentList "stop WebClient" net

ISO/IEC 12207 (JIS X 0160:2012) ソフトウェア構成管理プロセスのうちバージョン管理システム、GitHub/GitLab/etc、git-flow/GitHub Flowでカバーできる内容

(In English: What can you do for ISO/IEC 12207 (JIS X 0160:2012) Software Configuration Management Process with VCSs, GitHub/GitLab/etc, and git-flow/GitHub Flow)

ISO/IEC 12207 (JIS X 0160:2012)では、ソフトウェア開発におけるソフトウェア構成管理プロセスが規定されている。その中の作業には、バージョン管理システムを使えばカバーできるもの、GitHub/GitLab/etcを使っていればカバーできるもの、git-flow/GitHub Flowでカバーできるものがある。 ISO/IEC 12207 (JIS X 0160:2012)のソフトウェア構成管理の各項目に対する、各技術要素のカバー範囲を表にまとめてみた。

見出し バージョン管理システムでできること GitHab/GitLab/etcでできること git-flow/GitHub Flowでできること git-flowではできないこと 備考
7.2.2 - - - - -
7.2.2.1 ソフトウェア品目の利用
ソフトウェア品目の完全性の維持
ソフトウェア品目の完全性の確立
7.2.2.2 - - - - -
7.2.2.2 a) ソフトウェア構成管理戦略の作成 git-flowをそのままソフトウェア構成管理戦略と規定する想定
7.2.2.2 b) 生成される品目のベースライン化 生成される品目の識別
生成される品目の定義
ベースライン化に必要なレビュー等が作業フローに含まれるものとする
7.2.2.2 c) 品目の修正の制御
品目のリリースの制御
7.2.2.2 d) 修正の利用
リリースの利用
7.2.2.2 e) 品目及び修正の状態の記録 品目及び修正の状態の報告
7.2.2.2 f) 品目の完全性の確保
品目の一貫性の確保
7.2.2.2 g) 品目の保管の制御 品目の取り扱いの制御 品目の納入の制御
7.2.2.3 - - - - -
7.2.2.3.1 - - - - -
7.2.2.3.1.1 構成管理アクティビティの文書化
構成管理アクティビティを遂行するための手順の文書化
構成管理アクティビティを遂行するためのスケジュールの文書化
構成管理アクティビティを遂行する責任を負う組織の文書化
ソフトウェア開発,保守などの他の組織との関係の文書化
git-flowをそのまま文書として使用する想定
7.2.2.3.2 - - - - -
7.2.2.3.2.1 版の参照番号の識別 ベースラインを確立する文書の識別
その他の識別の詳細
7.2.2.3.3 - - - - -
7.2.3.3.1 変更依頼の識別及び記録
修正されたソフトウェア品目の実装,検証及びリリース各修正の追跡用監査証跡
修正されたソフトウェア品目の修正の承認の追跡用監査証跡
構成制御されたソフトウェア品目に対するアクセスの制御
変更の分析及び評価
変更依頼の承認又は不承認
修正されたソフトウェア品目の修正理由の追跡用監査証跡
構成制御されたソフトウェア品目に対するアクセスの監査
7.2.2.3.4 - - - - -
7.2.2.3.4.1 ソフトウェア品目の状態及び履歴を示す管理記録
プロジェクトにおける変更回数の記録
リリースの識別子の生成
リリースの回数の記録
リリース間の比較
最新のソフトウェア品目の版 状態報告書の準備
ベースラインの準備
7.2.2.3.5 - - - - -
7.2.2.3.5.1 要求事項に対するソフトウェア品目の機能的な完全さ及びソフトウェア品目の物理的な完全さの決定と保証
7.2.2.3.6 - - - - -
7.2.2.3.6.1 ソフトウェア製品及び文書のリリース及び納入の管理 コード及び文書の原本の保守
安全性又はセキュリティの重大な機能を含んだコード及び文書の関係する組織の方針に従った保管と納入

Rounding strategy in Java/JavaScript/PowerShell

tl;dr

  • Java: Floor in type casting. Round half up for Math.round(). You can choose rouding strategy for BigDecimal.
  • JavaScript: Round half up for Math.round(). Round half away from zero for toFixed().
  • PowerShell (.NET CLR): Round half to even (bankers' rounding) in type casting, and [math]::Round(). You can choose rouding strategy in [math]::Round() with optional argument.

Java

java> double [] arr = {-3.5,-2.5,-1.5,-0.5,0.5,1.5,2.5,3.5};
java> for (int i=0; i<arr.length; i++) { System.out.println((int)arr[i]); }
-3
-2
-1
0
0
1
2
3
java> for (int i=0; i<arr.length; i++) { System.out.println(Math.round(arr[i])); }
-3
-2
-1
0
1
2
3
4
java> for (int i=0; i<arr.length; i++) { System.out.println(BigDecimal.valueOf(arr[i]).setScale(0,BigDecimal.ROUND_HALF_UP)); }
-4
-3
-2
-1
1
2
3
4
java> for (int i=0; i<arr.length; i++) { System.out.println(BigDecimal.valueOf(arr[i]).setScale(0,BigDecimal.ROUND_HALF_DOWN)); }
-3
-2
-1
0
0
1
2
3
java> for (int i=0; i<arr.length; i++) { System.out.println(BigDecimal.valueOf(arr[i]).setScale(0,BigDecimal.ROUND_HALF_EVEN)); }
-4
-2
-2
0
0
2
2
4

JavaScript

var arr = [-3.5,-2.5,-1.5,-0.5,0.5,1.5,2.5,3.5];
for (i in arr) { console.log(Math.round(arr[i])); }
-3
-2
-1
-0
1
2
3
4
undefined
for (i in arr) { console.log((arr[i]).toFixed()); }
-4
-3
-2
-1
1
2
3
4

PowerShell

see http://satob.hatenablog.com/entry/2017/06/01/234802 .

PowerShell does banker's rounding in type casting to int

Problem:

PowerShell rounds 0.5 to 0 (banker’s rounding) in type casting to int.

PS > @(-3.5,-2.5,-1.5,-0.5,0.5,1.5,2.5,3.5) | ForEach-Object { "{0} -> {1}" -F $_, [int]$_ }
-3.5 -> -4
-2.5 -> -2
-1.5 -> -2
-0.5 -> 0
0.5 -> 0
1.5 -> 2
2.5 -> 2
3.5 -> 4

Hey, Scripting Guy! Blog says use [math]::Round() to round numbers, but [math]::Round() does also banker’s rounding in default.

PS > @(-3.5,-2.5,-1.5,-0.5,0.5,1.5,2.5,3.5) | ForEach-Object { "{0} -> {1}" -F $_, [math]::Round($_) }       -3.5 -> -4                                                                                                              
-2.5 -> -2
-1.5 -> -2
-0.5 -> 0
0.5 -> 0
1.5 -> 2
2.5 -> 2
3.5 -> 4

Solution:

Use [math]::Round() with option "AwayFromZero" when you want to round 0.5 to 1.

PS > @(-3.5,-2.5,-1.5,-0.5,0.5,1.5,2.5,3.5) | ForEach-Object { "{0} -> {1}" -F $_, [math]::Round($_,0,"AwayFromZero") }
-3.5 -> -4
-2.5 -> -3
-1.5 -> -2
-0.5 -> -1
0.5 -> 1
1.5 -> 2
2.5 -> 3
3.5 -> 4