🐶

PowerShellで自動セットアップ

2025/01/03に公開

PowerShellで自動化と言っても先に環境がないとどうにもなんねーわ
ってわけで今更ですが先ず環境を作ります/作りたいなというお話

管理権限でセッション開始

セットアップが環境弄る以上は大抵管理権限必要なんでそのへんは諦めるとして
少なくとも何度もUAC画面拝んで昇格すんのはメンドクセーです
ってわけでPowerShellくんが自分で管理権限に昇格する方法がこちら
# この辺でセットアップすると書いてある辺りに後述のWingetコマンドを並べます

# 管理者権限セッションでの実行を要求する
function isInAdmin {
    $UID = [Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()
    return $UID.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
}
if (isInAdmin -eq $false) {
    Start-Process powershell -ArgumentList "-NoExit", "-File", """$($MyInvocation.MyCommand.Path)""" -Verb RunAs
    exit 0
}
# この辺でセットアップする

Winget

Wingetというのはコマンドラインからアプリケーションをインストールするコマンド?です
2025現在ならWindows10/11ともにデフォで使え...るハズ多分きっといまそかり
どーも本体?はストアアプリのアプリインストーラーらしいんで普通に環境標準なのデスぞ

なおWingetはCUIなんでどーしてもインストールするそのパッケージの名前が必要になります
作業はこんな感じにサブコマンドつけて検索するだけですが、まぁソコはちょっと面倒ですねー
※つーわけで最後によく使われてるだろーアプリ並べてますんで参考にどーぞ

winget search imagemagick

名前                 ID                          バージョン 一致             ソース
-----------------------------------------------------------------------------------
ImageMagick Q16      ImageMagick.Q16             7.1.1.43   Tag: imagemagick winget
ImageMagick Q16-HDRI ImageMagick.Q16-HDRI        7.1.1.43   Tag: imagemagick winget
ImageMagick Q8       ImageMagick.Q8              7.1.1.43   Tag: imagemagick winget
ImageMagick Q16-HDRI ImageMagick.ImageMagick     7.1.1.43                    winget
ImageMagick Q16      ImageMagick.ImageMagick.Q16 7.1.1.43                    winget
ImageMagick Q8       ImageMagick.ImageMagick.Q8  7.1.1.43                    winget

実際のインストールは以下のように調べたIDを指定して実行するだけです
が、まぁ所詮Windowsだしセットアップできんのは大体環境レベル一択だと思ってください
指定自体はユーザレベルインストールも可能なんだけど多分MSIインストール同等なんで大抵無理
※ユーザレベルでインストールしたい場合はscoop使ってください

winget install ImageMagick.Q16

予想通りでしょうが一応アンインストールはuninstallで現状の一覧はlistです
なお、こいつでインストールしたブツはwinget upgrade --all一括更新できますんで
会社PCの複数台管理なんかにはとっても便利です※必要ならバージョン指定も可能です

configure

...と、まぁWingetは直接パッケージをインストールすることも出来るんですが、
サブコマンドのconfigureで構成管理?っぽい事もできますっていうかこっちが本題

winget configure configure.dsc.yaml

引数なファイルはセットアップ後の状態を指定する物体でyamlで記述します
DSCってのはDesired State Configuration(希望状態設定?)って意味らしいですが
まぁ横文字なんぞどーでもいいですね、おっさん的には

ってわけでとりあえずサンプルとしてのconfigure.dsc.yamlの中身はこんな感じです
内容的にはPowerToysをインストールしてちょっと設定するだけです
何故か何処の会社でも強制されるディスプレイ電源OFFをぶっ殺します

設定の後半ではMicrosoft.PowerToys.Configure/PowerToysConfigureを使ってますが
コレはPowerToys設定用の宣言なので当然PowerToys以外のアプリの設定には使えません
他のアプリの設定を弄りたい場合はそれ用のDSCモジュールをインストールして使います
多分汎用ファイル編集なFileContentDsc辺りでどーにかすることになるんでしょーね

んで実はPSGallaryな他のDSCは大抵Windows自体の設定を変更するヤツです
例えばNetworkingDscはデフォルトゲートウェイとかファイヤーウォールとか設定するヤツで
ActiveDirectoryDscはドメイン関連ComputerManagementDscは電源とか設定できます
※でもおっさんは情シスじゃないんで別に要りませんなー流石にドメイン権限はないし

なおMicrosoft.PowerToys.ConfigureってDSCモジュールはPowerToysインストール時に
勝手にインストールされてますんでなんかすげー不思議な感じですがそーいうもんです
同様にVisualStudio/VSCode辺りもDSCモジュールがインストールされるようです
※後述してますがMicrosoft.VisualStudio.DSCってを使うと
 VisualStudioでWinForm開発環境を追加インストールすることなんかもできます

もう少しマニュアル制御したい場合はPowerShellDSC?のScriptってのも使えるんですが
正直PowerShell呼ぶだけですんで冒頭のスクリプトに直接書いた方が早いですねー
特定のMSIファイルをインストールとかファイルサーバからコピーとかも同じかな
...いや、一生懸命調べたんだけどマジで使い所が見つからねぇんですよPowerShellDSC...

ところでDSCのインストールはそれぞれのマニュアル眺めればいいんですが
PowerShell経由の場合は基本的に以下のようにInstall-Moduleでどーにかなります
ので、使うならこの辺もスクリプトにブッ込んでしまったほーが良いでしょう
※つーかWinget自体がPowerShellありきっぽいんだよなぁ...

Install-Module -Name FileContentDsc

なおDSCモジュールがインストールしてあるかどーかは以下のコマンドで調べられるようです
多分大抵の環境ではデフォでPowerShellDSCなモジュールの一覧が出てくるハズ

Get-DscResource
                                                                                                                                         ]
