Skip to content

Instantly share code, notes, and snippets.

@CosmosKey
CosmosKey / Import Base64 cert into cert store
Created May 25, 2021 08:11
Import Cert without touching the file system.
# Orginal Cert
$base64CertAsGetContentImported = @(
'-----BEGIN CERTIFICATE-----'
'MIIDPjCCAiagAwIBAgIQQuZ95xvR1p5DHeDuOzxbPzANBgkqhkiG9w0BAQsFADAb'
'MRkwFwYDVQQDDBB3d3cuZmFicmlrYW0uY29tMB4XDTIxMDUyNTA3MzAwNFoXDTIy'
'MDUyNTA3NTAwNFowGzEZMBcGA1UEAwwQd3d3LmZhYnJpa2FtLmNvbTCCASIwDQYJ'
'KoZIhvcNAQEBBQADggEPADCCAQoCggEBANs51YbaCyGdgduRTKAwqd2R9z0BwfH1'
'RuBzWP4imFNjvgmJDR4tII3ESng3udp3QqBu8ArdBVAZL0a5w+lJ9uhC8vD+pDGb'
'dtEqBi5fSzEMSGahVT+P8VqB2myVah9Ab6kuz5dGjtkIX7aoI3yId3Ji3lU8gtbv'
'U8Az5mxqhsDajl9BuUJCtVr02+i4WmbSLI3nzxFDXYJjJIVvrS/yxTzH7nuRud+d'
$tmpFile = New-TemporaryFile
@"
DisplayName,Phone
Johan Akerstrom,0721234567
Test Tester,0711234567
"@ | Set-Content $tmpFile
#requires -version 5.1
@CosmosKey
CosmosKey / GetHelpRightForPSTypeNames.ps1
Created November 18, 2020 09:55
How to Get Help Right For PSTypeNames
#
# How to get help right for an array of PSTypeNames
#
# Awesome function to get some random stuff
# The important thing is that we get objects with an added PSTypeName of 'Stuff'
Function Get-Stuff {
[cmdletbinding()]
param(
@CosmosKey
CosmosKey / AbortShutdown.ps1
Created April 22, 2019 20:47
Abort Windows Shutdown
$definition = @'
using System;
using System.Runtime.InteropServices;
public class W32ShutdownUtil {
[DllImport("advapi32.dll", SetLastError = true)]
public static extern bool AbortSystemShutdown(String machineName);
[DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]
internal static extern bool AdjustTokenPrivileges(IntPtr htok, bool disall,ref TokPriv1Luid newst, int len, IntPtr prev, IntPtr relen);
While ( $True ) { $PSise.Options.Zoom = 100 + 10 * [math]::sin( (Get-Date).TimeOfDay.TotalSeconds ); Sleep -Milliseconds 40 }
@CosmosKey
CosmosKey / Get-ADGroupMemberSamAccountName
Created February 27, 2018 23:54
Get-ADGroupMemberSamAccountName
Function Get-ADGroupMemberSamAccountName {
param(
[Parameter(Mandatory,ValueFromPipeline)]
[ValidateNotNullOrEmpty()]
[string]$GroupName
)
process {
$name = $GroupName
$domain = [System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain()
$dc = $domain.FindDomainController([System.DirectoryServices.ActiveDirectory.LocatorOptions]::WriteableRequired)
$Name = "MSFT-Office 365 Enterprise E1"
$groupSearcher = [adsisearcher]"(samaccountname=$Name)"
$group = $groupSearcher.FindOne()
$memberSearch = [adsisearcher]::new($group.GetDirectoryEntry(),"(&(objectClass=user)(objectCategory=Person))",[string[]]'samaccountname','base')
$memberSearch.AttributeScopeQuery = "member";
foreach($member in $memberSearch.FindAll())
{
$member.Properties['samaccountname']
}
@CosmosKey
CosmosKey / SelectBuildingAndOfficeForm.ps1
Last active December 28, 2017 10:23
SelectBuildingAndOfficeForm.ps1 A piece of DropDown Forms sample code
# DropDown Data
$office = @{
'HQ' = 'Ground floor','1st floor','2nd floor','3rd floor'
'Garage' = 'Shop floor','Office'
'Sales office' = 'Ground floor','1st floor'
}
# Load assemblies
[void][System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void][System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
@CosmosKey
CosmosKey / Get-CertificateChain.ps1
Created September 9, 2017 14:07
Get-CertificateChain.ps1
Function Get-CertificateChain {
param(
[string]$server=$(throw "Mandatory parameter -Server is missing."),
[int]$port=$(throw "Mandatory parameter -Port is missing."),
[switch]$ToBase64
)
$code=@"
using System;
using System.Collections;
using System.Net;
@CosmosKey
CosmosKey / ConsoleCursorLab.ps1
Created August 26, 2017 10:18
Console Cursor Lab, handling cursors in [console]
Write-Host "Done Name State HasData"
$global:js = [System.Collections.ArrayList]::new()
for($i=0;$i -lt 10;$i++){
$j = Start-Job {"JOB" + $using:i;Start-Sleep -Milliseconds (Get-Random -Maximum 3000 -Minimum 0) } -Name "MyJob-$i"
$j | Add-Member -Name CursorTop -MemberType NoteProperty -Value ([console]::CursorTop) -Force
[void]$js.Add($j)
Write-Host "x " -ForegroundColor Red -NoNewline
Write-Host ("{0} {1} {2}" -f $j.Name,$j.State,$j.HasMoreData)
}
[console]::CursorVisible = $false