LoginSignup
4
6

More than 5 years have passed since last update.

ひとまずPowerShellコマンドをバッチファイルで出力できるよう対応する

Last updated at Posted at 2017-12-26

【本記事が役に立つ可能性がある場合】
・職場環境上、PowerShellスクリプトの実行ポリシーの変更が容易ではない
・パラメータ取得の自動化をしたい

PowerShellスクリプトは実行ポリシーがあり、古いWindowsのOSでは
デフォルトが「Restricted」という使用禁止の状態となっています。

ただWindowsのdiskpartコマンド等、
通常のコマンドでは期待した出力結果がでないこともあり、
PowerShellのコマンド使いたい。

そんな時はやっぱりバッチファイルを使います。

そして下記のようにPowerShellと頭につけて
PowerShellコマンドを書いてあげれば
バッチファイルで実行されます。

しっかりスクリプトを作成し運用自動化を図る場合は
ポリシー設定の変更から進めるべきです。

以下は参考です。
PowerShellコマンドの冒頭に「Powershell」を付与すればOKです。
-Commandや-NoProfileなどオプション指定がなくても起動します。
ちなみにFormat-Listするためにパイプ処理していますが、
"で囲ってあげればOKです。

get.bat
@echo off

echo 【PowerShellの実行ポリシー】> out2.txt
Powershell Get-ExecutionPolicy >> out2.txt
echo ==================================================================== >> out2.txt
Powershell "Get-PSDrive -PSProvider FileSystem | Format-List" >> out2.txt

出力ログです。

out2.JPG

下記は上記のバッチファイルでやりたいことの
PowerShellスクリプトです。ログ名は変えてます。

get.ps1
Write-Output  "【PowerShellの実行ポリシー】" > out.txt
Get-ExecutionPolicy >> out.txt
Write-Output =========================================================== >> out.txt
Get-PSDrive -PSProvider FileSystem >> out.txt
以上ですm(__)m
4
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
6