我有这个代码,它只适用于一个项目,如果我添加一个新项目,它只会删除旧项目
我尝试了很多方法但还是找不到如何做到这一点你能帮助我吗?
Import-Module WebAdministration
$HostName = $env:computername
$DateNow = Get-Date -format d
$WebServerEngine_MajorVersion = get-itemproperty HKLM:\SOFTWARE\Microsoft\InetStp\ | foreach >{$_.MajorVersion}
$WebServerEngine_MinorVersion = get-itemproperty HKLM:\SOFTWARE\Microsoft\InetStp\ | foreach {$_.MinorVersion}
$IIS_Version = [string]$WebServerEngine_MajorVersion + "." + [string]$WebServerEngine_MinorVersion
$Webs = Get-Website | select name
if ($Webs) {
ForEach ($Web in $Webs) {
$InfoServer = new-object psobject
$InfoServer | add-member noteproperty HostName $HostName
$InfoServer | add-member noteproperty Date_report $DateNow
$InfoServer | add-member noteproperty IIS_Version $IIS_Version
$InfoServer | add-member noteproperty SiteName $Web.name
}
}
答案1
您在循环的每次迭代中都会覆盖 $InfoServer 对象。尝试在循环末尾添加 Write-Output $InfoServer 命令。这会将数据发送回管道。尝试以下操作:
if ($Webs) {
ForEach ($Web in $Webs) {
$InfoServer = new-object psobject
$InfoServer | add-member noteproperty HostName $HostName
$InfoServer | add-member noteproperty Date_report $DateNow
$InfoServer | add-member noteproperty IIS_Version $IIS_Version$InfoServer | add-member noteproperty SiteName $Web.name
Write-Output $InfoServer
}
}