Powershell IIS 获取绑定列表导出 csv 问题 System.Object
Powershell IIS Get-Binding list export-csv issue System.Object
我正在尝试获取一个列出 IIS 站点名称、文件路径和绑定的脚本,它在我导出 csv 时工作正常。这样做时,我在路径的结果中得到 System.Object[],但不确定如何更正此问题。
Get-WebBinding | % {
$names = $_.ItemXPath -replace '(?:.*?)name=''([^'']*)(?:.*)', ''
$path = get-website $names | select-object physicalpath
New-Object psobject -Property @{
Binding = $_.bindinginformation.Split(":")[-1]
path = $path
Name = $names
} } | export-csv c:\test1.csv -notypeinformation
我实际上为此创建了一个更优雅的解决方案
解决方案:
Import-Module WebAdministration
$hostname = hostname
$Websites = Get-ChildItem IIS:\Sites
$date = (Get-Date).ToString('MMddyyyy')
foreach ($Site in $Websites) {
$Binding = $Site.bindings
[string]$BindingInfo = $Binding.Collection
[string[]]$Bindings = $BindingInfo.Split(" ")#[0]
$i = 0
$status = $site.state
$path = $site.PhysicalPath
$fullName = $site.name
$state = ($site.name -split "-")[0]
$Collection = ($site.name -split "-")[1]
$status = $site.State
$anon = get-WebConfigurationProperty -Filter /system.webServer/security/authentication/AnonymousAuthentication -Name Enabled -PSPath IIS:\sites -Location $site.name | select-object Value
$basic = get-WebConfigurationProperty -Filter /system.webServer/security/authentication/BasicAuthentication -Name Enabled -PSPath IIS:\ -location $site.name | select-object Value
Do{
if( $Bindings[($i)] -notlike "sslFlags=*"){
[string[]]$Bindings2 = $Bindings[($i+1)].Split(":")
$obj = New-Object PSObject
$obj | Add-Member Date $Date
$obj | Add-Member Host $hostname
$obj | Add-Member State $state
$obj | Add-Member Collection $Collection
$obj | Add-Member SiteName $Site.name
$obj | Add-Member SiteID $site.id
$obj | Add-member Path $site.physicalPath
$obj | Add-Member Protocol $Bindings[($i)]
$obj | Add-Member Port $Bindings2[1]
$obj | Add-Member Header $Bindings2[2]
$obj | Add-member AuthAnon $Anon.value
$obj | Add-member AuthBasic $basic.value
$obj | Add-member Status $status
$obj #| export-csv "c:\temp$date-$hostname.csv" -Append -notypeinformation
$i=$i+2
}
else{$i=$i+1}
} while ($i -lt ($bindings.count))
}
发生这种情况是因为 CSV 输出在嵌套对象上确实很糟糕。如果您查看您的代码,您的 "path" 属性基本上是网站对象的 "slice"。您想要做的可能是扩展该变量,以便您取回实际值(请参见下面的示例)。这是在 ISE 中进行 2 分钟调试非常有用的时候之一!
Get-WebBinding | ForEach-Object {
$names = $_.ItemXPath -replace '(?:.*?)name=''([^'']*)(?:.*)', ''
$path = get-website $names | select-object -expandproperty physicalpath
New-Object psobject -Property @{
Binding = $_.bindinginformation.Split(":")[-1]
path = $path
Name = $names
}
}| export-csv c:\test1.csv -notypeinformation
我正在尝试获取一个列出 IIS 站点名称、文件路径和绑定的脚本,它在我导出 csv 时工作正常。这样做时,我在路径的结果中得到 System.Object[],但不确定如何更正此问题。
Get-WebBinding | % {
$names = $_.ItemXPath -replace '(?:.*?)name=''([^'']*)(?:.*)', ''
$path = get-website $names | select-object physicalpath
New-Object psobject -Property @{
Binding = $_.bindinginformation.Split(":")[-1]
path = $path
Name = $names
} } | export-csv c:\test1.csv -notypeinformation
我实际上为此创建了一个更优雅的解决方案
解决方案:
Import-Module WebAdministration
$hostname = hostname
$Websites = Get-ChildItem IIS:\Sites
$date = (Get-Date).ToString('MMddyyyy')
foreach ($Site in $Websites) {
$Binding = $Site.bindings
[string]$BindingInfo = $Binding.Collection
[string[]]$Bindings = $BindingInfo.Split(" ")#[0]
$i = 0
$status = $site.state
$path = $site.PhysicalPath
$fullName = $site.name
$state = ($site.name -split "-")[0]
$Collection = ($site.name -split "-")[1]
$status = $site.State
$anon = get-WebConfigurationProperty -Filter /system.webServer/security/authentication/AnonymousAuthentication -Name Enabled -PSPath IIS:\sites -Location $site.name | select-object Value
$basic = get-WebConfigurationProperty -Filter /system.webServer/security/authentication/BasicAuthentication -Name Enabled -PSPath IIS:\ -location $site.name | select-object Value
Do{
if( $Bindings[($i)] -notlike "sslFlags=*"){
[string[]]$Bindings2 = $Bindings[($i+1)].Split(":")
$obj = New-Object PSObject
$obj | Add-Member Date $Date
$obj | Add-Member Host $hostname
$obj | Add-Member State $state
$obj | Add-Member Collection $Collection
$obj | Add-Member SiteName $Site.name
$obj | Add-Member SiteID $site.id
$obj | Add-member Path $site.physicalPath
$obj | Add-Member Protocol $Bindings[($i)]
$obj | Add-Member Port $Bindings2[1]
$obj | Add-Member Header $Bindings2[2]
$obj | Add-member AuthAnon $Anon.value
$obj | Add-member AuthBasic $basic.value
$obj | Add-member Status $status
$obj #| export-csv "c:\temp$date-$hostname.csv" -Append -notypeinformation
$i=$i+2
}
else{$i=$i+1}
} while ($i -lt ($bindings.count))
}
发生这种情况是因为 CSV 输出在嵌套对象上确实很糟糕。如果您查看您的代码,您的 "path" 属性基本上是网站对象的 "slice"。您想要做的可能是扩展该变量,以便您取回实际值(请参见下面的示例)。这是在 ISE 中进行 2 分钟调试非常有用的时候之一!
Get-WebBinding | ForEach-Object {
$names = $_.ItemXPath -replace '(?:.*?)name=''([^'']*)(?:.*)', ''
$path = get-website $names | select-object -expandproperty physicalpath
New-Object psobject -Property @{
Binding = $_.bindinginformation.Split(":")[-1]
path = $path
Name = $names
}
}| export-csv c:\test1.csv -notypeinformation