首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用PowerShell创建新的ConfigurationElement对象或阻止对现有对象的更改修改其源?

如何使用PowerShell创建新的ConfigurationElement对象或阻止对现有对象的更改修改其源?
EN

Stack Overflow用户
提问于 2014-12-18 20:02:55
回答 1查看 317关注 0票数 1

我正在使用WebAdministration模块通过PowerShell进行IIS管理,并希望创建一个新的ConfigurationElement对象以附加到现有ConfigurationElements的数组中。

完成后,修改后的数组将应用回IIS站点。

目前,我正在尝试制作数组中现有ConfigurationElement对象的副本,但每次我修改该项时,原始对象也会被修改。我知道PowerShell数组和引用,所以我一直在使用原始数组,克隆它,然后访问克隆数组中的一项以进行“复制”。这是我修改的“副本”,但我发现克隆数组中的原始对象也被修改了。

代码语言:javascript
复制
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更新它的源?

EN

回答 1

Stack Overflow用户

发布于 2014-12-18 20:26:36

我使用了一种复杂的方法-答案是使用New-WebBinding cmdlet向站点添加一个新的绑定,而不是修改现有的bindingCollection并将其应用回来。

添加net.tcp绑定的示例如下:

代码语言:javascript
复制
New-WebBinding -Name $siteName -Protocol "net.tcp" -Port 334 -IPAddress *
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27546172

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档