Last active
January 18, 2018 21:05
-
-
Save murrahjm/9fafcd927af3ff7c3bf8a321ddf2d7e8 to your computer and use it in GitHub Desktop.
Prequel 1 - Flawless Faction
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Function Get-ComputerInfo { | |
Param ( | |
[Parameter(Position=1, ValueFromPipelineByPropertyName=$True, ValueFromPipeline=$True)] | |
[alias('Computer','Host','Hostname','Server','ServerName')] | |
[String[]]$ComputerName = $env:computername, | |
[pscredential]$Credential = $Null | |
) | |
BEGIN{ | |
$ErrorActionPreference = 'Stop' | |
$FunctionName = $pscmdlet.MyInvocation.InvocationName.ToUpper() | |
write-verbose "$FunctionName`: Begin Function" | |
} | |
PROCESS { | |
Foreach ($Computer in $Computername){ | |
Write-Verbose "$FunctionName`: Attempting data retrieval via CIM for $computer" | |
Try { | |
If ($Credential){ | |
$Monitorinfo = Start-job -scriptblock {Get-CIMInstance -Namespace root\wmi -Class wmiMonitorID -ComputerName $args[0]} -argumentlist $computer -Credential $Credential | wait-job | receive-job | |
$Computerinfo = start-job -scriptblock {Get-CIMInstance -Class Win32_ComputerSystem -ComputerName $args[0]} -argumentlist $computer -Credential $Credential | wait-job | receive-job | |
$BIOSinfo = start-job -scriptblock {Get-CIMInstance -Class Win32_BIOS -ComputerName $args[0]} -argumentlist $computer -Credential $Credential | wait-job | receive-job | |
} else { | |
$Monitorinfo = Get-CIMInstance -Namespace root\wmi -Class wmiMonitorID -ComputerName $computer | |
$Computerinfo = Get-CIMInstance -class Win32_ComputerSystem -ComputerName $computer | |
$BIOSinfo = Get-CIMInstance -class Win32_BIOS -ComputerName $computer | |
} | |
} Catch { | |
Write-verbose "$FunctionName`: Unable to retrieve data via WMI method. Attempting WSMan connection." | |
write-verbose "$FunctionName`: Error message $_" | |
} | |
If (($null -eq $MonitorInfo -or $null -eq $computerinfo -or $Null -eq $BIOSinfo) -and ($computer -ne $env:computername)){ | |
Try { | |
If ($Credential){ | |
$Session = New-PSSession -ComputerName $Computer -Credential $Credential | |
} else { | |
$Session = New-PSSession -ComputerName $Computer | |
} | |
$Computerinfo = Invoke-command -ScriptBlock {Get-CIMInstance -class Win32_ComputerSystem} -Session $Session | |
$BIOSinfo = Invoke-command -ScriptBlock {Get-CIMInstance -class Win32_BIOS} -Session $Session | |
$Monitorinfo = Invoke-command -ScriptBlock {Get-CIMInstance -namespace root\wmi -class wmiMonitorID} -Session $Session -ErrorAction SilentlyContinue | |
Remove-PSSession $Session | |
} Catch { | |
Remove-PSSession $Session | |
write-error "Unable to connect to $Computer. Error code`: $_" | |
return | |
} | |
} | |
$output = New-object psobject -property @{ | |
'ComputerName' = $Computer | |
'MonitorInfo' = '' | |
'ComputerType' = '' | |
'ComputerSerial' = '' | |
} | |
If ($MonitorInfo){ | |
$monitors=@() | |
foreach ($monitor in $monitorinfo){ | |
$monitors += new-object psobject -property @{ | |
'MonitorSerial' = [System.Text.Encoding]::Default.GetString($Monitor.SerialNumberID) | |
'MonitorType' = [System.Text.Encoding]::Default.GetString($monitor.UserFriendlyName) | |
} | |
$output.MonitorInfo = $monitors | |
} | |
} | |
If ($ComputerInfo){ | |
$output.ComputerType = $computerinfo.model | |
} | |
If ($Biosinfo){ | |
$output.ComputerSerial = $BIOSinfo.serialnumber | |
} | |
return $output | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$here = Split-Path -Parent $MyInvocation.MyCommand.Path | |
$sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path) -replace '\.Tests\.', '.' | |
. "$here\$sut" | |
Describe -Name "Unit Testing Get-ComputerInfo" -Fixture { | |
Context -Name "Mock Functions" -Fixture { | |
Mock -CommandName Get-CimInstance -MockWith { | |
[pscustomobject]@{ | |
name = $ComputerName | |
model = 'PHILIPS' | |
} | |
} -ParameterFilter { $ClassName -and $ClassName -eq "Win32_ComputerSystem"} #Get-CimInstance Mock | |
Mock -CommandName Get-CimInstance -MockWith { | |
[pscustomobject]@{ | |
SerialNumber = '1234568' | |
} | |
} -ParameterFilter { $ClassName -and $ClassName -eq "Win32_BIOS"} #Get-CimInstance Mock | |
Mock -CommandName Get-CimInstance -MockWith { | |
[pscustomobject]@{ | |
UserFriendlyName = @('109','111','99','107','101','100') | |
SerialNumberID = @('56','54','55','53','51','48','57') | |
}#PSCustomObject | |
} -ParameterFilter { $ClassName -and $ClassName -eq "wmiMonitorID"} #Get-CimInstance Mock#Mock Get-Ciminstance | |
$Result = Get-ComputerInfo | |
It -name "Assert Mock called - Get-CimInstance" -test { | |
Assert-MockCalled -CommandName Get-CimInstance -Times 1 -ParameterFilter { $ClassName -and $ClassName -eq "Win32_ComputerSystem"} | |
Assert-MockCalled -CommandName Get-CimInstance -Times 2 | |
}#It | |
It -name "Result is not null" -test{ | |
$Result | Should not beNullOrEmpty | |
}#It | |
It -name "MonitorType is mocked" -test{ | |
$Result.monitorinfo.MonitorType | Should be "Mocked" | |
}#It | |
It -name "ComputerName matches env:ComputerName" -test{ | |
$Result.ComputerName | Should be $env:ComputerName | |
}#It | |
It -name "ComputerSerial is mocked" -test { | |
$Result.ComputerSerial | Should be '1234568' | |
}#It | |
It -name "MonitorSerial is mocked" -test { | |
$Result.monitorinfo.MonitorSerial | Should be '8675309' | |
}#It | |
It -name "Processes computername parameter from pipeline input" -test { | |
$result = 'computer1','computer2' | get-computerinfo | |
$result[0].Computername | should be 'computer1' | |
$result[1].Computername | should be 'computer2' | |
} | |
It -name "Processes computername parameter as property of input object" -test { | |
$inputobject = [pscustomobject]@{Computername='computer1'} | |
$Result = $inputobject | get-computerinfo | |
$result.Computername | should be 'computer1' | |
} | |
It -name "Processes multiple computernames as properties of input objects with alias" -test { | |
$inputarray = @() | |
$inputarray += [pscustomobject]@{host='computer1'} | |
$inputarray += [pscustomobject]@{host='computer2'} | |
$Result = $inputarray | get-computerinfo | |
$result[0].Computername | should be 'computer1' | |
$Result[1].Computername | should be 'computer2' | |
} | |
}#Context Mock Functions | |
Context -Name "No Mocked Functions" -Fixture { | |
$Result = Get-ComputerInfo | |
It -name "Result is not null" -test{ | |
$Result | Should not beNullOrEmpty | |
}#It | |
}#Context no mocked functions | |
}#Describe |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment