【问题标题】:Powershell IIS Audit ScriptingPowershell IIS 审计脚本
【发布时间】:2020-09-16 06:21:04
【问题描述】:

我正处于学习 powershell 的早期阶段,我正在尝试编写一个从我们的 IIS 服务器远程收集信息的脚本,但我遇到了几个问题。

第一个是 IP 地址和 OU 列在输出文件中保持为空。 第二个是我无法将管理员组列格式化为每行 1 个组,或用逗号分隔。

这是当前版本的代码:

$computers = Get-Content "C:\servers.txt"


#Running the invoke-command on remote machine to run the iisreset


$output = foreach ($computer in $computers) 
{
       Write-Host "Details from server $computer..."

    try{
           Invoke-command  -ComputerName $computer -ErrorAction Stop -ScriptBlock{

           # Ensure to import the WebAdministration and AD module

                Import-Module WebAdministration
                Import-Module ActiveDirectory

                $webapps = Get-WebApplication
                $list = @()
                foreach ($webapp in get-childitem IIS:\AppPools\)
                {
                    $name = "IIS:\AppPools\" + $webapp.name
                    $item = @{}
                    $item.server = $env:computername
                    $item.WebAppName = $webapp.name
                    $item.Version = (Get-ItemProperty $name managedRuntimeVersion).Value
                    $item.State = (Get-WebAppPoolState -Name $webapp.name).Value
                    $item.UserIdentityType = $webapp.processModel.identityType
                    $item.Username = $webapp.processModel.userName
                    $item.Password = $webapp.processModel.password
                    $item.OU = (Get-ADComputer $_ | select DistinguishedNAme)
                    $item.IPAddress = (Get-NetIPAddress -AddressFamily IPv4)
                    $item.Administrators = (Get-LocalGroupMember -Group "Administrators")



                    $obj = New-Object PSObject -Property $item
                    $list += $obj
                }



                $list | select -Property "Server","WebAppName", "Version", "State", "UserIdentityType", "Username", "Password", "OU", "Ip Address", "Administrators"

        }

    } catch {
        Add-Content .\failedservers.txt -Force -Value $computer
    }
} 

$output | Export-Csv -Path .\output.csv
#Stop-Transcript

对于如何使其正常工作或改进代码本身的任何意见,我将不胜感激。 提前致谢!

【问题讨论】:

    标签: powershell iis scripting audit


    【解决方案1】:

    对于OU 属性,您需要引用$env:COMPUTERNAME 而不是$_

    $item.OU = (Get-ADComputer $env:COMPUTERNAME |Select -Expand DistinguishedName)
    

    对于IPAddressAdministrators 字段,您需要使用-join 来创建相关值的逗号分隔列表:

    $item.IPAddress = (Get-NetIPAddress -AddressFamily IPv4).IPAddress -join ','
    $item.Administrators = (Get-LocalGroupMember -Group "Administrators").Name -join ','
    

    【讨论】:

    • 感谢您的回复和帮助。我已经尝试了您的解决方案,它部分有效。我可以用逗号分隔管理员组的成员,但 CSV 文件中的 IP 和 OU 字段仍然为空。
    • @DiegoS。更新了第一个示例(使用 -Expand 仅获取属性值)您可能希望在远程脚本块中使用 Import-Module NetTCPIP 以使 Get-NetIPAddress 工作
    • 我已经尝试过您的更新,但仍然无法完成 IP 和 OU 的字段。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多