【问题标题】:System.Management.Automation.PowerShell does not return results for Get-Website from IISSystem.Management.Automation.PowerShell 不从 IIS 返回 Get-Website 的结果
【发布时间】:2019-03-08 17:22:34
【问题描述】:

我们使用System.Management.Automation.PowerShell 来执行 PowerShell 脚本。

在我们需要在 IIS 上设置网站的某些属性(ServerAutostart 等)之前,在许多情况下一切正常。

我们制作了一个 PS1 脚本,它按预期工作,但是,在通过 C# 执行后,它崩溃了。经过一番摸索,我提取出了有问题的部分:

PS1 脚本:

# Set execution policy
Set-ExecutionPolicy Unrestricted -Scope Process -Force
Set-ExecutionPolicy Unrestricted -Scope LocalMachine -Force
Set-ExecutionPolicy Unrestricted -Scope CurrentUser -Force

# Import modules
Import-Module WebAdministration
Import-Module IISAdministration -MinimumVersion 1.1.0.0

# Getting IIS Sites
$iisSite = Get-IISSite

If ($iisSite)
{
    Write-Host "$($iisSite.Count) IISSites were found"
} 
else 
{
    Write-Error "IISSite was not found"
}

$webSite = Get-Website

# Getting Web Sites

If ($webSite)
{
    Write-Host "$($webSite.Count) WebSites were found"
} 
else 
{
    Write-Error "WebSites was not found"
} 

如果我通过Windows PowerShell ISE 运行它,它会完美运行:

23 IISSites were found
23 WebSites were found

但是,如果脚本是通过 C# 和 System.Management.Automation.PowerShell 执行的,它会返回以下内容:

23 IISSites were found
WebSites was not found

有什么区别?为什么Get-IISSite 有效而Get-Website 无效?权限?缺少模块?

要设置网站属性,我们需要通过Get-WebSite 使用它。不幸的是,这似乎是一个概念问题...

感谢您的帮助...

【问题讨论】:

  • 您是否“以管理员身份”运行您的 C# 应用程序?
  • 是的,这是我尝试的第一件事。没有效果。

标签: c# powershell iis


【解决方案1】:

问题是由 64 位系统的 Build 选项卡中的首选 32 位模式引起的。

Prefering 32-bit mode

取消勾选后,一切正常。

Powershell Invoke method neither throwing exception nor returning result

【讨论】:

    猜你喜欢
    • 2020-06-23
    • 1970-01-01
    • 2020-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-19
    • 1970-01-01
    • 2020-02-28
    相关资源
    最近更新 更多