ImplementedAs   Name                      ModuleName                     Version    Properties                                           ]
-------------   ----                      ----------                     -------    ----------                                           ]
Binary          File                                                                {Microsoft.PowerShell.DesiredStateConfiguration.D…   ]
Binary          SignatureValidation                                                 {Microsoft.PowerShell.DesiredStateConfiguration.D…   ]
PowerShell      PackageManagement         PackageManagement              1.0.0.1    {Microsoft.PowerShell.DesiredStateConfiguration.D…   ]
PowerShell      PackageManagementSource   PackageManagement              1.0.0.1    {Microsoft.PowerShell.DesiredStateConfiguration.D…   ]
PowerShell      Archive                   PSDesiredStateConfiguration    1.1        {Microsoft.PowerShell.DesiredStateConfiguration.D…   ]
PowerShell      Environment               PSDesiredStateConfiguration    1.1        {Microsoft.PowerShell.DesiredStateConfiguration.D…   ]
PowerShell      Group                     PSDesiredStateConfiguration    1.1        {Microsoft.PowerShell.DesiredStateConfiguration.D…   ]
Composite       GroupSet                  PSDesiredStateConfiguration    1.1        {Microsoft.PowerShell.DesiredStateConfiguration.D…   ]
Binary          Log                       PSDesiredStateConfiguration    1.1        {Microsoft.PowerShell.DesiredStateConfiguration.D…   ]
PowerShell      Package                   PSDesiredStateConfiguration    1.1        {Microsoft.PowerShell.DesiredStateConfiguration.D…   ]
Composite       ProcessSet                PSDesiredStateConfiguration    1.1        {Microsoft.PowerShell.DesiredStateConfiguration.D…   ]
PowerShell      Registry                  PSDesiredStateConfiguration    1.1        {Microsoft.PowerShell.DesiredStateConfiguration.D…   ]
PowerShell      Script                    PSDesiredStateConfiguration    1.1        {Microsoft.PowerShell.DesiredStateConfiguration.D…   ]
PowerShell      Service                   PSDesiredStateConfiguration    1.1        {Microsoft.PowerShell.DesiredStateConfiguration.D…   ]
Composite       ServiceSet                PSDesiredStateConfiguration    1.1        {Microsoft.PowerShell.DesiredStateConfiguration.D…   ]
PowerShell      User                      PSDesiredStateConfiguration    1.1        {Microsoft.PowerShell.DesiredStateConfiguration.D…   ]
PowerShell      WaitForAll                PSDesiredStateConfiguration    1.1        {Microsoft.PowerShell.DesiredStateConfiguration.D…   ]
PowerShell      WaitForAny                PSDesiredStateConfiguration    1.1        {Microsoft.PowerShell.DesiredStateConfiguration.D…   ]
PowerShell      WaitForSome               PSDesiredStateConfiguration    1.1        {Microsoft.PowerShell.DesiredStateConfiguration.D…   ]
PowerShell      WindowsFeature            PSDesiredStateConfiguration    1.1        {Microsoft.PowerShell.DesiredStateConfiguration.D…   ]
Composite       WindowsFeatureSet         PSDesiredStateConfiguration    1.1        {Microsoft.PowerShell.DesiredStateConfiguration.D…   ]
PowerShell      WindowsOptionalFeature    PSDesiredStateConfiguration    1.1        {Microsoft.PowerShell.DesiredStateConfiguration.D…   ]
Composite       WindowsOptionalFeatureSet PSDesiredStateConfiguration    1.1        {Microsoft.PowerShell.DesiredStateConfiguration.D…   ]
PowerShell      WindowsPackageCab         PSDesiredStateConfiguration    1.1        {Microsoft.PowerShell.DesiredStateConfiguration.D…   ]
PowerShell      WindowsProcess            PSDesiredStateConfiguration    1.1        {Microsoft.PowerShell.DesiredStateConfiguration.D…   ]

