1

我正在尝试使用解决方案PowerGUI将两个单独的 Powershell 文件编译成一个 .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 回答