Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

PowerShell Script to Send Online and Off Line Computer Names to Ouput Files

$
0
0

This powershell script will send the logged on users of each machine to an output file. I would like to have the script send the online computer names to one ouput file and the off line computer names to another output file.

$CurrentDir = Split-Path -Path $MyInvocation.MyCommand.Definition -Parent
$Source = "$CurrentDir/hostsSCCM.txt"   
$PCS=(Get-Content $Source)
$PCS = $PCS | select -uniq
IF ($PCS.count -eq $NULL)
    {
        $PCS = @("$PCS")
    }
ELSE
    {
    } #This catches hosts.txt files that are only one line and forces them to be an array.
   
$SIZE=($PCS).count
$NUM= 0
$PATCHPC = @()
$STATUS = @()

    Write-host "  Machine            Status   Current User" -fore Cyan
    Write-host "+-----------------------------------------------------------" -fore DarkCyan

WHILE ($NUM -lt $SIZE)
 {
 Write-host "¦" -nonewline -fore DarkCyan; Write-host $PCS[$NUM] -nonewline -fore white; Write-host "    ¦"  -nonewline -fore DarkCyan       
       
        $pinger = $PCS[$NUM]
        $ping = get-wmiobject -Query "Select * from win32_pingstatus where Address='$pinger'"
        if ($ping.statuscode -eq 0)
        {
            Write-host "ONLINE!" -fore green -nonewline; Write-host "¦"  -nonewline -fore DarkCyan
            $hostcatch = (get-wmiobject -ErrorVariable Err -Erroraction SilentlyContinue -computerName $PCS[$NUM] -class win32_computerSystem).username
            $STATUS += $hostcatch
            if ($Err -ne $null)
                {
                    write-host " WMI ERROR ON TARGET" -fore DARKRED -nonewline
                    $PATCHPC += $PCS[$NUM]
                    $STATUS += "ONLINE! WMI ERROR ON TARGET"
                }
            elseif ($hostcatch -eq $null)
                {
                    write-host " ACCESSIBLE - FIX CLIENT!" -fore YELLOW -nonewline
                    $PATCHPC += $PCS[$NUM]
                    $STATUS += "ONLINE! ACCESSIBLE - FIX CLIENT!"
                }
                
            Write-host "" $hostcatch -fore white;
        }
        #else
        #{        
        #    not Write-host
        #}
        else
        {        
            Write-host "OFFLINE" -fore red -nonewline; Write-host "¦" -fore DarkCyan
            $PATCHPC += $PCS[$NUM]
            $STATUS += "OFFLINE"
        }
        
    Write-host "+-------------------+-------+-------------------------------" -fore DarkCyan
    $NUM+=1
 }

    Write-host "+-----------------------------------------------------------" -fore DarkCyan
    Write-host "`n"   

$title = "Export Names"
$message = "Do you want to export the names of machines that may be patched to output.txt?"

$yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes", `
    "Exports each patchable computer to a single txt file."

$no = New-Object System.Management.Automation.Host.ChoiceDescription "&No", `
    "Does not export anything."

$options = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no)

$result = $host.ui.PromptForChoice($title, $message, $options, 0)

switch ($result)
    {
        0 {"The file has been exported.`n";$STATUS |Add-Content output.txt}
        1 {"`n"}
    }

Read-host "Press Enter to continue"

[console]::ResetColor()


Viewing all articles
Browse latest Browse all 6937

Trending Articles