【问题标题】:Set-ItemProperty working in PowerShell 5.1, but not in 7.0Set-ItemProperty 在 PowerShell 5.1 中工作,但在 7.0 中不工作
【发布时间】:2020-04-03 04:52:22
【问题描述】:

我安装了 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 格式化不同的属性?

【问题讨论】:

    标签: powershell


    【解决方案1】:

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

    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 中加载(至少在我的系统上),因此您需要在那里访问它。

    【讨论】:

    • 当我这样做时,变量 $session 返回为空。我错过了安装的东西吗?
    • 在 PowerShell 7.0 中,如果您运行 Import-Module WebAdministration,您应该会收到有关不兼容的消息。该消息将说明创建的会话名称。这就是您需要在 Get-PSSession 命令中使用的内容。如果您根本没有其他会话,您可以使用$session = Get-PSSession
    • 当我将行从 $session = Get-PSSession -Name WinPSCompat* 更改为 $session = Get-PSSession -Name WinPSCompatSession 时,它起作用了。感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-20
    • 2014-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多