我安装了 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/