c# - 从 C# 调用 powershell 脚本时包含脚本不起作用

标签 c# powershell

我有一个 power shell 脚本,其中包含脚本,如下所示。

. "$PSScriptRoot\CheckPermissions.ps1"

当从 c# 调用脚本时,我收到此错误

{The term '\CheckPermissions.ps1' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.}

从 PS 窗口运行时,脚本运行良好。

从 C# 运行脚本时 $PSScriptRoot 变量不可用吗?

最佳答案

您可以尝试使用以下命令来获取脚本目录:

$scriptDir = Split-Path $script:MyInvocation.MyCommand.Path

请注意,返回的文件夹路径将以反斜杠 (\) 结尾。

关于c# - 从 C# 调用 powershell 脚本时包含脚本不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30616013/

相关文章:

PowerShell 最佳实践 : Is it bad to treat a PowerShell script like an advanced batch file?

c# - 如何使用C#Nest 2.0从ElasticSearch响应中查看API查询

c# - 使用 com interop 从 VBA 调用 C# dll 时出现类型不匹配错误

azure - Azure yml 文件中的 Powershell 变量未从一个阶段传递到其他阶段

powershell - azure powershell无法按名称找到资源(vm)

powershell - 远程安装打印机

c# - 如何使用一致的语法重写此 LINQ 表达式?

c# - SqlCommand 返回 1 但数据库未更新。我做错了什么?

c# - 将 Dapper 查询反序列化为模型

windows - 从 Windows 命令行获取文件夹大小