1

ソリューションPowerGUIを使用して、2 つの分離された Powershell ファイルを 1 つの .EXE にコンパイルしようとしています。そのための Dependencies というボタンさえあるので、この機能をサポートしているようです。しかし、同じ.EXEに含まれる他のPowerShellファイルから依存関係ファイルを参照する方法の例は見つかりませんでした

メインの PS ファイルには、次の行のみが含まれています。

Start-Process powershell.exe -ArgumentList "-noexit -file C:\PGM\usbControl.ps1"

「C:\PGM\usbControl.ps1」を .EXE パッケージ内の相対パスとしてエンコードし、含まれている依存関係を指す方法を知りたいです。

ありがとう。

4

1 に答える 1

0

最後に、外部 IDE を使用する必要はありませんでした。標準の PowerShell コードでうまくいきました。埋め込みコード!:) ScriptBlock が鍵でした。

$sb = {

        $query = 'SELECT * FROM __InstanceOperationEvent WITHIN 5 WHERE TargetInstance ISA ''Win32_LogicalDisk'' AND TargetInstance.DriveType=2'

        Register-WmiEvent -Query $query -SourceIdentifier RemovableDiskDetection -Action {
            $class = $eventArgs.NewEvent.__CLASS
            $device = $eventArgs.NewEvent.TargetInstance.DeviceID

            $wshell = New-Object -ComObject Wscript.Shell
            switch ($class)
            {
                __InstanceCreationEvent {
                    $path = $device + '\flag\'
                    Write-Host '*** Checking the existence of the file $path'
                    if (Test-Path -Path $path)
                    {
                        $wshell.Popup('Inserted, device id: $device WITH flag', 0, 'Done', 0x1)

                    }
                    else
                    {
                        $wshell.Popup('Inserted, device id: $device WITHOUT flag', 0, 'Done', 0x1)
                    }
                }
                __InstanceDeletionEvent {
                    $wshell.Popup('Removed, device id: $device ', 0, 'Done', 0x1)
                }
           }
        }
}

start-process powershell.exe -argument "-noexit -nologo -noprofile -windowstyle hidden -command $sb"

この素晴らしい回避策の後、PS2EXEツールを使用してコンパイルしました。

.\ps2exe.ps1  -noConsole -inputFile .\magic.ps1 -outPutFile magic.exe
于 2016-02-12T09:49:40.923 に答える