###Beginning of script
param([SWITCH]$help, [STRING]$ouDN, [STRING]$file)
###################################################### Functions
#####################################################
Function get_help()
{
$helpText=@"
"@
write-host $helpText -ForegroundColor Yellow
exit
}
###################################################### Validate parameters
and combinations
#####################################################
if ($help -or $param.count -eq 0) {get_help}
###################################################### Functions
###################################################### Function to verify
the passed OU exists
#####################################################
function verify_OU
{
$ErrorActionPreference = "SilentlyContinue"
$ouExist = [ADSI]::Exists("LDAP://" + $ouDN)
while ($ouExist -ne $TRUE)
{
Write-Warning "Specified OU does not exist: $ouPath"
$ouDN = Read-Host "Retype target OU distinguishedName path"
$ouExist = [ADSI]::Exists("LDAP://" + $ouDN)
set-variable -name ouDN -value "$ouDN" -scope 1
}
}
###################################################### Function to collect
computer object information from AD
#####################################################
function get_computerObjectInfo
{
#set variable values to default of NULL
$computerDnsHostName = "NULL"
$computerOS = "NULL"
$computerLLTS = "NULL"
#collect computer dnsHostName
if ($computer.dnshostname -ne $null)
{
$computerDnsHostName = $computer.dnshostname
}
#collect computer operatingSystem
if ($computer.operatingsystem -ne $null)
{
$computerOS = $computer.operatingsystem
}
#collect computer lastLogonTimeStamp
if ($computer.lastLogonTimeStamp -ne $null)
{
$computerLLTS = Get-Date -Date
([DateTime]::FromFileTime([Int64]::Parse($computer.lastLogonTimeStamp)))`
-Format MM/dd/yyyy
}
$SCRIPT:adResults =
"$computerName,$computerDnsHostName,$computerOS,$computerLLTS"
}
###################################################### Function to ping
computer
#####################################################
function ping_computer
{
PROCESS
{
trap { continue }
$pingStatus = $ping.send($computerName).status
if ($pingStatus -eq "Success")
{
$pingResponse = ("Online")
}
else
{
$pingResponse = ("Offline")
}
}
END
{
$SCRIPT:pingResults = $pingResponse
}
}
###################################################### Verifty script
execution pre-requisits
#####################################################
verify_ou
Import-Module activedirectory
###################################################### Create .csv files
#####################################################$csvPath = Get-Location
$csvFile = $csvPath.Path + "\computerInfo.csv"
New-Item $csvFile -type file -Force
Set-Content $csvFile
"name,dnsHostName,operatingSystem,lastLogonTimeStamp,pingStatus"
#Clear-Host
#####################################################
# Main
#####################################################
$ping = new-object System.Net.NetworkInformation.Ping
$computers = get-adcomputer -filter * -searchBase $ouDN -resultpagesize 1000
-searchScope subtree -Properties
lastLogonTimeStamp,name,operatingsystem,dnshostname
foreach ($computer in $computers)
{
$computerName = $computer.name
get_computerObjectInfo
ping_computer
"$adResults,$pingResults"
Add-Content $csvFile "$adResults,$pingResults"
}
$colComputers = @()
$colComputerInput = Import-Csv -Path $csvFile
foreach ($objComputer in $colComputerInput)
{
$objComputerInfo = New-Object System.Object
$objComputerInfo | Add-Member -MemberType NoteProperty -name
computername -value $objComputer.name
$objComputerInfo | Add-Member -MemberType NoteProperty -name dnsHostName
-value $objComputer.dnsHostName
$objComputerInfo | Add-Member -MemberType NoteProperty -name
operatingSystem -value $objComputer.operatingSystem
$objComputerInfo | Add-Member -MemberType NoteProperty -name
lastLogonTimeStamp -value $objComputer.lastLogonTimeStamp
$objComputerInfo | Add-Member -MemberType NoteProperty -name pingStatus
-value $objComputer.pingStatus
$colComputers += $objComputerInfo
}
$colComputers | ft ###<---This is causing the below error
###End of script
##########
out-linoutput : The object of type
"Microsoft.PowerShell.Commands.Internal.Format.FormatStartData" is not valid
or not in the correct sequence. This is likely caused by a user-specified
"format-table" command which is conflicting with the default formatting.
+ CategoryInfo : InvalidData: (:) [out-lineoutput], InvalidOperationException
+ FullyQualifiedErrorId :
ConsoleLineOutputOutOfSequencePacket,Microsoft.PowerShell.Commands.OutLineOutputCommand
##########
Help...why? and how can easily resolve. Thanks.
Paul
Is it possible I have a streaming or "file-state" conflict where the file
must be explicitly closed prior to reading the contents back in? If so, how
do I explicitly close? Thanks!
Paul
__________