Skip to content

Instantly share code, notes, and snippets.

View martin77s's full-sized avatar

Martin Schvartzman martin77s

View GitHub Profile
@martin77s
martin77s / Remove-Profile.ps1
Last active October 22, 2018 09:32
Remove Profiles from a local or remote computer
#requires -version 3
function Remove-Profile {
param(
[string[]]$ComputerName = $env:ComputerName,
[pscredential]$Credential = $null,
[string[]]$Name,
[ValidateRange(0,365)][int]$DaysOld = 0,
[string[]]$Exclude,
[switch]$IgnoreLastUseTime,
[switch]$Remove
@martin77s
martin77s / PowerShellFromBatch.cmd
Last active April 4, 2021 13:35
Embed PowerShell code in a batch file
@ECHO off
@setlocal EnableDelayedExpansion
@goto label1
@echo this lines should not be printed
@echo and this whole block (lines 4-8) can actually be removed
@echo I added these just for Charlie22911
@:label1
@set LF=^
@martin77s
martin77s / Get-CertificateFromCredential.ps1
Last active February 15, 2018 08:34
Get the certificate selected in Get-Credential
function Get-CertificateFromCredential {
param([PSCredential]$Credential)
Add-Type -TypeDefinition @'
using System;
using System.Runtime.InteropServices;
public static class NativeMethods {
@martin77s
martin77s / New-Share.ps1
Created February 15, 2018 08:35
Create a new share with specific permissions
function New-Share {
param(
[string] $ComputerName = $env:COMPUTERNAME,
[string] $Path = 'C:\Temp',
[string] $ShareName = 'Temp',
[string] $AccountName = 'Domain Users',
[ValidateSet('FullControl', 'Change','Read')] $AccessPermissions = 'Read',
[string] $ShareDescription
)
@martin77s
martin77s / Add-ShareAccess.ps1
Created February 15, 2018 08:37
Add permissions to an existing share
function Add-ShareAccess {
param(
[string] $ComputerName = $env:COMPUTERNAME,
[string] $ShareName = 'Temp',
[string] $AccountName = 'Domain Users',
[ValidateSet('FullControl', 'Change','Read')] $AccessPermissions = 'Read'
)
# Convert the AccessPermissions
$accessFlags = @{
@martin77s
martin77s / DscWithWinRmDisabled.ps1
Last active August 17, 2020 13:55
Using DSC with the WinRM service disabled
#region ### BUILD THE CONFIGURATION ###
# Declare the configuration:
Configuration TestDscWithoutWinRm {
Import-DscResource –ModuleName PSDesiredStateConfiguration
node localhost {
File akada {
Ensure = 'Present'
Type = 'File'
Contents = 'Martin was here!'
@martin77s
martin77s / AddSessionConfigurationPermissions.ps1
Created February 25, 2018 17:54
Add permissions to a Session Configuration programmatically
# The identity to add permissions for
$Identity = "myDomain\nonAdmins"
# The configuration name to change permissions to (default is 'microsoft.powershell')
$sessionConfigurationName = 'microsoft.powershell'
# Get the current permissions on the default endpoint
$sddl = (Get-PSSessionConfiguration -Name $sessionConfigurationName).SecurityDescriptorSddl
@martin77s
martin77s / Get-LogonReport.ps1
Created March 21, 2018 13:50
Get-LogonReport
function Get-LogonReport {
param($ComputerName = $Env:COMPUTERNAME)
$filterXml = '<QueryList><Query Id="0" Path="Security"><Select Path="Security">*[System[(EventID=4624)]]</Select></Query></QueryList>'
$logonTypes = @{
2 = 'Interactive'
3 = 'Network'
4 = 'Batch'
5 = 'Service'
6 = 'Proxy'
@martin77s
martin77s / Disable-Scripting.FileSystemObject.ps1
Last active February 12, 2020 18:29
Disabling the Scripting.FileSystemObject ComObject (When you get the 0x8002801c error)
#region Helper functions
function Set-Ownership {
[CmdletBinding(SupportsShouldProcess = $false)]
param(
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[string]$Path,
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()] [ValidatePattern('(\w+)\\(\w+)')]
@martin77s
martin77s / Get-MsiProductInformation.ps1
Created May 26, 2018 19:11
Get the product name, version and Id from an MSI installation package
function Get-MsiProductInformation {
param($Path)
$msis = Get-ChildItem -File -Path $Path -Filter '*.msi'
foreach($msi in $msis) {
$WI = New-Object -ComObject WindowsInstaller.Installer
$DB = $WI.GetType().InvokeMember("OpenDatabase","InvokeMethod", $Null, $WI, @($msi.FullName, 0))
$query = 'SELECT * FROM Property'
$View = $DB.GetType().InvokeMember("OpenView","InvokeMethod",$Null,$DB,($query))
$View.GetType().InvokeMember("Execute", "InvokeMethod", $Null, $View, $Null)