Created
September 7, 2016 20:46
-
-
Save rufflabs/e4b7eac537335571ec1e9a800b4c7042 to your computer and use it in GitHub Desktop.
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
Import-Module ActiveDirectory | |
Import-Module DhcpServer | |
Import-Module PSReadLine | |
Add-PsSnapin VMware.VimAutomation.Core | |
Add-PSSnapin VeeamPSSnapIn | |
## | |
# Authenticode Signing | |
## | |
# Location of signed history file | |
$SignedHistoryXML = "$($env:temp)\signedHistory.xml" | |
function Add-SignedHistory | |
{ | |
param( | |
[Parameter(Mandatory=$True)]$File | |
) | |
if((Get-SignedHistory) -eq $Null) { | |
$History = @() | |
} else { | |
$History = Get-SignedHistory | |
} | |
if($History.Count -ge 10) { | |
# Pop the top item, and add the newest to the list | |
$null, $History = $History | |
$History += $File | |
} else { | |
$History += $File | |
} | |
# Save the current history | |
Export-Clixml -Path $SignedHistoryXML -InputObject $History | |
} | |
function Get-SignedHistory | |
{ | |
if(Test-Path $SignedHistoryXML) { | |
Import-Clixml $SignedHistoryXML | |
} else { | |
$Null | |
} | |
} | |
function Sign-Script($File) | |
{ | |
$codeCert = (Get-ChildItem cert:\CurrentUser\My\ -CodeSigningCert) | |
try { | |
$status = (Set-AuthenticodeSignature -FilePath $File -Certificate $codeCert -ErrorAction Stop) | |
if ($status.Status -eq 'Valid') { | |
Write-Host "Signed: $File" | |
} else { | |
Write-Host -ForegroundColor 'red' -BackgroundColor 'black' "Failed to sign $File. Error: $($status.StatusMessage). Make sure file encoding is UTF-8." | |
} | |
} catch { | |
Write-Host -ForegroundColor 'red' -BackgroundColor 'black' "$_" | |
} | |
} | |
function sign | |
{ | |
param( | |
[String]$file="" | |
) | |
if($file -ne "") { | |
Sign-Script $File | |
Add-SignedHistory $File | |
} else { | |
# No file was specified, give a choice from previously signed files | |
$History = Get-SignedHistory | |
if($History -eq $Null) { | |
Write-Host -ForegroundColor 'red' -BackgroundColor 'black' 'Error: No file specified. Please supply a filename to sign.' | |
} else { | |
Write-Host "Please select one of the previously signed files below:" | |
Write-Host "---" | |
$ItemCount = 0 | |
ForEach($Item in $History) { | |
Write-Host "[$ItemCount] $Item" | |
$ItemCount++ | |
} | |
Write-Host "---" | |
While($True) { | |
try { | |
Write-Host "Enter the number of the file to sign: " -NoNewLine | |
[Int][String]$Selected = ([Console]::ReadKey()).KeyChar | |
Write-Host "" | |
if($Selected -lt $History.Count) { | |
# Sign the specified file | |
Sign-Script $History[$Selected] | |
} else { | |
Write-Host "There is no number $Selected!" | |
Continue | |
} | |
Break | |
} catch { | |
Write-Host "" | |
Write-Host "That's not a number!" | |
} | |
} | |
} | |
} | |
} | |
# TODO: | |
# If a current user is false, provide a best guess. Look at timestamp of user home dirs. | |
function Get-CurrentUser($Computer) { | |
if ((Test-Connection -ComputerName $Computer -Quiet)) { | |
$computerInfo = Get-WmiObject -Class "Win32_ComputerSystem" -Namespace "root\CIMV2" -ComputerName $Computer | |
if ($computerInfo.UserName -ne $null) { | |
return $computerInfo.UserName | |
} else { | |
try { | |
# If explorer is running, someone is using RDP | |
$processCheck = (Get-Process -Name explorer -ComputerName $Computer -ErrorAction Stop) | |
return "RDP-User" | |
} catch { | |
# Process not found, no one is using this computer. | |
return $false | |
} | |
} | |
} else { | |
Write-Host "$Computer is offline" | |
} | |
} | |
function Search-ADUser { | |
param( | |
[String]$SearchString | |
) | |
$Match = Get-ADUser -Filter "samaccountname -like '*$($SearchString)*' -or name -like '*$($SearchString)*' -or givenname -like '*$($SearchString)*' -or surname -like '*$($SearchString)*' -or userprincipalname -like '*$($SearchString)*'" | |
if($Match -eq $null) { | |
# Nothing was found | |
Write-Host "No matching accounts were found." | |
} else { | |
$Match | |
} | |
} | |
function Search-ADComputer { | |
param( | |
[String]$SearchString | |
) | |
$Match = Get-ADComputer -Filter "samaccountname -like '*$($SearchString)*' -or name -like '*$($SearchString)*'" | |
if($Match -eq $null) { | |
# Nothing was found | |
Write-Host "No matching accounts were found." | |
} else { | |
$Match | |
} | |
} | |
function Search-Uninstall{ | |
param($Search) | |
$Keys = @('HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\*') | |
if([IntPtr]::Size -eq 8) { | |
$Keys += 'HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*' | |
} | |
$Keys | ForEach-Object {Get-ItemProperty $_} | Where-Object {$_.DisplayName -like "*$($Search)*"} | Select-Object DisplayName, DisplayVersion, UninstallString, PSPath | Sort-Object DisplayVersion | |
} | |
<# | |
.SYNOPSIS | |
Searches DHCP reservations for the hostname or MAC address. | |
.DESCRIPTION | |
A detailed description of the Search-DhcpReservation2 function. | |
.PARAMETER Server | |
The DHCP Server to query. Defaults to localhost. | |
.PARAMETER MacAddress | |
The MAC address to search for, can be in any format. | |
.PARAMETER ComputerName | |
The hostname to search for. | |
.NOTES | |
Additional information about the function. | |
#> | |
function Search-DhcpReservation { | |
[CmdletBinding()] | |
param | |
( | |
[String]$Server='localhost', | |
# Validates the supplied mac, can accept any of the common delimiters | |
[ValidatePattern("([0-9a-fA-F]{2}[-:]){5}[0-9a-fA-F]{2}|([0-9a-fA-F]{4}[\.|-]){2}[0-9a-fA-F]{4}|[0-9a-fA-F]{12}")] | |
[String]$MacAddress, | |
[String]$ComputerName | |
) | |
function Format-MacAddress { | |
[CmdletBinding()] | |
param | |
( | |
[Parameter(Mandatory = $true)] | |
[String]$MacAddress | |
) | |
# Remove the unknown delimiters and replace with colons | |
$MacAddress = $MacAddress -replace ':|-|\.','' | |
$MacAddress = $MacAddress.Insert('2', ':').Insert('5', ':').Insert('8', ':').Insert('11', ':').Insert('14', ':') | |
return $MacAddress | |
} | |
if($MacAddress -eq $null -and $ComputerName -eq $null) { | |
Write-Host 'Please provide either -MacAddress or -ComputerName to search DHCP reservations. -MacAddress can be in any format.' | |
return | |
} else { | |
# Find the most likely active DHCP Scope | |
# TODO: Verify that this only returns one item | |
$ScopeId = (Get-DhcpServerv4Scope -ComputerName $Server | Where-Object {$_.ScopeId -like '10.*.0.0' -and $_.State -eq 'Active'}).ScopeId | |
if ($MacAddress -ne $null) { | |
return Get-DhcpServerv4Reservation -ComputerName $Server -ScopeId $ScopeId | Where-Object {$_.MacAddress -eq (Format-MacAddress $MacAddress)} | |
} else { | |
Get-DhcpServerv4Reservation -ComputerName $Server -ScopeId $ScopeId | Where-Object {$_.Name -like "*$($ComputerName)*"} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment