├── README.md ├── FSRM ├── RansomwareBlockSmb │ ├── StartRansomwareBlockSmb.cmd │ └── RansomwareBlockSmb.ps1 ├── Copy-FSRMFolder.ps1 ├── Update-FSRMServer.ps1 └── New-FSRMServer.ps1 ├── Intune ├── Assign-AutoPilotDevice │ ├── GroupTags.txt │ ├── Assign-AutoPilotDevice.bat │ └── Assign-AutoPilotDevice.ps1 └── Set-IntuneDeviceManangementScriptAssignment.ps1 ├── ConfigMgr ├── Invoke-CMCimFunctions.ps1 ├── Read-CMLog.ps1 ├── Test-CMMPUrl.ps1 └── New-CMMaintenanceWindow.ps1 └── Other ├── Template-Logging.ps1 ├── Backup_TechNet_Gallery_Contributions_Category.ps1 └── Get-EdgeEnterpriseMSI.ps1 /README.md: -------------------------------------------------------------------------------- 1 | # PowerShell -------------------------------------------------------------------------------- /FSRM/RansomwareBlockSmb/StartRansomwareBlockSmb.cmd: -------------------------------------------------------------------------------- 1 | powershell -noexit "& ""C:\RansomwareBlockSmb\RansomwareBlockSmb.ps1""" 2 | -------------------------------------------------------------------------------- /Intune/Assign-AutoPilotDevice/GroupTags.txt: -------------------------------------------------------------------------------- 1 | AutoPilot-VM=AutoPilot-VM 2 | Friendly name=TAG002 3 | This is another GroupTag=TAG003 -------------------------------------------------------------------------------- /Intune/Assign-AutoPilotDevice/Assign-AutoPilotDevice.bat: -------------------------------------------------------------------------------- 1 | Powershell.exe -executionpolicy unrestricted -File %~dp0Assign-AutoPilotDevice.ps1 -------------------------------------------------------------------------------- /FSRM/Copy-FSRMFolder.ps1: -------------------------------------------------------------------------------- 1 | 2 | $ScriptSourcePath = "\\servername\FSRM\RansomwareBlockSmb" 3 | [array]$servers = "","" 4 | 5 | foreach($server in $servers) 6 | { 7 | try 8 | { 9 | Copy-Item $ScriptSourcePath "\\$server\c$" -recurse -Force 10 | Write-Host "RansomwareBlockSmb folder copied to \\$server\c$\" 11 | } 12 | catch 13 | { 14 | Write-Host $_.Exception.Message 15 | } 16 | } -------------------------------------------------------------------------------- /ConfigMgr/Invoke-CMCimFunctions.ps1: -------------------------------------------------------------------------------- 1 | <# 2 | .SYNOPSIS 3 | Functions for using Cim instance and methods with ConfigMgr 4 | 5 | .DESCRIPTION 6 | Examples how to use Invoke-CimMethod with Configuration Manager to add or remove a direct collection member 7 | 8 | .NOTES 9 | Version: 1.0 10 | Author: Mattias Benninge 11 | Creation Date: 2023-04-02 12 | Purpose/Change: Initial script development 13 | 14 | .EXAMPLE 15 | Gets a Computer object using Get-CimInstance 16 | $CMResource = Get-CMCimClient -CMServerFQDN CM01.corp.mblab.org -SiteCode PS1 -ComputerName "COMPUTER01" 17 | 18 | .EXAMPLE 19 | Gets a Collection object using Get-CimInstance 20 | $CMCollection = Get-CMCimCollection -CMServerFQDN CM01.corp.mblab.org -SiteCode PS1 -CollectionID 'PS100015' 21 | 22 | .EXAMPLE 23 | Adds a direct membership rule using Invoke-CimMethod 24 | Add-CMCimDirectMembershipRule -Collection $CMCollection -Resource $CMResource 25 | 26 | .EXAMPLE 27 | Removes a direct membership rule using Invoke-CimMethod 28 | Remove-CMCimDirectMembershipRule -Collection $CMCollection -ResourceID $CMResource.ResourceId 29 | #> 30 | 31 | function Get-CMCimClient { 32 | param ( 33 | [Parameter(Mandatory = $true)] 34 | [string]$CMServerFQDN, 35 | [Parameter(Mandatory = $true)] 36 | [string]$SiteCode, 37 | [Parameter(Mandatory = $true)] 38 | [string]$ComputerName 39 | ) 40 | [array]$resource = Get-CimInstance -ComputerName $CMServerFQDN -Namespace "ROOT\SMS\Site_$SiteCode" -ClassName "SMS_R_System" -Filter "Name = '$ComputerName'" 41 | If ($resource) { 42 | If ($resource.Count -eq 1) { 43 | return $resource 44 | } 45 | else { 46 | Write-Error -Message "Multiple resources found matching name $client = $($resource -join ",")" -LogLevel 3 47 | return $null 48 | } 49 | } 50 | } 51 | 52 | function Get-CMCimCollection { 53 | param ( 54 | [Parameter(Mandatory = $true)] 55 | [string]$CMServerFQDN, 56 | [Parameter(Mandatory = $true)] 57 | [string]$SiteCode, 58 | [Parameter(Mandatory = $true)] 59 | [string]$CollectionID 60 | ) 61 | 62 | $Collection = Get-CimInstance -ComputerName $CMServerFQDN -Namespace "ROOT\SMS\Site_$SiteCode" -ClassName "SMS_Collection" -Filter "CollectionID = '$CollectionID'" 63 | return $Collection 64 | } 65 | 66 | 67 | function Add-CMCimDirectMembershipRule { 68 | param ( 69 | $Collection, 70 | [Parameter(Mandatory = $true)] 71 | $Resource 72 | ) 73 | 74 | $null = New-CimInstance -Namespace "ROOT\SMS\Site_PS1" -OutVariable collectionRule -ClassName SMS_CollectionRuleDirect -ClientOnly -Property @{ 75 | ResourceClassName = [string]"SMS_R_System" 76 | RuleName = [string]$Resource.Name 77 | ResourceID = [uint32]$Resource.ResourceID 78 | } 79 | 80 | Invoke-CimMethod -InputObject $Collection -MethodName AddMemberShipRule -Arguments @{ CollectionRule = [CimInstance]$collectionRule[0] } -ErrorAction Stop 81 | 82 | } 83 | 84 | function Remove-CMCimDirectMembershipRule { 85 | param ( 86 | [Parameter(Mandatory = $true)] 87 | $Collection, 88 | [Parameter(Mandatory = $true)] 89 | [uint32]$ResourceID 90 | ) 91 | 92 | [ciminstance[]]$collRules = Get-CimInstance -InputObject $Collection | Select-Object -ExpandProperty CollectionRules 93 | Foreach ($rule in $collRules) { 94 | If ($rule.CimClass.CimClassName -eq "SMS_CollectionRuleDirect") { 95 | If($rule.ResourceID -eq "16777225") 96 | { 97 | $params = @{ collectionRule = $rule } 98 | Invoke-CimMethod -InputObject $Collection -MethodName DeleteMembershipRule -Arguments $params -ErrorAction Stop 99 | } 100 | } 101 | } 102 | } 103 | -------------------------------------------------------------------------------- /FSRM/RansomwareBlockSmb/RansomwareBlockSmb.ps1: -------------------------------------------------------------------------------- 1 | <# 2 | .SYNOPSIS 3 | RansomwareBlockSmb 4 | 5 | .DESCRIPTION 6 | Script that runs when a custom command is triggered on a FSRM server. It will block the bad user on SMBShare Level 7 | 8 | The script requires PowerShell 4.0 or later to work. 9 | 10 | .NOTES 11 | Version: 1.0 12 | Author: Mattias Benninge 13 | Creation Date: 2017-05-03 14 | Purpose/Change: Initial script development 15 | 16 | .EXAMPLE 17 | 18 | #> 19 | 20 | #region ---------------------------------------------------[Declarations]---------------------------------------------------------- 21 | 22 | #Any Global Declarations go here 23 | $maxlogfilesize = 5Mb 24 | 25 | #endregion 26 | #region ---------------------------------------------------[Functions]------------------------------------------------------------ 27 | 28 | #region Logging: Functions used for Logging, do not edit! 29 | Function Start-Log{ 30 | [CmdletBinding()] 31 | param ( 32 | [ValidateScript({ Split-Path $_ -Parent | Test-Path })] 33 | [string]$FilePath 34 | ) 35 | 36 | try 37 | { 38 | if (!(Test-Path $FilePath)) 39 | { 40 | ## Create the log file 41 | New-Item $FilePath -Type File | Out-Null 42 | } 43 | 44 | ## Set the global variable to be used as the FilePath for all subsequent Write-Log 45 | ## calls in this session 46 | $global:ScriptLogFilePath = $FilePath 47 | } 48 | catch 49 | { 50 | Write-Error $_.Exception.Message 51 | } 52 | } 53 | 54 | Function Write-Log{ 55 | param ( 56 | [Parameter(Mandatory = $true)] 57 | [string]$Message, 58 | 59 | [Parameter()] 60 | [ValidateSet(1, 2, 3)] 61 | [int]$LogLevel = 1 62 | ) 63 | $TimeGenerated = "$(Get-Date -Format HH:mm:ss).$((Get-Date).Millisecond)+000" 64 | $Line = '' 65 | 66 | if($MyInvocation.ScriptName){ 67 | $LineFormat = $Message, $TimeGenerated, (Get-Date -Format MM-dd-yyyy), "$($MyInvocation.ScriptName | Split-Path -Leaf):$($MyInvocation.ScriptLineNumber)", $LogLevel 68 | } 69 | else { #if the script havn't been saved yet and does not have a name this will state unknown. 70 | $LineFormat = $Message, $TimeGenerated, (Get-Date -Format MM-dd-yyyy), "Unknown", $LogLevel 71 | } 72 | $Line = $Line -f $LineFormat 73 | 74 | #Make sure the logfile do not exceed the $maxlogfilesize 75 | if (Test-Path $ScriptLogFilePath) { 76 | if((Get-Item $ScriptLogFilePath).length -ge $maxlogfilesize){ 77 | If(Test-Path "$($ScriptLogFilePath.Substring(0,$ScriptLogFilePath.Length-1))_") 78 | { 79 | Remove-Item -path "$($ScriptLogFilePath.Substring(0,$ScriptLogFilePath.Length-1))_" -Force 80 | } 81 | Rename-Item -Path $ScriptLogFilePath -NewName "$($ScriptLogFilePath.Substring(0,$ScriptLogFilePath.Length-1))_" -Force 82 | } 83 | } 84 | 85 | Add-Content -Value $Line -Path $ScriptLogFilePath 86 | 87 | } 88 | #endregion 89 | 90 | # Add functions Here 91 | 92 | 93 | #endregion 94 | #-----------------------------------------------------------[Execution]------------------------------------------------------------ 95 | #Default logging to %temp%\scriptname.log, change if needed. 96 | Start-Log -FilePath "C:\RansomwareBlockSmb\RansomWareBlockSmbLog.log" 97 | # Syntax is: 98 | # Loglevel 1 is default and does not need to be specified 99 | # Write-Log -Message "" 100 | # Write-Log -Message "" -LogLevel 2 101 | 102 | #Script Execution goes here 103 | $shares = get-WmiObject -class Win32_Share |Where-Object {$_.Description -ne "Default Share" -and $_.Description -ne "Remote IPC"} 104 | $events = Get-WinEvent -FilterHashtable @{logname='Application';providername='SRMSVC';StartTime=(get-date).AddMinutes(-2)} 105 | 106 | foreach ($Event in $Events) 107 | { 108 | $MsgArray = $Event.Message -split ";" 109 | $BadUser = $MsgArray[0] 110 | $BadFile = $MsgArray[1] 111 | $Rule = $MsgArray[2] 112 | 113 | #Match filepath against local share 114 | foreach($share in $shares){ 115 | $sPath = [regex]::escape("$($share.Path)") 116 | if($BadFile -match $sPath) 117 | { 118 | $SharePart = $share.Name 119 | } 120 | } 121 | 122 | if ($Rule -match "Ransomware_Extensions") 123 | { 124 | try{ 125 | Block-SmbShareAccess -Name $SharePart -AccountName $BadUser -Force 126 | } 127 | catch 128 | { 129 | Write-Log -Message $_.Exception.Message -LogLevel 3 130 | } 131 | 132 | Write-Log -Message "$BadUser;$SharePart;$BadFile" 133 | 134 | 135 | } 136 | else{exit} 137 | } -------------------------------------------------------------------------------- /Other/Template-Logging.ps1: -------------------------------------------------------------------------------- 1 | <# 2 | .SYNOPSIS 3 |