无法将 IIS 结果导出到 CSV
我创建了一个脚本来从多个服务器获取所有 IIS 站点和应用程序池详细信息,该脚本按预期工作正常,但我无法将结果导出到 CSV 文件。
如果有人建议我如何将输出详细信息导出到 CSV 文件,这将会很有帮助。
代码
#Import-Module -Name WebAdministration
$Computers = Get-Content "C:\Desktop\Scripts\Servers.txt"
foreach ($server in $Computers) {
$iis = Get-WmiObject Win32_Service -Filter "Name = 'IISADMIN'" -ComputerName $server
if ($iis.State -eq 'Running')
{
$details = Write-Host "IIS is running on $server" -ForegroundColor Green
#Chandu
Invoke-Command -ComputerName $Computers -ScriptBlock {
# Changed to newer IISAdministration Module to match Get-IISAppPool
$Websites = Get-IISSite
foreach ($Website in $Websites) {
$AppPool = Get-IISAppPool -Name $Website.Applications[0].ApplicationPoolName
#$Website.ApplicationPool
[PSCustomObject]@{
Server_Name = $env:COMPUTERNAME +'.'+ $env:USERDNSDOMAIN
Website_Name = $Website.Name
Website_Id = $Website.Id -join ';'
Website_State = $Website.State -join ';'
Website_PhysicalPath = $Website.PhysicalPath -join ';'
Website_Bindings = $Website.Bindings.Collection -join ';'
Website_Attributes = ($Website.Attributes | ForEach-Object { $_.name + "=" + $_.value }) -join ';'
AppPool_Name = $AppPool.Name -join';'
AppPool_State = $AppPool.State -join ';'
AppPool_ManagedRuntimeVersion = $AppPool.ManagedRuntimeVersion -join ';'
AppPool_ManagedPipelineMode = $AppPool.ManagedPipelineMode -join ';'
AppPool_StartMode = $AppPool.StartMode -join ';'
}
} Export-csv -Path "C:\Desktop\Scripts\IISite_App-pool_Details.csv" -Append
}
}
}
#Export-Excel -Append -Path C:\Desktop\Scripts\IISite_App-pool_Details.xlsx -AutoSize -BoldTopRow
I have created a script to get all the IIS Sites and App-pool details from multiple servers, the script is working fine as expected, but I am unable to export the results to CSV file.
It will helpful if someone suggest on how can I export the output details to CSV file.
CODE
#Import-Module -Name WebAdministration
$Computers = Get-Content "C:\Desktop\Scripts\Servers.txt"
foreach ($server in $Computers) {
$iis = Get-WmiObject Win32_Service -Filter "Name = 'IISADMIN'" -ComputerName $server
if ($iis.State -eq 'Running')
{
$details = Write-Host "IIS is running on $server" -ForegroundColor Green
#Chandu
Invoke-Command -ComputerName $Computers -ScriptBlock {
# Changed to newer IISAdministration Module to match Get-IISAppPool
$Websites = Get-IISSite
foreach ($Website in $Websites) {
$AppPool = Get-IISAppPool -Name $Website.Applications[0].ApplicationPoolName
#$Website.ApplicationPool
[PSCustomObject]@{
Server_Name = $env:COMPUTERNAME +'.'+ $env:USERDNSDOMAIN
Website_Name = $Website.Name
Website_Id = $Website.Id -join ';'
Website_State = $Website.State -join ';'
Website_PhysicalPath = $Website.PhysicalPath -join ';'
Website_Bindings = $Website.Bindings.Collection -join ';'
Website_Attributes = ($Website.Attributes | ForEach-Object { $_.name + "=" + $_.value }) -join ';'
AppPool_Name = $AppPool.Name -join';'
AppPool_State = $AppPool.State -join ';'
AppPool_ManagedRuntimeVersion = $AppPool.ManagedRuntimeVersion -join ';'
AppPool_ManagedPipelineMode = $AppPool.ManagedPipelineMode -join ';'
AppPool_StartMode = $AppPool.StartMode -join ';'
}
} Export-csv -Path "C:\Desktop\Scripts\IISite_App-pool_Details.csv" -Append
}
}
}
#Export-Excel -Append -Path C:\Desktop\Scripts\IISite_App-pool_Details.xlsx -AutoSize -BoldTopRow
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以将开关
-HideComputerName
添加到Invoke-Command
cal,但输出中仍然会添加属性“RunspaceId”和“PSShowComputerName”。如果您不想要这些,您可以从结果中取消选择它们,例如
P.S.您的代码不显示初始化变量
$Computers
的位置。You could add switch
-HideComputerName
to theInvoke-Command
cal, but then still the output will also have properties 'RunspaceId' and 'PSShowComputerName' added to it.If you don't want those, you can de-select them from the result like
P.S. Your code doesn't show where you initialized variable
$Computers
..