Skip to content

Instantly share code, notes, and snippets.

View itpropro's full-sized avatar
🏠
Working from home

Jan-Henrik Damaschke itpropro

🏠
Working from home
View GitHub Profile
#Does not work with DSC Configurations, using the $using: variable to change scopes
Invoke-Command -ScriptBlock (Get-Command .\DSCConfig.ps1|select -ExpandProperty ScriptBlock) -ComputerName localhost
Invoke-Command -FilePath ('.\' + $DSCConfig + '.ps1') -ComputerName localhost
#Does work with $using because of the -NoNewScope switch
Invoke-Command -ScriptBlock (Get-Command .\DSCConfig.ps1|select -ExpandProperty ScriptBlock) -NoNewScope
#Applies the pending configuration
Invoke-CimMethod -Namespace root\Microsoft\Windows\DesiredStateConfiguration -ClassName MSFT_DSCLocalConfigurationManager -MethodName ApplyConfiguration
#Starts a consistency check for the last applied config
Invoke-CimMethod -Namespace root\Microsoft\Windows\DesiredStateConfiguration -ClassName MSFT_DSCLocalConfigurationManager -MethodName PerformRequiredConfigurationChecks -Arguments @{Flags = [uint32]1}
$nics = Get-CimInstance Win32_NetworkAdapter -Filter 'netenabled = true'
$powerenabled = Get-CimInstance -Namespace root\wmi -ClassName MSPower_DeviceWakeEnable
$nics|% {if ($powerenabled.InstanceName -match [regex]::Escape($_.PNPDeviceID)) {$powernics = $_.PNPDeviceID}}
$Items = $powerenabled|Where-Object {$_.InstanceName -like "$powernics*"}
$Items.Enable = $true
Set-CimInstance -InputObject $Items
$ips = @()
foreach ($i in 1..254)
{
$ips += "192.168.0.$i"
}
New-NetFirewallRule -Protocol TCP –LocalPort 3389 -Profile Public -DisplayName 'RDP VPN S2S' -Direction Inbound -Action Allow -RemoteAddress $ips
$OFS =’; ’
$Job = {Get-VM|Stop-VM}
Start-Job $Job
While((Get-VM|? State -NE 'Off').Count -gt 0) {Write-Host 'VMs' ([string](Get-VM|? State -ne 'Off').Name) 'shutting down...';Start-Sleep 1}
Stop-Computer -Force
$message = "The size of your desktop folder exceeds 500MB.`r`nThis can cause delays at logon and logoff.`r`n`r`nGreetings from your IT-Department"
if ([Math]::Truncate((Get-ChildItem "$env:USERPROFILE\Desktop" -Recurse|Measure-Object -property Length -sum).Sum / 1MB) -gt 500){[System.Windows.Forms.MessageBox]::Show($message)}
function Get-SidfromHex {
param
(
[string]$Sid
)
$ByteSid = New-Object Byte[] 150
for ($i = 0; $i -lt $Sid.Length ; $i += 2) {
$ByteSid.Set(($i/2),([Byte]::Parse($Sid.Substring($i, 2), [System.Globalization.NumberStyles]::HexNumber)))
}
function Get-NtSecurityDescriptor {
param
(
[string]$SecurityDescriptor
)
$ntsecdesc = New-Object Byte[] ($SecurityDescriptor.Length)
for ($i = 0; $i -lt $SecurityDescriptor.Length ; $i += 2) {
$ntsecdesc.Set(($i/2),([Byte]::Parse($SecurityDescriptor.Substring($i, 2), [System.Globalization.NumberStyles]::HexNumber)))
}
[DscLocalConfigurationManager()]
Configuration PullClientV2 {
param(
[Parameter(Mandatory=$True)]
[String]$ConfigurationNames,
[Parameter(Mandatory=$True)]
[String]$Server
)
Settings
{
# When a document is created in D:\Test with the extension .rtf This script will print its name
Register-CimIndicationEvent -Query "SELECT * FROM __InstanceCreationEvent WITHIN 1 WHERE
TargetInstance ISA 'CIM_DataFile' AND (TargetInstance.Drive='D:' AND TargetInstance.Path='\\Test\\' AND TargetInstance.Extension='rtf')" -Action {
Write-Host $($Event.SourceEventArgs.NewEvent.TargetInstance.Name)
}