【发布时间】: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