本題?

っつーわけで以上の知識を元に冒頭のスクリプトに好き放題書きましょう
一度書けばクッソ面倒な環境構築を何度もやらんで済む...と信じるんだッ
※Winget使う意味は更新簡単+ある程度初期設定イジれる辺りなんで
 そのへん諦めるなら使わなくても別に問題なしデス

メモ

とりあえず一般的なアプリのインストール用yamlはこんなですかねー
見つからないアプリはScoopかポータブル版でどーにかするしか無さ気です
まぁランチャー全部登録すんの面倒だろーし元々そんな感じの作業は必要だろ

properties:
  configurationVersion: 0.2.0
  resources:
    # PowerToys
    - resource: Microsoft.WinGet.DSC/WinGetPackage
      id: installPowerToys
      directives:
        description: Install PowerToys
        allowPrerelease: true
      settings:
        id: Microsoft.PowerToys
        source: winget
    - resource: Microsoft.PowerToys.Configure/PowerToysConfigure
      dependsOn:
        - installPowerToys
      directives:
        description: Configure PowerToys
      settings:
        Awake:
          Enabled: true
          KeepDisplayOn: true
          Mode: INDEFINITE

    # 7zip
    - resource: Microsoft.WinGet.DSC/WinGetPackage
      id: install7zip
      directives:
        description: Install 7zip
      settings:
        id: 7zip.7zip
        source: winget

    # Chrome
    - resource: Microsoft.WinGet.DSC/WinGetPackage
      id: installChrome
      directives:
        description: Install Chrome
      settings:
        id: Google.Chrome
        source: winget

    # Thunderbird
    # - resource: Microsoft.WinGet.DSC/WinGetPackage
    #   id: installThunderbird
    #   directives:
    #     description: Install Thunderbird
    #   settings:
    #     id: Mozilla.Thunderbird
    #     source: winget

    # WinSCP
    - resource: Microsoft.WinGet.DSC/WinGetPackage
      id: installWinSCP
      directives:
        description: Install WinSCP
      settings:
        id: WinSCP.WinSCP
        source: winget

    # IP Messenger
    # - resource: Microsoft.WinGet.DSC/WinGetPackage
    #   id: installIPMsg
    #   directives:
    #     description: Install IP Messenger
    #   settings:
    #     id: FastCopy.IPMsg
    #     source: winget

    # Zoom
    # - resource: Microsoft.WinGet.DSC/WinGetPackage
    #   id: installZoom
    #   directives:
    #     description: Install Zoom
    #     allowPrerelease: true
    #   settings:
    #     id:     Zoom.Zoom
    #     source: winget

    # MPC-BE
    - resource: Microsoft.WinGet.DSC/WinGetPackage
      id: installMPCBE
      directives:
        description: Install MPC-BE
        allowPrerelease: true
      settings:
        id:     MPC-BE.MPC-BE
        source: winget

    # K-LiteCodecPack
    - resource: Microsoft.WinGet.DSC/WinGetPackage
      id: installKLiteCodecPack
      directives:
        description: Install K-LiteCodecPack
        allowPrerelease: true
      settings:
        id:     CodecGuide.K-LiteCodecPack.Full
        source: winget

    # KeePassXC
    # - resource: Microsoft.WinGet.DSC/WinGetPackage
    #   id: installKeePassXC
    #   directives:
    #     description: Install KeePassXC
    #     allowPrerelease: true
    #   settings:
    #     id:     KeePassXCTeam.KeePassXC
    #     source: winget

    # LibreOffice
    - resource: Microsoft.WinGet.DSC/WinGetPackage
      id: installLibreOffice
      directives:
        description: Install LibreOffice
        allowPrerelease: true
      settings:
        id:     TheDocumentFoundation.LibreOffice
        source: winget

    # AcrobatReader
    - resource: Microsoft.WinGet.DSC/WinGetPackage
      id: installAcrobatReader
      directives:
        description: Install Acrobat Reader
        allowPrerelease: true
      settings:
        id:     Adobe.Acrobat.Reader.64-bit
        source: winget

    # PDFTKBuilder
    # - resource: Microsoft.WinGet.DSC/WinGetPackage
    #   id: installPDFTKBuilder
    #   directives:
    #     description: Install PDFTKBuilder
    #     allowPrerelease: true
    #   settings:
    #     id:     AngusJohnson.PDFTKBuilder
    #     source: winget

    # Pandoc
    - resource: Microsoft.WinGet.DSC/WinGetPackage
      id: installPandoc
      directives:
        description: Install Pandoc
        allowPrerelease: true
      settings:
        id:     JohnMacFarlane.Pandoc
        source: winget

    # ImageMagick
    # - resource: Microsoft.WinGet.DSC/WinGetPackage
    #   id: installImageMagick
    #   directives:
    #     description: Install ImageMagick
    #     allowPrerelease: true
    #   settings:
    #     id:     ImageMagick.ImageMagick.Q16
    #     source: winget

    # Screenpresso
    - resource: Microsoft.WinGet.DSC/WinGetPackage
      id: installScreenpresso
      directives:
        description: Install Screenpresso
        allowPrerelease: true
      settings:
        id:     Learnpulse.Screenpresso
        source: winget

    # ScreenToGif
    - resource: Microsoft.WinGet.DSC/WinGetPackage
      id: installScreenToGif
      directives:
        description: Install ScreenToGif
        allowPrerelease: true
      settings:
        id:     NickeManarin.ScreenToGif
        source: winget

    # OneDrive
    - resource: Microsoft.WinGet.DSC/WinGetPackage
      id: installOneDrive
      directives:
        description: Install OneDrive
        allowPrerelease: true
      settings:
        id:     Microsoft.OneDrive
        source: winget

    # GoogleDrive
    - resource: Microsoft.WinGet.DSC/WinGetPackage
      id: installGoogleDrive
      directives:
        description: Install GoogleDrive
        allowPrerelease: true
      settings:
        id:     Google.Drive
        source: winget

    # Dropbox
    - resource: Microsoft.WinGet.DSC/WinGetPackage
      id: installDropbox
      directives:
        description: Install Dropbox
        allowPrerelease: true
      settings:
        id:     Dropbox.Dropbox
        source: winget

