powershell 使用PS 5.1列表从远程AD系统捕获信息的脚本

eiee3dmh  于 2022-11-10  发布在  Shell
关注(0)|答案(2)|浏览(159)

我正在尝试建立一个脚本,它将请求信息(主机名、MAC、IP、标题(操作系统版本)和序列号),使用从AD拉出的计算机列表。
这是可行的,但它会创建多行/多行,而我需要在一行中包含所有这些信息。是的,我是个新手..我可以为一台机器编写一个脚本,但让相同的脚本与列表一起工作却是遥不可及的,这个脚本允许我获取信息,但不在同一行。
我使用的是PW版本5.1
这就是了;

Function Get-CInfo {

    $ComputerName = Get-Content C:\Users\scott.hoffman.w.tsc\Desktop\scripts\get-cinfo-tools\comp-list.txt

    $ErrorActionPreference = 'Stop'

    foreach ($Computer in $ComputerName) {

        Try {

            gwmi -class "Win32_NetworkAdapterConfiguration" -cn $Computer | ? IpEnabled -EQ "True" | select DNSHostName, MACAddress, IPaddress | FT -AutoSize 
            gwmi win32_operatingsystem -cn $computer | select Caption | FT -Autosize
            Get-WmiObject win32_bios -cn $computer | select Serialnumber | FT -Autosize

        }

        Catch {

            Write-Warning "Can't Touch This : $Computer"

        }

    }#End of Loop

}#End of the Function

Get-CInfo > comp-details.txt

Comp-list.txt文件只是;
计算机名01计算机名02
我很想从输入到输出使用CSV,但我迷路了。
谢谢你的帮助/投入/开怀大笑!

3htmauhk

3htmauhk1#

帮自己一个大忙,学习如何创建自定义对象:


# Function is more useful if you remove specific filepaths from inside it

# Using a parameter and set it to accept pipeline input

Function Get-CInfo {
    [CmdletBinding()]
    Param (
        [parameter(Mandatory = $true,ValueFromPipeline = $true)]$ComputerList
    )
    Begin {
        $ErrorActionPreference = 'Stop'
        Write-Host "Processing:"
    }
    Process {
        foreach ($Computer in $ComputerList) {
            Try {
                Write-Host $Computer
        # Gather data
                $NetAdapter = gwmi -class "Win32_NetworkAdapterConfiguration" -cn $Computer | ? IpEnabled -EQ "True" | select DNSHostName, MACAddress, IPaddress
                $OStype = gwmi win32_operatingsystem -cn $computer | select Caption
                $Serial = Get-WmiObject win32_bios -cn $computer | select Serialnumber
        # Output custom object with required properties
                [pscustomobject]@{
                    Computer = $Computer
                    DNSHostName = $NetAdapter.DNSHostName;
                    MACAddress = $NetAdapter.MACAddress;
                    IPAddress = $NetAdapter.IPAddress;
                    OperatingSystem = $OSType.Caption;
                    Serial = $Serial.Serialnumber;
                    Error = ''
                }

            }
            Catch {
            # Within the catch section $_ always contains the error.
                 [pscustomobject]@{
                    Computer = $Computer
                    DNSHostName = '';
                    MACAddress = '';
                    IPAddress = '';
                    OperatingSystem = '';
                    Serial = '';
                    Error = $_.Exception.Message
                }
            }
        }#End of Loop
    }
    End {
        Write-Host "Done"
    }
}#End of the Function

# Pipe list to function and store to '$Results'

$Results = Get-Content C:\Users\scott.hoffman.w.tsc\Desktop\scripts\get-cinfo-tools\comp-list.txt | Get-CInfo

# Output and formatting should almost always be the last thing you do

# Now that you have an object ($Results) with your data, you can use it however you like

# Format and output to text file

$Results | ft -AutoSize > comp-details.txt

# Or send to csv

$Results | Export-Csv -Path comp-details.csv -NoTypeInformation
gr8qqesn

gr8qqesn2#

感谢#节奏家!
下面是我用来将文本文件逐行读入变量的PS脚本:
文本

ComputerName01
    ComputerName02

脚本

function Get-TimeStamp {return "[{0:HH:mm:ss}]" -f (Get-Date)}

$StartTime = Get-Date -Format 'yyyy/MM/dd    HH:mm:ss'

# Using a parameter and set it to accept pipeline input

Function Get-CInfo {
[CmdletBinding()]
Param (
    [parameter(Mandatory = $true, ValueFromPipeline = $true)]$ComputerList
)
Begin {
    $ErrorActionPreference = 'Stop'

        Write-Host ""
    Write-Host "Processing now: $StartTime"
}
Process {
    foreach ($Computer in $ComputerList) {
        Try {
            Write-Host "$(Get-TimeStamp) Working on machine: $Computer"
    # Gather data
            $NetAdapter = gwmi -class "Win32_NetworkAdapterConfiguration" -cn $Computer | ? IpEnabled -EQ "True" | select MACAddress, IPaddress
            $OStype = gwmi win32_operatingsystem -cn $computer | select Caption
            $Serial = Get-WmiObject win32_bios -cn $computer | select Serialnumber
    # Output custom object with required properties
            [pscustomobject]@{
                Computer = $Computer
                #DNSHostName = $NetAdapter.DNSHostName;
                MACAddress = $NetAdapter.MACAddress;

                # Here is the line that I added [0] to the end

                IPAddress = $NetAdapter.IPAddress[0];
                OperatingSystem = $OSType.Caption;
                Serial = $Serial.Serialnumber;
                Error = ''
            }

        }
        Catch {
        # Within the catch section $_ always contains the error.
             [pscustomobject]@{
                Computer = $Computer
                #DNSHostName = '';
                MACAddress = '';
                IPAddress = '';
                OperatingSystem = '';
                Serial = '';
                Error = $_.Exception.Message
            }
        }
    }#End of Loop
}
End {
    Write-Host ""
Write-Host "*****"
Write-Host ""
Write-Host "Done"
Write-Host ""
}
}#End of the Function

# Pipe list to function and store to '$Results'

$Results = Get-Content .\comp-list.txt | Get-CInfo

# Output and formatting

# Format and output to text file

$Results | ft -AutoSize > comp-details.txt

# Or send to csv

$Results | Export-Csv -Path comp-details.csv -NoTypeInformation

# Output results to console

Get-Content -Path .\comp-details.csv

以下是CSV输出(已编辑):

"Computer","MACAddress","IPAddress","OperatingSystem","Serial","Error"
"ComputerName001","xx:xx:xx:xx:xx:xx","123.456.789.000","Microsoft Windows 11 Enterprise","JJJJJJJ",""

相关问题