【问题标题】:Powershell Set-Item returns Object reference not set to an object of an instancePowershell Set-Item 返回未设置为实例对象的对象引用
【发布时间】:2017-06-02 18:25:42
【问题描述】:

我正在脚本中运行以下命令:

$currentSite = Get-Item IIS:\Sites\$WebSiteName
$currentSite.id = $WebSiteID
$currentSite | Set-Item

我想在哪里设置网站的 ID。 此脚本适用于某些环境,但不适用于其他环境(寻找不同之处希望尽快发布)。

如果它不起作用,它会返回以下错误:

Set-Item : Object reference not set to an instance of an object.
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Set-Item], NullReferenceException
    + FullyQualifiedErrorId : path,Microsoft.PowerShell.Commands.SetItemCommand

所以,为了寻找解决方法,我尝试了以下代码:

$currentSite = Get-Item IIS:\Sites\$WebSiteName
$currentSite | Set-Item

但我得到了同样的错误。

所以我想知道 Set-Item 是否需要一些参数,但 Get-Item 在与站点一起使用时没有返回(我指定了站点,因为我使用相同的结构设置应用程序池,但我没有'没有任何问题)

【问题讨论】:

  • 收到错误时$currentSite | gm 会返回什么?
  • TypeName: System.Object 你也需要这份名单吗?
  • Set-Item 调用之前尝试if ($currentSite -ne $null) { Write-Host currentSite is NOT null! }。从错误来看,path 似乎为空,或者如果不为空,则它指向无处。因此,您应该尝试使用其他 Set-Item 语法,例如:Set-Item -path IIS:\Sites\$WebSiteName -value $currentSite

标签: powershell powershell-4.0 powershell-5.0


【解决方案1】:

即使这不能回答问题,我也找到了解决问题的方法:

代替使用

$currentSite = Get-Item IIS:\Sites\$WebSiteName
$currentSite.id = $WebSiteID
$currentSite | Set-Item

可以使用以下命令设置网站的 id:

Set-ItemProperty -Path IIS:\Sites\$WebSiteName -Name id -Value $WebSiteID

【讨论】:

    猜你喜欢
    • 2014-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多