あとは開発用なんだけど実はコッチはPlantUMLがねぇんですわー
もーそんなんじゃーDoxygenの効果半減だろがよー、めんどくせーなー
つーかImageMagickは管理権限必須な上にGhostScriptなんか存在もしてねぇし...
って有り様ですんで多分scoopとの併用は今のところ必須なよーです
chocolatelyは...流石に3つ併用ってなるとメンドイし...ゴメンなさい?
でも結局Nugetはどっかで使うだろーなぁ...うーん...UniGetUI...?えー?マジですかー

properties:
  configurationVersion: 0.2.0
  resources:
    # #########################################################################

    # PowerShell
    - resource: Microsoft.WinGet.DSC/WinGetPackage
      id: installPowerShell
      directives:
        description: Install PowerShell
      settings:
        id: Microsoft.PowerShell
        source: winget

    # WindowsTerminal
    - resource: Microsoft.WinGet.DSC/WinGetPackage
      id: installWindowsTerminal
      directives:
        description: Install WindowsTerminal
      settings:
        id: Microsoft.WindowsTerminal
        source: winget

    # TeraTerm
    - resource: Microsoft.WinGet.DSC/WinGetPackage
      id: installTeraTerm
      directives:
        description: Install TeraTerm
      settings:
        id: TeraTermProject.teraterm
        source: winget

    # #########################################################################

    # WinMerge
    - resource: Microsoft.WinGet.DSC/WinGetPackage
      id: installWinMerge
      directives:
        description: Install WinMerge
      settings:
        id: WinMerge.WinMerge
        source: winget

    # Git
    - resource: Microsoft.WinGet.DSC/WinGetPackage
      id: installGit
      directives:
        description: Install Git
      settings:
        id: Git.Git
        source: winget

    # Sourcetree
    - resource: Microsoft.WinGet.DSC/WinGetPackage
      id: installSourceTree
      directives:
        description: Install Sourcetree
      settings:
        id: Atlassian.Sourcetree
        source: winget

    # Visual Studio Code
    - resource: Microsoft.WinGet.DSC/WinGetPackage
      id: installVSCode
      directives:
        description: Install Visual Studio Code
      settings:
        id: Microsoft.VisualStudioCode
        source: winget

    # Doxygen
    - resource: Microsoft.WinGet.DSC/WinGetPackage
      id: installDoxygen
      directives:
        description: Install Doxygen
      settings:
        id: DimitriVanHeesch.Doxygen
        source: winget

    # DevToys
    - resource: Microsoft.WinGet.DSC/WinGetPackage
      id: installDevToys
      directives:
        description: Install DevToys
      settings:
        id: DevToys-app.DevToys
        source: winget

    # #########################################################################

    # VisualStudio 2022 Community
    - resource: Microsoft.WinGet.DSC/WinGetPackage
      id: installVisualStudio2022Community
      directives:
        description: Install VisualStudio 2022 Community
      settings:
        id: Microsoft.VisualStudio.2022.Community
        source: winget
    - resource: Microsoft.VisualStudio.DSC/VSComponents
      dependsOn:
        - installVisualStudio2022Community
      directives:
        description: Install required VS workloads
        allowPrerelease: true
      settings:
        productId: Microsoft.VisualStudio.Product.Community
        channelId: VisualStudio.17.Release
        components:
          - Microsoft.VisualStudio.Workload.ManagedDesktop

    # AndroidStudio
    # - resource: Microsoft.WinGet.DSC/WinGetPackage
    #   id: installAndroidStudio
    #   directives:
    #     description: Install AndroidStudio
    #   settings:
    #     id: Google.AndroidStudio
    #     source: winget

    # Arduino IDE
    # - resource: Microsoft.WinGet.DSC/WinGetPackage
    #   id: installArduino
    #   directives:
    #     description: Install Arduino IDE
    #   settings:
    #     id: ArduinoSA.IDE.rc
    #     source: winget

    # #########################################################################

    # Wireshark
    - resource: Microsoft.WinGet.DSC/WinGetPackage
      id: installWireshark
      directives:
        description: Install Wireshark
      settings:
        id: WiresharkFoundation.Wireshark
        source: winget

Discussion