以下の内容はhttps://tech.guitarrapc.com/entry/2013/05/02/220517より取得しました。


PowerShellのModuleインストール用スクリプトを作ってみた

自分で作ったスクリプトや、GitHubでお知恵を拝借したスクリプトには、「Module Pathにして便利に使ってね!」というものが少なくありません。 というか、一々スクリプトをたたいてなどいられません。

しかもPowerShell 3.0では、Import Moduleを一々しなくても、Module Pathを自動で確認します。 ということは、Module Pathに移動させたいスクリプトを移動させる ( = 甘々に解釈してインストールとでも呼称しておきます) のは、サクッと終わらせたいものです。 移動スクリプトの時点でExecution-Policyの確認も行えるというのは一石二鳥でしょうか。

ということで、公開します。

GitHub

以下にps1と起動用のバッチスクリプトを置いておきます。

PowerShellUtil/Install-PowershellModule | GitHub

利用方法

簡単です。バッチダブルクリック一発!

  • Moduleパスにコピーしたいファイルを指定された(指定がなければ作りたい)フォルダにまとめる
  • install.ps1とinstall.batを同フォルダに設置
  • install.batを実行
  • Module Pathの進捗が表示されるので、コピーされたことを画面表示で確認
  • PowerShell 3.0なら、ISEなどを開いてGet-Moduleやfunctionを入力すれば存在する

スクリプト全文

GitHubを見ることすら面倒な方……というより、軽く参考までに。 TechNet参考にしてみようと思ったのですが、元々のは、VistaとかXPとか意味わからない言葉が書いてあったので、ごりっと削除しています。

結構動作が気にくわなかったので、インストーラーのあるパスのフォルダ名でモジュールフォルダを作り、インストーラーと同じフォルダ・より深い階層のps1やpsm1/psd1を、モジュールパスに全てコピーしています。

個別フォルダの対応はしていないので、欲しい方はリクエスト下さい。 (私はいらない予感)

なお、PowerShell Moduleのルートパスは$env:userProfile\documents\WindowsPowerShell\Modulesです。 (Windows Vista移行は統一)

#Requires -Version 2.0

[CmdletBinding()]
Param(
  [Parameter(
  Position=0,
  Mandatory=$false,
  ValueFromPipeline=$True)]
  [string]
  $path=$(Split-Path $PSCommandPath -Parent)
)

Function Get-OperatingSystemVersion{
  (Get-WmiObject -Class Win32_OperatingSystem).Version
}

Function Test-ModulePath{

  $Path = "$env:userProfile\documents\WindowsPowerShell\Modules"

  Write-Verbose "Checking Module Home."
  if ([int](Get-OperatingSystemVersion).substring(0,1) -ge 6)
  {
    if(-not(Test-Path -path $Path))
    {
      Write-Verbose "Creating Module Home at $path"
      New-Item -Path $Path -itemtype directory > $null
    }
    else
    {
      Write-Verbose "$path found. Never create Module Direcoty and end Test-ModulePath function."
    }
  }

}

Function Copy-Module{

  param(
  [string]
  $name
  )

  $UserPath = $env:PSModulePath.split(";")[0]
  $global:ModulePath = Join-Path -path $userPath -childpath $(Get-Item $PSCommandPath).Directory.Name

  If(-not(Test-Path -path $modulePath))
  {
    Write-Verbose "Creating Custom Module Firectory at $ModulePath"
    New-Item -path $ModulePath -itemtype directory > $null

    try
    {
      Write-Verbose "Copying modules into $ModulePath"
      Copy-item -path $name -destination $ModulePath > $null
    }
    catch
    {
      Write-Warning "Copying error, Please check failed item. If you can, please copy it to $ModulePath"
    }
    finally
    {
    }
  }
  Else
  {
    Write-Verbose "Copying modules into $ModulePath"
    try
    {
      Copy-item -path $name -destination $ModulePath > $null
    }
    catch
    {
      Write-Warning "Copying error, Please check failed item. If you can, please copy it to $ModulePath"
    }
    finally
    {
    }
  }
}

Write-Host "Starting checking Module path and Copy PowerShell Scripts job." -ForegroundColor Green

Write-Host "Checking Module Path existing." -ForegroundColor Green
Test-ModulePath

Write-Host "Copying Modules to Module Path." -ForegroundColor Green
Get-ChildItem -Path $path -File -Recurse `
  | where {$_.Extension -eq ".ps1" -or ".psm1" -or ".psd1"} `
  | Foreach-Object {
    Write-Verbose "Copying $($_.fullName) to $path ."
    Copy-Module -name $_.fullName
    }

Write-Host "Installation finished. Your Module Path is $ModulePath" -ForegroundColor Green

参考サイト

時代はWindows 8 / Blue? ですよねー。 Hey, Scripting Guy! How Can I Install Windows PowerShell Modules on Multiple Users' Computers?

続き

上記のバグを修正しています。 GitHubには最新版をおいていますが、一応。 PowerShell の Moduleインストール用スクリプトを作ってみた (続き)




以上の内容はhttps://tech.guitarrapc.com/entry/2013/05/02/220517より取得しました。
このページはhttp://font.textar.tv/のウェブフォントを使用してます

不具合報告/要望等はこちらへお願いします。
モバイルやる夫Viewer Ver0.14