2
I would like some help with my code, it doesn't return the OS architecture correctly
Post Body
I'll start off by saying that my code is ugly, but mostly works(Except it gets really pissed off if a local user is logged in vs domain user). But for some reason when I run the command
(Get-WmiObject Win32_OperatingSystem -cn <computername).OSArchitecture
It returns correctly. But in my code it's forever stuck on 32bit. What am I doing wrong?
#Written by me
#My friend wrote a script similar to this in vbscript
#But in the off chance a company I worked for didn't allow it's use, I wanted something to replace it that was as versatile
# Parameters
param
(
[string]$strComp
)
#Clear the screen
cls
# Check parameters input
if( $strComp -eq "" )
{
$res = "Missing parameters - Usage: .\CheckComputer.ps1 hostname"
echo $res
exit
}
#Load AD module
import-module activedirectory
#Checks if computer is 32-Bit or 64-Bit
$Arch = (Get-WmiObject Win32_OperatingSystem -computername $strComp).OSArchitecture
#The below function was borrowed from http://stackoverflow.com/questions/2688547/muliple-foreground-colors-in-powershell-in-one-command
#This helps set the color of the text
function Write-Color([String[]]$Text, [ConsoleColor[]]$Color = "White", [int]$StartTab = 0, [int] $LinesBefore = 0,[int] $LinesAfter = 0) {
$DefaultColor = $Color[0]
if ($LinesBefore -ne 0) { for ($i = 0; $i -lt $LinesBefore; $i ) { Write-Host "`n" -NoNewline } } # Add empty line before
if ($StartTab -ne 0) { for ($i = 0; $i -lt $StartTab; $i ) { Write-Host "`t" -NoNewLine } } # Add TABS before text
if ($Color.Count -ge $Text.Count) {
for ($i = 0; $i -lt $Text.Length; $i ) { Write-Host $Text[$i] -ForegroundColor $Color[$i] -NoNewLine }
} else {
for ($i = 0; $i -lt $Color.Length ; $i ) { Write-Host $Text[$i] -ForegroundColor $Color[$i] -NoNewLine }
for ($i = $Color.Length; $i -lt $Text.Length; $i ) { Write-Host $Text[$i] -ForegroundColor $DefaultColor -NoNewLine }
}
Write-Host
if ($LinesAfter -ne 0) { for ($i = 0; $i -lt $LinesAfter; $i ) { Write-Host "`n" } } # Add empty line after
}
#Function for days since password change borrowed from http://www.workingsysadmin.com/powershell-function-to-get-time-since-a-users-password-was-last-changed/
function Get-TimeSinceLastPWSet {
[CmdletBinding()]
param (
[Parameter(Mandatory=$True,
Position=1,
ValueFromPipeline=$True)]
[string]$Username
)
$tsSinceLastPWSet = New-TimeSpan $(get-aduser $Username -Properties Passwordlastset).Passwordlastset $(get-date)
$strFormatted = '{0:dd} days, {0:hh} hours' -f $tsSinceLastPWSet
return $strFormatted
}
#This is to check if Imprivata exists
If ($Arch = "32-Bit") {
if((Test-Path -Path "\\$strComp\C$\Program files\Imprivata\OneSign Agent\ISXAgent.exe" -pathtype Container) -eq $True)
{
Set-Variable -Name Imprivata -Value True
}
Else {
Set-Variable -Name Imprivata -Value False
}
}
elseif ($Arch = "64-Bit") {
if((Test-Path -Path "\\$strComp\C$\Program files (x86)\Imprivata\OneSign Agent\ISXAgent.exe" -pathtype Container) -eq $True)
{
Set-Variable -Name Imprivata -Value True
}
Else {
Set-Variable -Name Imprivata -Value False
}
}
$ModelInfo = (Get-WmiObject -computername $strComp Win32_Computersystem -ErrorAction silentlycontinue).model
$SerNum = (Get-WmiObject -computername $strComp -Class Win32_BIOS -ErrorAction silentlycontinue).SerialNumber
$LoggedOnUser = (Get-WMIObject -computername $strComp -class Win32_ComputerSystem).username
#Split
$Split = $LoggedOnUser.Split('\')
# Get User
$User = $Split[1]
$LoggedOnName = (Get-ADuser -Identity $User).Name
$DaysSinceChange = Get-TimeSinceLastPWSet $User
#This function helps me get Password expiration
#function Get-PasswordExpirationDays ($User)
#{
# (([datetime]::FromFileTime((Get-ADUser –Identity $User -Properties "msDS-UserPasswordExpiryTimeComputed")."msDS-UserPasswordExpiryTimeComputed"))-(Get-Date)).Days
#}
$Booted = { (Get-WmiObject -computername $strComp Win32_OperatingSystem).LastBootUpTime
[Management.ManagementDateTimeConverter]::ToDateTime($Booted) }
$wmi = Get-WmiObject -ComputerName $strComp -Query "SELECT LastBootUpTime FROM Win32_OperatingSystem"
$now = Get-Date
$boottime = $wmi.ConvertToDateTime($wmi.LastBootUpTime)
$uptime = $now - $boottime
$d =$uptime.days
$h =$uptime.hours
$m =$uptime.Minutes
$s = $uptime.Seconds
$OS = (Get-WmiObject -ComputerName $strComp Win32_OperatingSystem).Caption
$disk = Get-WmiObject Win32_LogicalDisk -ComputerName $strComp -Filter "DeviceID='C:'" | Select-Object Size, FreeSpace
$RAM = [Math]::Round((Get-WmiObject -Class win32_computersystem -ComputerName $strComp).TotalPhysicalMemory/1Gb)
$Insdate = (gcim Win32_OperatingSystem).InstallDate
$LockedOut = (Get-Aduser $User -Properties LockedOut).LockedOut
$PasswdLastSet = (Get-ADUser -Identity $User -Properties PasswordLastSet).PasswordLastSet
$Department = Get-ADUser -Identity $User -Properties Department | Select-Object -expandproperty Department
$HomeDir = Get-ADuser -Identity $User -Properties homeDirectory | Select-Object -expandproperty homeDirectory
$PhoneNum = Get-ADuser -Identity $User -Properties officephone | Select-Object -expandproperty officephone
#Lets test if the computer is on the network before we throw lots of commands at it
if(Test-Connection -cn $strComp -Quiet -BufferSize 16 -Count 1)
{
Write-Color "#########################################" -Color Red
Write-Color "Information for $strComp" -Color White
Write-Color "#########################################" -Color Red
Write-Color "Operating System: $OS $Arch" -Color White
Write-Color "Computer Model: $ModelInfo" -Color White
Write-Color "Computer Serial Number: $SerNum" -Color White
Write-Color "Computer uptime: Days: $d Hours: $h Minutes: $m Seconds: $s" -Color White
Write-Color "Computer OS was installed on $InsDate" -Color White
Write-Color "Computer Network information: $Info" -Color White
Write-Color "#########################################" -Color Red
Write-Color "Currently logged in name: $LoggedOnName" -Color White
Write-Color "Currently logged on user: $User" -Color White
Write-Color "Current User locked: $LockedOut" -Color White
Write-Color "Current User Password last set : $DaysSinceChange ago" -Color White
Write-Color "Current User Department: $Department" -Color White
Write-Color "Current User Home Directory: $HomeDir" -Color White
Write-Color "Current User Phone Number: $PhoneNum" -Color White
Write-Color "#########################################" -Color Red
Write-Color "Hard drive information for C:" -Color White
Write-Color ("{0}GB total" -f [math]::truncate($disk.Size / 1GB)) -Color White
Write-Color ("{0}GB free" -f [math]::truncate($disk.FreeSpace / 1GB)) -Color White
Write-Color "#########################################" -Color Red
Write-Color "RAM information for $strComp" -Color White
Write-Color "Installed RAM: $RAM GB" -Color White
Write-Color "#########################################" -Color Red
Write-Color "Has Imprivata: $Imprivata"
}
Else
{
write-host $strComp is offline
}
Author
Account Strength
100%
Account Age
12 years
Verified Email
Yes
Verified Flair
Yes
Total Karma
66,614
Link Karma
30,920
Comment Karma
35,639
Profile updated: 6 days ago
Posts updated: 9 months ago
Subreddit
Post Details
We try to extract some basic information from the post title. This is not
always successful or accurate, please use your best judgement and compare
these values to the post title and body for confirmation.
- Posted
- 8 years ago
- Reddit URL
- View post on reddit.com
- External URL
- reddit.com/r/PowerShell/...