powershell - Set-ItemProperty 在 PowerShell 5.1 中工作,但在 7.0 中不起作用

标签 powershell

我安装了 PowerShell 7.0 并开始了一些测试。以下脚本在 PowerShell 5.1 中运行没有错误。

Import-Module WebAdministration

Set-ItemProperty "IIS:\Sites\TestSite" -Name physicalPath -Value "D:\Dev\Test\Website\WebSiteDown"

在 PowerShell 7.0 中,我尝试更改导入以使用更新的“IISAdministration”模块而不是旧的“WebAdministration”。我对两个模块都遇到相同的错误。

Set-ItemProperty: D:\Dev\OProTrunk\Website\WebSiteDown\OctopusCommand\Test.ps1:3
Line |
   3 |  Set-ItemProperty "IIS:\Sites\TestSite" -Name physicalPath -Value  …
     |  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | Cannot find drive. A drive with the name 'IIS' does not exist.

两者都以管理员身份运行,以便能够更新网站物理路径。我是否需要使用与 PowerShell 7.0 不同的格式设置属性?

最佳答案

据我所知,您需要在此处与 Windows PowerShell 的兼容性 session 进行交互。

Import-Module WebAdministration
$session = Get-PSSession -Name WinPSCompatSession
$sb = {Set-ItemProperty "IIS:\Sites\TestSite" -Name physicalPath -Value "D:\Dev\Test\Website\WebSiteDown"}
Invoke-Command -Scriptblock $sb -Session $session

IIS PSDrive 由 WebAdministration 提供程序创建。由于该提供程序仅在 Windows PowerShell 中加载(至少在我的系统上),因此您需要在那里访问它。

关于powershell - Set-ItemProperty 在 PowerShell 5.1 中工作,但在 7.0 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60874533/

相关文章:

powershell - Powershell尝试将用户从一个AD组复制到另一个AD组

powershell - 多维数组初始化似乎对空格敏感

azure - 使用 Powershell 获取逻辑应用列表的新 AZ 命令是什么?

powershell - 更新 Azure Powershell 模块后,无法再导入我的模块并出现奇怪的错误

c# - 如何在 C# 中使用 PowerShell 的 Invoke-WebRequest

powershell - 新-AzureRmVM : Parameter set cannot be resolved using the specified named parameters

powershell - 在 Visual Studio Nuget 包管理器控制台中中止 "unclosed"powershell 命令

PowerShell 提示路径,但路径有效

javascript - 如何在Powershell中将实时输出保存到txt文件?监控脚本

python - Powershell 文件路径命令正在被 Pycharm 更改