Windows PowerShell ってみなさんご存じでしょうか?
たまにはエンジニアリングしておこうと思う marionnettezero です。

普段利用している OS で Windows の方は、大変世の中に多いと思いますが、意外と Windows の事を知らない人が多いのかなと思っています。
かつては専門家と一部の人のみが利用していた PC を一般の人にも広げたのは、間違いなく Windows の登場であると思っています。
そんな Windows の事を知らないなんて、みんな失礼だぞ!

という事で今日は、Windows については書きません。
「え?その流れでなぜ書かないんだ?」
と思われる方もいるかと思いますが、Windows の事は、検索エンジンにでも聞いてください。
※ むしろその方が詳しいですよ!

という事で、今回の内容はこちら。

 

Windows PowerShell は管理者用のコマンドラインツールなのですが、これをもっと技術力のない人でも利用可能に、GUIを用意してあげたりしたい時があります。
※ え?そもそも使わない?便利なものは使ってください!

そんな時には、さてどうしたらよいでしょうか?
そう答えは簡単、GUIのバックグラウンドで Windows PowerShell のコンソールを操作してしまえばいいのです。
まあ、言うのは簡単ですが、おそらくインターネット上探してもあまり情報はありません。
情報がほしい方は、英語圏に飛んでください。


でも・・・せっかくブログ見てくれたので、ちょっとしたやり方は書いておきます。
べっ・・・べつに、あなたの為じゃないんだからね!

1.まずは、Windows PowerShell コンソールをオープン

' PS:Runsapce のオープン
Public Function PSOpen(ByRef istrErrorText As String, Optional ByVal rsScript As System.Management.Automation.Runspaces.ScriptConfigurationEntry = Nothing) As Boolean
    ' エラーメッセージを初期化
    istrErrorText = ""

    ' 例外処理開始
    Try
        ' RunspaceConfiguration の取得
        rsConfig = System.Management.Automation.Runspaces.RunspaceConfiguration.Create()

        ' スクリプトの指定がある場合
        If Not rsScript Is Nothing Then
            Dim rsScripts As System.Management.Automation.Runspaces.RunspaceConfigurationEntryCollection(Of ScriptConfigurationEntry) = rsConfig.Scripts
            rsScripts.Append(rsScript)
        End If

        ' Runspace の取得
        rsRunSpace = System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace(rsConfig)

        ' Runspace をオープン
        rsRunSpace.Open()

        ' 正常終了
        Return True

    ' 例外処理実態
    Catch ex As Exception
        ' エラーメッセージを保存
        istrErrorText = ex.Message
        ' 異常終了
        Return False
    End Try
End Function

2.とりあえず、認証オブジェクトでも取得してみようか。

' PS:Get-Credential の実行
Function PSGetCredential(ByRef istrErrorText As String) As ObjectModel.Collection(Of PSObject)
    Dim objResult As ObjectModel.Collection(Of PSObject) = Nothing          ' 戻り値格納用
    Dim crCredential As System.Management.Automation.PSCredential = Nothing ' PSCredential インスタンス格納用

    ' エラーメッセージを初期化
    istrErrorText = ""

    ' 例外処理開始
    Try
        ' パスワードをセキュリティ文字列に変換
        Dim strPassword As String = System.Configuration.ConfigurationManager.ConnectionStrings.Item("AdminPWD").ConnectionString
        Dim sssPassword As System.Security.SecureString = New System.Security.SecureString()

        Dim intI As Integer = 1
        For intI = 1 To Len(strPassword)
            sssPassword.AppendChar(CChar(Mid(strPassword, intI, 1)))
        Next

        ' 認証オブジェクトを生成
        crCredential = New System.Management.Automation.PSCredential(System.Configuration.ConfigurationManager.ConnectionStrings.Item("AdminID").ConnectionString, sssPassword)

        ' *** PS コマンドの実行:START ***
        ' コマンドを定義
        Dim rsCmd As New System.Management.Automation.Runspaces.Command("Get-Credential")
        ' 引数の設定
        rsCmd.Parameters.Add("-Credential", crCredential)

        ' Pipeline の生成
        Dim rsPipeline As System.Management.Automation.Runspaces.Pipeline = rsRunSpace.CreatePipeline()
        ' Pipeline にコマンドを定義
        rsPipeline.Commands.Add(rsCmd)

        ' PS コマンドを実行し戻り値を取得する
        objResult = rsPipeline.Invoke

        ' Pipeline の破棄
        rsPipeline.Dispose()
        ' *** PS コマンドの実行:END ***
    ' 例外処理実態
    Catch ex As Exception
        ' エラーメッセージを保存
        istrErrorText = ex.Message
    End Try

    ' 戻り値を返す
    Return objResult
End Function

3.終了処理もちゃんとしよう。

' PS:Runsapce のクローズ
Public Function PSClose(ByRef istrErrorText As String) As Boolean
    ' エラーメッセージを初期化
    istrErrorText = ""

    ' 例外処理開始
    Try
        ' Runspace を破棄
        rsRunSpace.Close()
        rsRunSpace.Dispose()

        ' 正常終了
        Return True

    ' 例外処理実態
    Catch ex As Exception
        ' エラーメッセージを保存
        istrErrorText = ex.Message
        ' 異常終了
        Return False
    End Try
End Function

1~3を実行すれば、PSコンソールをオープンして認証してクローズまでができると思います。
詳しい利用方法は、MSのサイト等に詳しく乗っているので見てみてください。
新しい技術に興味を持つ事は良いことですが、自分の近くにある技術も思い出してあげてください。

今回はツンデレ風でお送りいたしました。