Disassembler0 / Win10-Initial-Setup-Script

PowerShell script for automation of routine tasks done after fresh installations of Windows 10 / Server 2016 / Server 2019
MIT License
4.7k stars 1.08k forks source link

Win10-NotInitial-Setup-Script #229

Closed fMichaleczek closed 4 years ago

fMichaleczek commented 5 years ago

I made a POC for daily not initial setup. WindowsTweaks

Features :

Breaking changes (todo) :

I think this code is suffisant for handle HKCU change in desktop without explorer restart

function Refresh-Explorer {
    if ( -not ([System.Management.Automation.PSTypeName]'WindowsDesktopTools.Explorer').Type) {
        $typeParams = @{
            Namespace = 'WindowsDesktopTools'
            Name = 'Explorer'
            Language = 'CSharp'
            MemberDefinition = @'
                private static readonly IntPtr HWND_BROADCAST = new IntPtr(0xffff);
                private const int WM_SETTINGCHANGE = 0x1a;
                private const int SMTO_ABORTIFHUNG = 0x0002;

                [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
                static extern bool SendNotifyMessage(IntPtr hWnd, uint Msg, IntPtr wParam, string lParam);

                [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
                private static extern IntPtr SendMessageTimeout(IntPtr hWnd, int Msg, IntPtr wParam, string lParam, int fuFlags, int uTimeout, IntPtr lpdwResult);

                [DllImport("shell32.dll", CharSet = CharSet.Auto, SetLastError = false)]
                private static extern int SHChangeNotify(int eventId, int flags, IntPtr item1, IntPtr item2);

                public static void Refresh()
                {
                    // Update desktop icons
                    SHChangeNotify(0x8000000, 0x1000, IntPtr.Zero, IntPtr.Zero);
                    // Update environment variables
                    SendMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE, IntPtr.Zero, null, SMTO_ABORTIFHUNG, 100, IntPtr.Zero);
                    // Update taskbar
                    SendNotifyMessage(HWND_BROADCAST, WM_SETTINGCHANGE, IntPtr.Zero, "TraySettings");

                }
'@
        }
        Add-Type @typeParams -IgnoreWarnings -ErrorAction Stop
    }
    Write-Verbose 'Refreshing Shell environment ...'
    [WindowsDesktopTools.Explorer]::Refresh()
}

Let me know, if you are interested by my works .After this version on gist, I will maybe try to rewrite functions (parsing AST scriptblock) and be a fork :/

Thank you so much for this project !

Disassembler0 commented 5 years ago

Wow. That's a huge overhaul. I need some time to go through all this (not sure when, I haven't had any since February :/ ).

Right off the bat I see a lot of boilerplate code which could probably be simplified or moved to more appropriate places. I have had similar ideas for the grouping and categorizing and wanted to do it via custom attributes, but as it turns out, PowerShell is still not .NETty enough for that.

I'll probably eventually turn down most of your ideas, but don't be bummed about it and keep up the good work. :)

fMichaleczek commented 5 years ago

@Disassembler0 About your problems with attributes, this is a workaround :

@'
    class SomeAttribute : Attribute {
        [string]$Text
    }

    class OtherAttribute : Attribute {
        [string]$Text
    }
'@ > "$pwd\attributes.ps1"

@'
class MyClass {
    [SomeAttribute(Text="sometext")]
    [OtherAttribute(Text="othertext")]
    MyMethod() {
        # ...
    }
}
[MyClass].GetMethod("MyMethod").GetCustomAttributes($false)
'@ > "$pwd\MyClass.ps1"

. "$pwd\attributes.ps1"
. "$pwd\MyClass.ps1"
Disassembler0 commented 4 years ago

The version and features you've got are nice and all, but I'm probably just lying to myself by keeping this issue open. I don't think I'd ever get to merging most of the ideas, as it seems that my use cases are vastly different from yours. TBH, I don't even use my own script anymore, because it deviated from the original intention (short, simple and straight to the point) so much. :/ So with that, thanks for your work, but there's no point in keeping this issue open. Sorry about that.