{"id":675,"date":"2025-09-11T17:56:25","date_gmt":"2025-09-11T08:56:25","guid":{"rendered":"https:\/\/www.flumen-jp.com\/wordpress\/?p=675"},"modified":"2025-09-16T23:09:36","modified_gmt":"2025-09-16T14:09:36","slug":"powershell","status":"publish","type":"post","link":"https:\/\/www.flumen-jp.com\/wordpress\/index.php\/2025\/09\/11\/powershell\/","title":{"rendered":"PowerShell \u306e\u304a\u52c9\u5f37"},"content":{"rendered":"\n<p>\u3000\u30b3\u30de\u30f3\u30c9\u30d7\u30ed\u30f3\u30d7\u30c8\u3067\u51fa\u6765\u306a\u3044\u3053\u3068\u3092\u3059\u308b\u305f\u3081\u306b\u3001PowerShell \u306e\u304a\u4e16\u8a71\u306b\u306a\u308b\u3053\u3068\u304c\u3042\u308b\u3002Windows \u306e\u6a19\u6e96\u6a5f\u80fd\u306a\u306e\u3067\u30e6\u30fc\u30b6\u30fc\u3092\u9078\u3070\u306a\u3044\u306e\u304c\u5229\u70b9\u3002<br>\u3000\u305f\u3060\u3001\u3053\u3046\u3044\u3063\u305f\u30cb\u30fc\u30ba\u304c\u983b\u7e41\u306b\u3042\u308b\u8a33\u3067\u306a\u3044\u306e\u3068\u3001\u7d50\u69cb\u300c\u30af\u30bb\u5f37\u300d\u306a\u8a00\u8a9e\u306a\u306e\u3067\u3001\u3057\u3070\u3089\u304f\u4f7f\u308f\u306a\u3044\u3068\u3059\u3063\u304b\u308a\u5fd8\u308c\u3066\u3057\u307e\u3046\u3002<br>\u3000\u3068\u3044\u3046\u8a33\u3067\u5099\u5fd8\u306e\u305f\u3081\u3001Excel \u306e\u30de\u30af\u30ed\u306e\u30a4\u30f3\u30dd\u30fc\u30c8\u3001\u30a8\u30ad\u30b9\u30dd\u30fc\u30c8\u3092\u884c\u3046\u30b9\u30af\u30ea\u30d7\u30c8\u3092\u66f8\u3044\u3066\u307f\u305f\u3002\u3053\u3046\u3059\u308b\u3068\u3001Git \u3067\u30de\u30af\u30ed\u306e\u30d0\u30fc\u30b8\u30e7\u30f3\u7ba1\u7406\u304c\u51fa\u6765\u308b\u3088\u3046\u306b\u306a\u308b\u3002<\/p>\n\n\n\n<p>\u3000\u5148\u305a\u306f\u30a8\u30ad\u30b9\u30dd\u30fc\u30c8\u3002\u3053\u3061\u3089\u306f\u6bd4\u8f03\u7684\u697d\u306b\u5b8c\u6210\u3002<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-bash\" data-lang=\"Bash\"><code>function Usage() {\n    Write-Host &quot;`n Export VBComponents`n&quot;\n    Write-Host &quot;  Usage : export (-h | foo.xlsm)`n&quot;\n    exit 0\n}\n\nif ($($Args.Count) -eq 1) {\n    $arg = $Args[0]\n    if ($arg -eq &quot;-h&quot;) { Usage } \n    else {\n        if ($arg -notlike &quot;*.xlsm&quot;) {\n            Write-Host &quot;`n ERROR : invalid file type&quot;\n            exit 1\n        }\n        if ((Test-Path -Path $arg)) {\n            $workbookPath = Resolve-Path -Path $arg\n        } else {\n            Write-Host &quot;`n ERROR : $arg not found&quot;\n            exit 1\n\n        }\n    }\n} else { Usage }\n\n$excel = New-Object -ComObject Excel.Application\n$excel.Visible = $false\n$excel.DisplayAlerts = $false\n$excel.AutomationSecurity = 1 # msoAutomationSecurityLow\n\n$workbook = $excel.Workbooks.Open($workbookPath)\n\nforeach ($VBComponent in $workbook.VBProject.VBComponents) {\n    switch ($VBComponent.Type) {\n        1 {$ext = &quot;.bas&quot;}\n        2 {$ext = &quot;.cls&quot;}\n        3 {$ext = &quot;.frm&quot;}\n        default {$ext = &quot;.cls&quot;}\n    }\n    $VBComponent.Export($PWD.Path + &#39;\\&#39; + $VBComponent.Name + $ext)\n}\n\n$workbook.Close($false)\n\n$excel.Quit()\n\n[System.Runtime.InteropServices.Marshal]::ReleaseComObject($workbook) | Out-Null\n[System.Runtime.InteropServices.Marshal]::ReleaseComObject($excel) | Out-Null\n[System.GC]::Collect()\n[System.GC]::WaitForPendingFinalizers()<\/code><\/pre><\/div>\n\n\n\n<p>\u3000\u6b21\u3044\u3067\u3001\u30a4\u30f3\u30dd\u30fc\u30c8\u3002Excel \u306e\u30a8\u30ad\u30b9\u30dd\u30fc\u30c8\u306e\u4ed5\u69d8\u3068\u3057\u3066\u3001\u30ef\u30fc\u30af\u30b7\u30fc\u30c8\uff08Thisworkbook \u3092\u542b\u3080\uff09\u306e VBA \u306f\u62e1\u5f35\u5b50\u306b\u300c.cls\u300d\u3092\u6301\u3064\u30d5\u30a1\u30a4\u30eb\u306b\u4fdd\u5b58\u3055\u308c\u308b\u3002\u3053\u308c\u306f\u300c\u30af\u30e9\u30b9\u300d\u306b\u5c5e\u3059\u308b\u30d5\u30a1\u30a4\u30eb\u3068\u30d0\u30c3\u30c6\u30a3\u30f3\u30b0\u3059\u308b\u3002\u3057\u304b\u3082\u3001\u3053\u308c\u3092\u30a4\u30f3\u30dd\u30fc\u30c8\u3059\u308b\u6642\u306f\u300c\u30af\u30e9\u30b9\u300d\u306e\u30e1\u30bf\u30c7\u30fc\u30bf\u3092\u9664\u3044\u3066\u53d6\u308a\u8fbc\u307e\u306a\u3051\u308c\u3070\u306a\u3089\u306a\u3044\u3002<br>\u3000\u3053\u306e\u8fba\u3092\u8003\u616e\u3057\u3066\u66f8\u3044\u305f\u30b3\u30fc\u30c9\u304c\u4ee5\u4e0b\u3002\u3061\u3087\u3063\u3068\u82e6\u52b4\u3002<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-bash\" data-lang=\"Bash\"><code>function Usage() {\n    Write-Host &quot;`n Import VBComponents`n&quot;\n    Write-Host &quot;  Usage : import (-h | foo.xlsm)`n&quot;\n    exit 0\n}\n\nfunction is_include($test, $items) {\n    foreach ($item in $items) {\n        if ($item -eq $test) {\n            return $True\n        }\n    }\n    return $False\n}\n\nfunction Import($item_name, $codeFilePath, $is_sheet) {\n\n    try {\n        if ($is_sheet) {\n            $lines = Get-Content -Path $codeFilePath\n            $match_1st = $false\n            $codeLines = @()\n            foreach ($line in $lines) {\n                $match = $line.Trim() -like &quot;Attribute*&quot;\n                if ((-not $match_1st) -and $match) {\n                    $match_1st = $True\n                }\n                if ((-not $match_1st) -or  $match) {\n                    continue\n                }\n                $codeLines += $line\n            }\n            $codeContent = $codeLines | Out-String\n            $item = $workbook.VBProject.VBComponents.Item($item_name)\n            $code = $item.CodeModule\n            $code.DeleteLines(1, $code.CountOfLines)\n            $code.AddFromString($codeContent)\n        } else {\n            $components = $workbook.VBProject.VBComponents\n            $item = $components.Item($item_name)\n            $components.Remove($item)\n            $components.Import($codeFilePath)\n        }\n        return $True\n    }\n    catch {\n        return $False\n    }\n}\n\nif ($($Args.Count) -eq 1) {\n    $arg = $Args[0]\n    if ($arg -eq &quot;-h&quot;) { Usage } \n    else {\n        if ($arg -notlike &quot;*.xlsm&quot;) {\n            Write-Host &quot;`n ERROR : invalid file type&quot;\n            exit 1\n        }\n        if ((Test-Path -Path $arg)) {\n            $workbookPath = Resolve-Path -Path $arg\n            $exist_wbk = $True\n        } else {\n            $currentPath = Get-Location\n            $workbookPath = Join-Path -Path $currentPath -ChildPath $arg\n            $exist_wbk = $False\n        }\n    }\n} else { Usage }\n\n$excel = New-Object -ComObject Excel.Application\n$excel.Visible = $false\n$excel.DisplayAlerts = $false\n$excel.AutomationSecurity = 1 # msoAutomationSecurityLow\nif ($exist_wbk) {\n    $workbook = $excel.Workbooks.Open($workbookPath)\n} else {\n    $workbook = $excel.Workbooks.Add()\n}\n\n$sheets = @()\nforeach ($sheet in $workbook.Sheets) {\n    $sheets += $sheet.CodeName\n}\n$sheets += &quot;Thisworkbook&quot;\n\nforeach ($file in (Get-ChildItem -Path &quot;.\\*&quot; `\n                                 -Include *.cls, *.bas, *.frm)) {\n    $module_name = $file.BaseName\n    if ($file.Extension -eq &quot;.cls&quot;) {\n        $include = is_include -test $module_name -items $sheets\n    } else {\n        $include = $False\n    }\n    $success = Import -item_name $module_name `\n                      -codeFilePath $file.FullName `\n                      -is_sheet $include\n    if (-not $success) {\n        Write-Host &quot;fail to import $module_name&quot;\n    }\n}\n\nif ($exist_wbk) {\n    $workbook.Save()\n} else {\n    $fileFormat = 52 # 52 : xlOpenXMLWorkbookMacroEnabled\n    $workbook.SaveAs($workbookPath, $fileFormat)\n}\n\n$workbook.Close($false)\n\n$excel.Quit()\n\n[System.Runtime.InteropServices.Marshal]::ReleaseComObject($workbook) | Out-Null\n[System.Runtime.InteropServices.Marshal]::ReleaseComObject($excel) | Out-Null\n[System.GC]::Collect()\n[System.GC]::WaitForPendingFinalizers()<\/code><\/pre><\/div>\n\n\n\n<p>\u3000\u305d\u308c\u305e\u308c\u3001PowerShell \u3092\u958b\u304f\u306e\u304c\u9762\u5012\u306a\u306e\u3067\u3001\u30d0\u30c3\u30c1\u30d5\u30a1\u30a4\u30eb\u304b\u3089\u8d77\u52d5\u3067\u304d\u308b\u3088\u3046\u306b\u3057\u3066\u304a\u304f\u3002<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-bash\" data-lang=\"Bash\"><code>@echo off\npowershell %~dp0export_vba.ps1 %*<\/code><\/pre><\/div>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-bash\" data-lang=\"Bash\"><code>@echo off\npowershell %~dp0import_vba.ps1 %*<\/code><\/pre><\/div>\n\n\n\n<p>\u3000\u9014\u4e2d\u3001AI \u306e\u52a9\u3051\u3082\u501f\u308a\u305f\u304c\u3001\u5f53\u65b9\u306e\u805e\u304d\u65b9\u304c\u60aa\u3044\u306e\u304b\u3001\u5148\u65b9\u306e\u77e5\u8b58\u30d9\u30fc\u30b9\u304c\u5c11\u306a\u3044\u306e\u304b\u3001\u5fae\u5999\u306a\u6240\u3067\u306f\u3042\u307e\u308a\u30a2\u30c6\u306b\u306a\u3089\u306a\u304b\u3063\u305f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u3000\u30b3\u30de\u30f3\u30c9\u30d7\u30ed\u30f3\u30d7\u30c8\u3067\u51fa\u6765\u306a\u3044\u3053\u3068\u3092\u3059\u308b\u305f\u3081\u306b\u3001PowerShell \u306e\u304a\u4e16\u8a71\u306b\u306a\u308b\u3053\u3068\u304c\u3042\u308b\u3002Windows \u306e\u6a19\u6e96\u6a5f\u80fd\u306a\u306e\u3067\u30e6\u30fc\u30b6\u30fc\u3092\u9078\u3070\u306a\u3044\u306e\u304c\u5229\u70b9\u3002\u3000\u305f\u3060\u3001\u3053\u3046\u3044\u3063\u305f\u30cb\u30fc\u30ba\u304c\u983b\u7e41\u306b\u3042\u308b\u8a33\u3067\u306a\u3044\u306e\u3068\u3001\u7d50\u69cb\u300c\u30af\u30bb\u5f37\u300d [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[58,57],"class_list":["post-675","post","type-post","status-publish","format-standard","hentry","category-it","tag-git","tag-powershell"],"_links":{"self":[{"href":"https:\/\/www.flumen-jp.com\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/675","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.flumen-jp.com\/wordpress\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.flumen-jp.com\/wordpress\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.flumen-jp.com\/wordpress\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.flumen-jp.com\/wordpress\/index.php\/wp-json\/wp\/v2\/comments?post=675"}],"version-history":[{"count":8,"href":"https:\/\/www.flumen-jp.com\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/675\/revisions"}],"predecessor-version":[{"id":683,"href":"https:\/\/www.flumen-jp.com\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/675\/revisions\/683"}],"wp:attachment":[{"href":"https:\/\/www.flumen-jp.com\/wordpress\/index.php\/wp-json\/wp\/v2\/media?parent=675"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.flumen-jp.com\/wordpress\/index.php\/wp-json\/wp\/v2\/categories?post=675"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.flumen-jp.com\/wordpress\/index.php\/wp-json\/wp\/v2\/tags?post=675"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}