You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VB.net中嵌入PowerShell脚本无法加载执行的问题求助

解决VB.NET中无法加载嵌入式PowerShell脚本的问题

核心问题分析

代码找不到嵌入式脚本的核心原因是资源名称格式错误。.NET中嵌入式资源的完整名称不是单纯的script.ps1,而是[项目命名空间].[文件名]的组合形式;如果脚本放在子文件夹中,还要加上文件夹路径,比如MyProject.Scripts.script.ps1。

解决步骤

1. 确认脚本的嵌入设置

右键script.ps1文件 → 属性 → 确保“生成操作”选择嵌入的资源,“复制到输出目录”选“不复制”。

2. 查看所有嵌入式资源的真实名称

添加调试代码枚举当前程序集的所有嵌入式资源,找到脚本的正确全名:

Private Sub ExecutePowerShellScript()
    ' 先枚举所有嵌入式资源,找到正确的脚本名称后可注释掉
    Dim assembly As System.Reflection.Assembly = System.Reflection.Assembly.GetExecutingAssembly()
    Dim allResources = assembly.GetManifestResourceNames()
    MessageBox.Show(String.Join(Environment.NewLine, allResources), "所有嵌入式资源")

    ' 后续原有代码...
End Sub

运行后弹窗会列出所有资源的完整名称,复制对应script.ps1的全名替换代码中的scriptResourceName即可。

3. 修正资源加载代码(可选:处理编码问题)

如果脚本是UTF-8编码(带BOM或不带),建议读取流时指定编码,避免乱码导致执行出错:

Private Function GetEmbeddedResourceText(ByVal assembly As System.Reflection.Assembly, ByVal resourceName As String) As String
    Using stream As System.IO.Stream = assembly.GetManifestResourceStream(resourceName)
        If stream IsNot Nothing Then
            ' 根据脚本实际编码调整,这里以UTF-8为例
            Using reader As New System.IO.StreamReader(stream, System.Text.Encoding.UTF8)
                Return reader.ReadToEnd()
            End Using
        End If
    End Using
    Return Nothing
End Function

4. 完整修正后的示例代码

Private Sub ExecutePowerShellScript()
    Dim assembly As System.Reflection.Assembly = System.Reflection.Assembly.GetExecutingAssembly()
    
    ' 调试用:列出所有资源名,找到正确名称后可注释
    Dim allResources = assembly.GetManifestResourceNames()
    MessageBox.Show(String.Join(Environment.NewLine, allResources), "所有嵌入式资源")

    ' 替换为你实际的资源全名
    Dim scriptResourceName As String = "YourProjectNamespace.script.ps1"

    Using ps As PowerShell = PowerShell.Create()
        Dim script As String = GetEmbeddedResourceText(assembly, scriptResourceName)

        If script IsNot Nothing Then
            ps.AddScript(script)
            Dim results As Collection(Of PSObject) = ps.Invoke()

            If ps.Streams.Error.Count > 0 Then
                Dim errorMessage As String = ""
                For Each errorRecord As ErrorRecord In ps.Streams.Error
                    errorMessage &= errorRecord.ToString() & Environment.NewLine
                Next
                MessageBox.Show("执行PowerShell脚本出错:" & Environment.NewLine & errorMessage, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Else
                For Each result As PSObject In results
                    MessageBox.Show(result.ToString(), "执行结果", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Next
            End If
        Else
            MessageBox.Show("无法加载嵌入式脚本文件", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If
    End Using
End Sub

Private Function GetEmbeddedResourceText(ByVal assembly As System.Reflection.Assembly, ByVal resourceName As String) As String
    Using stream As System.IO.Stream = assembly.GetManifestResourceStream(resourceName)
        If stream IsNot Nothing Then
            Using reader As New System.IO.StreamReader(stream, System.Text.Encoding.UTF8)
                Return reader.ReadToEnd()
            End Using
        End If
    End Using
    Return Nothing
End Function

额外注意事项

  • 如果脚本放在项目子文件夹中,资源名称会包含文件夹路径,比如脚本在Scripts文件夹下,资源名就是YourNamespace.Scripts.script.ps1
  • 必须使用“嵌入的资源”作为生成操作,“附加文件”无法通过GetManifestResourceStream读取

内容的提问来源于stack exchange,提问作者samfisher5986

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.14 09:22:03