Skip to content

Instantly share code, notes, and snippets.

@altrive
altrive / Microsoft.PowerShellISE_profile.ps1
Last active February 7, 2023 07:11
Sample code to defer PowerShell modules loading.
#Register OnIdle event to load PS moduless.
Register-EngineEvent -SourceIdentifier ([Management.Automation.PsEngineEvent]::OnIdle) -MaxTriggerCount 1 -Action {
Import-Module TabExpansion++
#Import-Module FunctionExplorer
Import-Module VariableExplorer
Import-Module PSCodeAnalyzer.ISEAddin
Import-Module Altrive.ISEExtensions
#Remove current Job (No Need to get job result)
$EventSubscriber.Action | Remove-Job -Force -ErrorAction Ignore
@altrive
altrive / New-OSBaseImage.ps1
Last active September 27, 2022 16:07
PowerShell script to create OS Base VHD for Windows 8/Windows Server2012
#Create OS BaseImage VHD
function New-OSBaseImage
{
[Cmdletbinding()]
param(
$VhdPath,
$OSImageName,
$MediaPath,
$VhdConfig,
$DismConfig,
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
@altrive
altrive / PSNuGet.ps1
Created January 24, 2014 14:29
Sample code to use NuGet as PowerShell package manager
$ErrorActionPreference = "Stop"
Set-StrictMode -Version Latest
$Script:PackageManager = $null
function Initialize-NuGetPackageManager
{
[CmdletBinding()]
param (
[hashtable] $Repositories = @{},
@altrive
altrive / SetAccountPrevilage.ps1
Created February 22, 2014 09:55
Sample code to set LSA account previlage via P/Invoke(LsaAddAccountRights).
#Requires -RunAsAdministrator
function Main
{
$ErrorActionPreference = "Stop"
$VerbosePreference = "Continue"
#Target account to assign previlage
$svcAccount = "TestSvc"
@altrive
altrive / SavedCredential.ps1
Last active October 9, 2020 09:38
Credential management utilities for PowerShell
#Requires -Version 3
#Credential file path
$script:CredentialsFile = Join-Path $env:TEMP "SavedCredentials\Credentials.xml"
function Set-SavedCredential()
{
[CmdletBinding()]
param(
[Parameter(Mandatory)]
@altrive
altrive / Cevio.ps1
Last active March 23, 2020 02:50
Call CeVIO .NET API by PowerShell
if([Environment]::Is64BitProcess){
throw "64bitプロセス内だとDLLロードに失敗する"
}
$ErrorActionPreference = "Stop"
Add-Type -Path "${env:ProgramFiles(x86)}\CeVIO\CeVIO Creative Studio\CeVIO.Talk.RemoteService.dll"
[CeVIO.Talk.RemoteService.ServiceControl]::StartHost($true) >$null
$talker = New-Object CeVIO.Talk.RemoteService.InteroperableComponents.Talker
$ErrorActionPreference = "Stop"
#Need to install Nuget packages before execute
Add-Type -Path (Join-Path (Split-Path $profile -Parent) "packages\Windows7APICodePack-Core.1.1.0.0\lib\Microsoft.WindowsAPICodePack.dll" -Resolve)
Add-Type -Path (Join-Path (Split-Path $profile -Parent) "\packages\Windows7APICodePack-Shell.1.1.0.0\lib\Microsoft.WindowsAPICodePack.Shell.dll" -Resolve)
<#
#TODO: Define Interop code to register shortcut with appid
$referencedAssemblies = @(
[MS.WindowsAPICodePack.Internal.PropVariant].Assembly.FullName,
@altrive
altrive / Get-WindowsUpdateFileList.ps1
Last active November 12, 2019 16:23
PowerShell cmdlets to get WindowsUpdate patch file List.
#Requires –Version 3
#Get WindowsUpdate List for offline patch
function Get-WindowsUpdateFileList
{
param(
[Parameter(Mandatory=$True)]
[string] $Filter
)
$objSession = New-Object -ComObject "Microsoft.Update.Session"
@altrive
altrive / Get-EventLogError.ps1
Last active November 12, 2019 16:22
PowerShell utility function to find EventLog errors.
function Get-EventLogError
{
[CmdletBinding()]
param(
[Parameter(Mandatory,ParameterSetName="FromLastBootupTime")]
[switch] $FromLastBootupTime,
[Parameter(Mandatory,ParameterSetName="FromLastQueryTime")]
[switch] $FromLastQueryTime,
[Parameter(Mandatory,ParameterSetName="Range")]
[DateTime] $From,