我正在使用WebAdministration模块通过PowerShell进行IIS管理,并希望创建一个新的ConfigurationElement对象以附加到现有ConfigurationElements的数组中。
完成后,修改后的数组将应用回IIS站点。
目前,我正在尝试制作数组中现有ConfigurationElement对象的副本,但每次我修改该项时,原始对象也会被修改。我知道PowerShell数组和引用,所以我一直在使用原始数组,克隆它,然后访问克隆数组中的一项以进行“复制”。这是我修改的“副本”,但我发现克隆数组中的原始对象也被修改了。
Import-Module WebAdministration
$siteName = "MySite"
$site = Get-Website | Where { $_.Name -eq $siteName }
$foundSite = $site.Name
# Switch to IIS sites
cd IIS:\Sites
# Get our current IIS Site bindings collection from the site.
$bindingCollection = Get-ItemProperty $foundSite -Name bindings
# Clone the collection array
$tempBindingCollection = $bindingCollection.Collection.Clone()
# We now do our modifications in the cloned/copied array, so as not to affect our main array which will be sent back
# to IIS to configure the bindings later on...
$newBinding = $tempBindingCollection[0]
$newBinding.protocol = "net.tcp"
$newBinding.bindingInformation = $NetTCPBindingValue
# Now at this point, $newBinding has changed (the protocol and bindingInformation properties), but so has the original $bindingCollection.Collection[0] object!!
# I only want $newBinding to change.所以我的问题是-我怎么才能
A)使用PowerShell创建我自己的ConfigurationElement对象,这样我就不必克隆现有的ConfigurationElement,或者
B)当我修改它时,阻止我的克隆/复制的ConfigurationElement更新它的源?
发布于 2014-12-18 20:26:36
我使用了一种复杂的方法-答案是使用New-WebBinding cmdlet向站点添加一个新的绑定,而不是修改现有的bindingCollection并将其应用回来。
添加net.tcp绑定的示例如下:
New-WebBinding -Name $siteName -Protocol "net.tcp" -Port 334 -IPAddress *https://stackoverflow.com/questions/27546172
复制相似问题