Sunday, June 23, 2019

Report--localComputer.ps1

#Requires -Version 2
#1. Information & Troubleshooting\1. Command Line\Automation via Batch Files - PowerShell\Dell EMC\PS Script - All in One\Ping\Report from Local Computer\Report--localComputer.ps1


[CmdletBinding(ConfirmImpact='Low')]
Param([Parameter(Position=0,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]
    [String[]]$ComputerName = $env:COMPUTERNAME)

$PCData = foreach ($PC in $ComputerName) {
    Write-Verbose "Checking computer'$PC'"
    try {
        Test-Connection -ComputerName $PC -Count 2 -ErrorAction Stop | Out-Null
        $OS    = Get-WmiObject -ComputerName $PC -Class Win32_OperatingSystem -EA 0
        $Mfg   = Get-WmiObject -ComputerName $PC -Class Win32_ComputerSystem -EA 0
        $IPs   = @()
        $MACs  = @()
        foreach ($IPAddress in ((Get-WmiObject -ComputerName $PC -Class "Win32_NetworkAdapterConfiguration" -EA 0 |
            Where { $_.IpEnabled -Match "True" }).IPAddress | where { $_ -match "\." })) {
                $IPs  += $IPAddress
                $MACs += (Get-WmiObject -ComputerName $PC -Class "Win32_NetworkAdapterConfiguration" -EA 0 |
                    Where { $_.IPAddress -eq $IPAddress }).MACAddress
        }
        $Props = @{
            ComputerName   = $PC
            Status         = 'Online'
            IPAddress      = $IPs -join ', '
            MACAddress     = $MACs -join ', '
            DateBuilt      = ([WMI]'').ConvertToDateTime($OS.InstallDate)
            OSVersion      = $OS.Version
            OSCaption      = $OS.Caption
            OSArchitecture = $OS.OSArchitecture
            Model          = $Mfg.model
            Manufacturer   = $Mfg.Manufacturer
            VM             = $(if ($Mfg.Manufacturer -match 'vmware' -or $Mfg.Manufacturer -match 'microsoft') { $true } else { $false })
            LastBootTime   = ([WMI]'').ConvertToDateTime($OS.LastBootUpTime)
        }
        New-Object -TypeName PSObject -Property $Props
    } catch { # either ping failed or access denied
        try {
            Test-Connection -ComputerName $PC -Count 2 -ErrorAction Stop | Out-Null
            $Props = @{
                ComputerName   = $PC
                Status         = $(if ($Error[0].Exception -match 'Access is denied') { 'Access is denied' } else { $Error[0].Exception })
                IPAddress      = ''
                MACAddress     = ''
                DateBuilt      = ''
                OSVersion      = ''
                OSCaption      = ''
                OSArchitecture = ''
                Model          = ''
                Manufacturer   = ''
                VM             = ''
                LastBootTime   = ''
            }
            New-Object -TypeName PSObject -Property $Props         
        } catch {
            $Props = @{
                ComputerName   = $PC
                Status         = 'No response to ping'
                IPAddress      = ''
                MACAddress     = ''
                DateBuilt      = ''
                OSVersion      = ''
                OSCaption      = ''
                OSArchitecture = ''
                Model          = ''
                Manufacturer   = ''
                VM             = ''
                LastBootTime   = ''
            }
            New-Object -TypeName PSObject -Property $Props           
        }
    }
}
$PCData | sort ComputerName |
    select ComputerName, Status, OSVersion, OSCaption, OSArchitecture, IPAddress, MacAddress, VM, Model, Manufacturer, DateBuilt, LastBootTime


Ping.ps1




1. Information & Troubleshooting\1. Command Line\Automation via Batch Files - PowerShell\Dell EMC\PS Script - All in One\Ping\Only Ping & to Get IPAddress\ping.ps1



$scriptPath = split-path -parent $MyInvocation.MyCommand.Definition
$servers = Get-Content "$scriptPath\Servers.txt"

#$UserName = Read-Host "Enter User Name:"
#$Password = Read-Host -AsSecureString "Enter Your Password:"
#$Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $UserName, $Password


$infoColl = @()
foreach ($S in $Servers)

{

       $CPUInfo = if (test-Connection -ComputerName $S -Count 1 -Quiet ) {"Pinging "} else {"Not pinging"} 
       $IPs = if (Test-Connection $S -count 1 -Quiet) {Test-Connection $S -count 1}
     


Foreach ($CPU in $CPUInfo)
{
$infoObject = New-Object PSObject
#The following add data to the infoObjects.
Add-Member -inputObject $infoObject -memberType NoteProperty -name "ServerName" -value $S
Add-Member -inputObject $infoObject -memberType NoteProperty -name "Ping Status" -value $CPUInfo
Add-Member -inputObject $infoObject -memberType NoteProperty -name "IP Address" -value $IPs.IPV4Address



        $infoObject #Output to the screen for a visual feedback.
$infoColl += $infoObject
}
}
$infoColl | Export-Csv -path "$scriptPath\Server_Inventory_$((Get-Date).ToString('MM-dd-yyyy')).csv" -NoTypeInformation #Export the results in csv file.