PowerShell +网络管理 - 如何从网络应用程序获取网站?

发布于 2024-12-05 19:06:23 字数 440 浏览 1 评论 0原文

我正在编写一个 PowerShell 脚本来执行 IIS 7.5 中的某些管理功能。

import-module WebAdministration

在某些情况下,我知道我想要使用的 Web 应用程序的名称,但不知道它所在的网站。获取应用程序很容易:

$app = get-webapplication -name 'MyApp'

但我不知道如何获取给定应用程序的网站名称。它似乎不是 webapplication 对象的属性。我能想到的最好办法是尝试通过测试路径获取它:

get-website | where {test-path "iis:\sites\$_.name\MyApp"}

由于某种原因,结果是空的。关于如何解决这个问题有什么想法吗?提前致谢。

I'm writing a PowerShell script to perform certain administrative functions in IIS 7.5.

import-module WebAdministration

In some cases I know the name of the web application I want to work with but not the web site it is under. Getting the application is easy:

$app = get-webapplication -name 'MyApp'

But I cannot figure out how to get the name of the web site given the app. It doesn't seem to be a property off the webapplication object. Best I could come up with was trying to get it via test-path:

get-website | where {test-path "iis:\sites\$_.name\MyApp"}

For some reason that comes up empty. Any thoughts on how to go about this? Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

梅倚清风 2024-12-12 19:06:23

这是获取站点名称的方法:

$siteName = (Get-WebApplication -name 'YourApp').GetParentElement().Attributes['name'].Value

或者更短:

$siteName = (Get-WebApplication -name 'YourApp').GetParentElement()['name']

This is how you can get the site name:

$siteName = (Get-WebApplication -name 'YourApp').GetParentElement().Attributes['name'].Value

Or even shorter:

$siteName = (Get-WebApplication -name 'YourApp').GetParentElement()['name']
清晰传感 2024-12-12 19:06:23

我还没有深入研究原因,但如果您在字符串中的任何位置使用星号或问号,它就像通配符一样并返回。

IE

get-website -name '*myapp'

   Name      ID   State    Physical Path        Bindings
   ----      --   -----    -------------        --------

   myapp     12   Started  C:\inetpub\wwwroot   http:*:80:
  amyapp     13   Stopped  C:\anetpub\          http:*:81:
 aamyapp     14   Stopped  C:\another\place     http:172.198.1.2:80:host.header.com

get-website -name '?myapp'

Name   ID State   Physical Path Bindings
----   -- -----   ------------- --------
amyapp 13 Stopped C:\anetpub    http:*:81:

I haven't dug into why, but if you use an asterisk or question mark anywhere in the string it works like a wildcard and returns as such.

I.E.

get-website -name '*myapp'

   Name      ID   State    Physical Path        Bindings
   ----      --   -----    -------------        --------

   myapp     12   Started  C:\inetpub\wwwroot   http:*:80:
  amyapp     13   Stopped  C:\anetpub\          http:*:81:
 aamyapp     14   Stopped  C:\another\place     http:172.198.1.2:80:host.header.com

or

get-website -name '?myapp'

Name   ID State   Physical Path Bindings
----   -- -----   ------------- --------
amyapp 13 Stopped C:\anetpub    http:*:81:
千年*琉璃梦 2024-12-12 19:06:23

尝试一下,

$app = get-webapplication -name 'MyApp'
foreach($a in $app)
{
$a.Attributes[0].Value;
}

这将为您提供 Web 应用程序的所有名称,有时单个 Web 应用程序可能有多个名称。

有关详细信息,请参阅链接

http:// nisanthkv.blog.com/2012/07/06/name-of-web-applications-using-powershell/

希望有帮助..

Try this

$app = get-webapplication -name 'MyApp'
foreach($a in $app)
{
$a.Attributes[0].Value;
}

This will give you all the names of the Web Applications, sometimes you can have more than 1 name for a single Web Applications.

For more information see the link

http://nisanthkv.blog.com/2012/07/06/name-of-web-applications-using-powershell/

Hope it helps..

久随 2024-12-12 19:06:23

为什么这件事如此复杂和令人费解?

这应该只是工作:

$WebApplicationExists = ( Get-WebApplication |Where-Object { $_.Name -eq $WebAppName } | Measure-Object ).Count -gt 0

这工作:

$WebApplicationExists = ( Get-WebApplication -Name $WebAppName | Measure-Object ).Count -gt 0

$PSVersionTable.PSVersion

主要次要版本修订版


5 1 17763 3770

Why is this so complicated and convoluted?!

This should just work:

$WebApplicationExists = ( Get-WebApplication | Where-Object { $_.Name -eq $WebAppName } | Measure-Object ).Count -gt 0

This works:

$WebApplicationExists = ( Get-WebApplication -Name $WebAppName | Measure-Object ).Count -gt 0

$PSVersionTable.PSVersion

Major Minor Build Revision


5 1 17763 3770

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文