c# - System.Management.Automation.PowerShell 不从 IIS 返回 Get-Website 的结果
问题描述
我们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
. 不幸的是,这似乎是一个概念问题......
感谢您的帮助...
解决方案
推荐阅读
- azure - 如何重新创建实时聊天机器人?
- c# - 使用 Swashbuckle ISchemaFilter 设置输入参数的默认描述
- python - 如何将此验证码图像拆分为每个单词的 5 块?
- javascript - 如何检查Angular中的日期是否低于4年?
- go - 在heroku上部署一个golang应用程序,构建成功但应用程序错误
- java - 使用 IAM 凭证通过 JDBC 从 Spring 连接到 AWS Aurora 是否合适?
- python - Python中的“尝试相对导入超出顶级包”错误意味着什么?
- python - TypeError:加载 .npy 文件时图像数据无法转换为浮点数
- c++ - 如果无效,如何重复用户输入的电子邮件
- react-native-android - 如何在 react-native 中使用 axios 而不是 fetch?