首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用Powershell创建MSI提取器?

如何使用Powershell创建MSI提取器?
EN

Stack Overflow用户
提问于 2022-11-19 17:29:08
回答 1查看 52关注 0票数 -1

几个月前,我制作了一个简单的MSI提取器脚本,今天我使用WScript.Shell添加了一个弹出对话框,对其进行了一些改进,但是我也希望改进脚本的第一部分,即提取器。原始用户必须进入并手动编辑输入文件(.msi)和输出目录。我用老朋友google查看文件弹出语法是什么。试着把它加进剧本里,却没有运气。

当前代码:

代码语言:javascript
复制
msiexec /n /a $FileBrowser /qb TARGETDIR=$Out # This uses the built in Windows tool to extract the MSI

$FileBrowser = New-Object System.Windows.Forms.OpenFileDialog -Property @{ 
    InitialDirectory = [Environment]::GetFolderPath('Desktop') 
    Filter = 'Windows Packages (*.msi)|*.msi'
}
$Out = $FileBrowser.ShowDialog()

$Shell = New-Object -ComObject "WScript.Shell"
$Button = $Shell.Popup("Once you install the MSI using this PowerShell script, please add any programs that run from a shell (i.e. CMD, PowerShell) be added to Path. 
To add a program to path, search for Control Panel in Windows Search, and open it. Once in Control Panel,
select User Accounts, then User Accounts again. On the side bar, select Change my Enviorment Variables.
Select the Path variable, and then Edit. Select a unfilled box, and type the path to the program (for most, it can be just the root folder, some may need to be bin) and then Ok, and Ok again. 
You WILL need to restart any open shells.", 0, "Thank you for using MSI-Extractor", 0)

任何帮助都会得到极大的支持,

詹姆斯

我试着加入瓦尔斯,但没有运气。

编辑:我得到了一些外部的帮助,而且我对它做了一些修正。

代码语言:javascript
复制
#Use Windows Forms to open a file select dialog

$FileBrowser = New-Object System.Windows.Forms.OpenFileDialog -Property @{ 
    InitialDirectory = [Environment]::GetFolderPath('Desktop') 
    Filter = 'Windows Packages (*.msi)|*.msi'
}

$Out = $FileBrowser.ShowDialog() #Display the dialog

#Select output directory

$FolderBrowser = New-Object System.Windows.Forms.FolderBrowserDialog -Property @{
    Description = 'Output'
}

$Out = $FolderBrowser.ShowDialog() #Display the dialog

$FolderBrowser.SelectedPath #Variable stuff

msiexec /a $FileBrowser.FileName /qb TARGETDIR=$($FolderBrowser.SelectedPath) # This uses the built in Windows tool to extract the MSI

#A helpful message

$Shell = New-Object -ComObject "WScript.Shell"
$Button = $Shell.Popup("Once you install the MSI using this PowerShell script, please add any programs that run from a shell (i.e. CMD, PowerShell) be added to Path. 
To add a program to path, search for Control Panel in Windows Search, and open it. Once in Control Panel,
select User Accounts, then User Accounts again. On the side bar, select Change my Enviorment Variables.
Select the Path variable, and then Edit. Select a unfilled box, and type the path to the program (for most, it can be just the root folder, some may need to be bin) and then Ok, and Ok again. 
You WILL need to restart any open shells.", 0, "Thank you for using MSI-Extractor", 0)

在中运行此操作,但在Powershell中直接出错

代码语言:javascript
复制
New-Object : Cannot find type [System.Windows.Forms.OpenFileDialog]: verify that the assembly containing this type is
loaded.
At C:\Users\693982\Downloads\MSI-extractor.ps1:4 char:16
+ ... leBrowser = New-Object System.Windows.Forms.OpenFileDialog -Property  ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidType: (:) [New-Object], PSArgumentException
    + FullyQualifiedErrorId : TypeNotFound,Microsoft.PowerShell.Commands.NewObjectCommand

You cannot call a method on a null-valued expression.
At C:\Users\693982\Downloads\MSI-extractor.ps1:9 char:1
+ $Out = $FileBrowser.ShowDialog() #Display the dialog
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

New-Object : Cannot find type [System.Windows.Forms.FolderBrowserDialog]: verify that the assembly containing this
type is loaded.
At C:\Users\693982\Downloads\MSI-extractor.ps1:13 char:18
+ ... erBrowser = New-Object System.Windows.Forms.FolderBrowserDialog -Prop ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidType: (:) [New-Object], PSArgumentException
    + FullyQualifiedErrorId : TypeNotFound,Microsoft.PowerShell.Commands.NewObjectCommand

You cannot call a method on a null-valued expression.
At C:\Users\693982\Downloads\MSI-extractor.ps1:17 char:1
+ $Out = $FolderBrowser.ShowDialog() #Display the dialog
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull
EN

回答 1

Stack Overflow用户

发布于 2022-11-19 19:35:17

运行在Windows中的

可以工作,但在Powershell中直接出错

这在ISE中工作的原因是ISE将自动加载所需的库/模块。Powershell控制台(Powershell. The /pwsh.exe)不会。

您需要将这样的内容放在代码的顶部,以便加载shell,以便在基于GUI的脚本中使用。

代码语言:javascript
复制
Add-Type -AssemblyName  System.Drawing,
                        PresentationCore,
                        PresentationFramework,
                        System.Windows.Forms,
                        microsoft.VisualBasic
[System.Windows.Forms.Application]::EnableVisualStyles()

点注:

你所做的并不需要所有这些,但我把它们放在这里是为了你对它们的认识/研究。当然,你还是可以用这个旧的.

代码语言:javascript
复制
New-Object -ComObject "WScript.Shell"

..。但是,对于弹出窗口,您应该使用上面的现代.Net名称空间。

示例详细信息:

代码语言:javascript
复制
[enum]::GetNames([System.Windows.MessageBoxImage])
# Results
<#
None
Hand
Error
Stop
Question
Exclamation
Warning
Asterisk
Information
#>

[System.Windows.MessageBox]::Show('Do you want to proceed?', 'Confirm', 'YesNoCancel','Error')

使用.Net的示例消息框。

代码语言:javascript
复制
[System.Windows.MessageBox]::Show(
"
General Info
`n Some other Info
`n Username
`n Password,
", 'Dialog Title'
)


[System.Windows.Forms.MessageBox]::Show(
"
General Info
`n Some other Info
`n Username
`n Password
", 'Dialog Title'
)

这里是一个重构您的弹出消息,以提高可读性。

代码语言:javascript
复制
[System.Windows.MessageBox]::Show(
"1. Once you install the MSI using this PowerShell script, 
    please add any programs that run from a shell (i.e. CMD, PowerShell) 
    be added to Path. 
`n2. To add a program to path, 
`t->search for Control Panel in Windows Search, and open it. 
`n3. Once in Control Panel, 
`t->select User Accounts, 
`t->then User Accounts again. 
`n4. On the side bar, select Change my Enviorment Variables.
`tSelect the Path variable, and then Edit. 
`t`Select a unfilled box, and type the path to the program 
`t->for most, it can be just the root folder, 
`t->some may need to be bin
`t->and then Ok, and Ok again. 
`n5. You WILL need to restart any open shells.", "Thank you for using MSI-Extractor"
)
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74502154

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档