忍者ブログ

◆当blogは、Linuxサーバ構築する際の実際の設定手順を個人的メモとして記載しております。LinuC試験の役に立つ情報があるかも…?

LinuC(Linux技術者認定資格)&リナックスサーバ構築設定事例

   
カテゴリー「PowerShell」の記事一覧

[PR]

×

[PR]上記の広告は3ヶ月以上新規記事投稿のないブログに表示されています。新しい記事を書く事で広告が消えます。

【PowerShell】ログ取得

#-----------------------------
# ログファイル取得開始
$LogFile = Get-Date -Format "LOG_yyyy_MMdd_HHmmss"
Start-Transcript C:\PS_Logs\$LogFile.log -Append | Out-Null
#-----------------------------
# 強制上書きオプション
Copy-Item -Path C:\Temp999\pstest.txt -Destination C:\Temp999\copy.txt -Force
Copy-Item -Path C:\Temp999\pstest.txt -Destination C:\Temp999\copy.txt -Force -Recurse
Copy-Item -Path C:\Temp999\pstest.txt -Destination C:\Temp999\copy.txt -Force -PassThru
#-----------------------------
# ログファイル取得終了
Stop-Transcript | Out-Null
#-----------------------------
#標準出力とログ出力
$Output_File = "C:\tmp\History.txt"
echo "テスト" | Tee-Object $Output_File -Append
Get-Content $Output_File
PR

【PowerShell】アカウントの情報表示

★ユーザーアカウント表示(ローカル
Get-WmiObject Win32_UserAccount | ? { $_.LocalAccount -eq $true } | Format-table -autosize -wrap

★ユーザーアカウント表示(ドメイン

Get-WmiObject Win32_UserAccount | ? { $_.LocalAccount -eq $false } | Format-table -autosize -wrap

★グループ表示(ローカル

Get-WmiObject Win32_Group | ? { $_.LocalAccount -eq $true }

★グループ表示(ドメイン

Get-WmiObject Win32_Group | ? { $_.LocalAccount -eq $false }

★役割と機能をCSV形式に出力【おまけ】

Get-WindowsFeature | Export-Csv -encoding Default Feature.csv

【PowerShell】バッチから実行

■以下のようにバッチファイルに記述するとバッチからPowerShellが実行出来る
@echo off
PowerShell -command "Write-Output '--- --- --- --- --- --- --- --- --- --- --- --- --- ---'"
PowerShell -command "Write-Output '★★★ 開始 ★★★'"
PowerShell -command "Write-Output '--- --- --- --- --- --- --- --- --- --- --- --- --- ---'"
PowerShell -command "Write-Output ' '"
PowerShell -command "Write-Output '▲▼▲▼▲▼ ホスト名 ▲▼▲▼▲▼'"
PowerShell -command "hostname"
PowerShell -command "Write-Output '--- --- --- --- --- --- --- --- --- --- --- --- --- ---'"
PowerShell -command "Write-Output '▲▼▲▼▲▼ 日時 ▲▼▲▼▲▼'"
PowerShell -command "Get-Date -Format 'yyyy/MM/dd HH:mm'"
PowerShell -command "Write-Output '--- --- --- --- --- --- --- --- --- --- --- --- --- ---'"
PowerShell -command "Write-Output '▲▼▲▼▲▼ パッチ情報(降順) ▲▼▲▼▲▼'"
PowerShell -command "Get-HotFix | Where-Object HotFixID -match "KB" | Sort-Object -Descending InstalledOn"
PowerShell -command "Write-Output '--- --- --- --- --- --- --- --- --- --- --- --- --- ---'"
PowerShell -command "Write-Output '▲▼▲▼▲▼ 最近のOS起動の日時 ▲▼▲▼▲▼'"
PowerShell -command "Get-EventLog System | Where-Object {$_.InstanceId -eq "12" } | Select-Object -First 1"
PowerShell -command "Write-Output '--- --- --- --- --- --- --- --- --- --- --- --- --- ---'"
PowerShell -command "Write-Output '▲▼▲▼▲▼ システムイベント(エラー) ▲▼▲▼▲▼'"
PowerShell -command "Get-EventLog System | Where-Object {$_.EntryType -eq 'Error'} | Select-Object -First 50"
PowerShell -command "Write-Output '--- --- --- --- --- --- --- --- --- --- --- --- --- ---'"
pause
PowerShell -command "Write-Output '▲▼▲▼▲▼ システムイベント(警告) ▲▼▲▼▲▼'"
PowerShell -command "Get-EventLog System | Where-Object {$_.EntryType -eq 'Warning'} | Select-Object -First 50"
PowerShell -command "Write-Output '--- --- --- --- --- --- --- --- --- --- --- --- --- ---'"
pause
rem PowerShell -command "Write-Output '▲▼▲▼▲▼ アプリケーションイベント(エラー) ▲▼▲▼▲▼'"
rem PowerShell -command "Get-EventLog Application | Where-Object {$_.EntryType  -Like 'Error'} | Select-Object -First 30"
rem PowerShell -command "Write-Output '--- --- --- --- --- --- --- --- --- --- --- --- --- ---'"
rem pause
rem PowerShell -command "Write-Output '▲▼▲▼▲▼ アプリケーションイベント(警告) ▲▼▲▼▲▼'"
rem PowerShell -command "Get-EventLog Application | Where-Object {$_.EntryType  -Like 'Warning'} | Select-Object -First 30"
rem PowerShell -command "Write-Output '--- --- --- --- --- --- --- --- --- --- --- --- --- ---'"
rem pause
PowerShell -command "Write-Output ' '"
PowerShell -command "Write-Output '--- --- --- --- --- --- --- --- --- --- --- --- --- ---'"
PowerShell -command "Write-Output '★★★ 終了 ★★★'"
PowerShell -command "Write-Output '--- --- --- --- --- --- --- --- --- --- --- --- --- ---'"
pause

【PowerShell】フォルダ内容一覧表示

以下のようにバッチファイルからPowershellスクリプトを呼んで処理する場合の例
[_PowerShell_実行.bat] ←バッチファイル名
 @echo off
 echo .
 echo *********************************
 echo ★ Powershellスクリプトを実行 ★
 echo *********************************
 echo .
 @echo on
 powershell -ExecutionPolicy RemoteSigned -File .\_PowerShell_001.ps1
 powershell -ExecutionPolicy RemoteSigned -File .\_PowerShell_002.ps1
 @echo off
 echo .
 echo ▲▼▲▼▲▼▲ 実行完了 ▼▲▼▲▼▲▼
 echo .
 pause

[_PowerShell_001.ps1] ←スクリプトファイル名

 # ★カレントフォルダの中身を一覧表示(名前、更新日時)名前順にソート
 # テキストファイルに出力

 Get-ChildItem .\ -File -Recurse | Sort-Object FullName | Out-File -LiteralPath .\_XXX_LIST_01.txt

 Get-ChildItem .\ -Recurse | Sort-Object FullName | Format-Table -Property Attributes, LastWriteTime, Name, Mode, DirectoryName | Out-File -LiteralPath .\_XXX_LIST_02.txt -Encoding Default

 # CSVファイルに出力
 Get-ChildItem .\ -Recurse | Sort-Object FullName | Select-Object Attributes, LastWriteTime, Name, Mode, DirectoryName | Export-Csv -LiteralPath .\_XXX_LIST_File.csv -Encoding Default

 Get-ChildItem .\ -File -Recurse | Sort-Object FullName | Select-Object Name | Where-Object { ! $_.PSIsContainer } | Out-File -LiteralPath .\_XXX_Name_csv_File.csv

 Get-ChildItem .\ -File -Recurse | Sort-Object FullName | Select-Object LastWriteTime | Get-Date -Format "yyyy/M/d" | Out-File -LiteralPath .\_XXX_Date_csv_File.csv

 Get-ChildItem .\ -File -Recurse | Sort-Object FullName | Select-Object LastWriteTime | Get-Date -Format "HH:mm" | Out-File -LiteralPath .\_XXX_TIME_csv_File.csv
 
[_PowerShell_002.ps1] ←スクリプトファイル名
 cat .\_XXX_LIST_01.txt

 $LIST = Get-Content .\_XXX_LIST_01.txt | ForEach-Object { $_ -replace '\b0+\B' }

 $LIST | Out-File .\_XXX_LIST_01_MMDD.txt

 # デフォルトの文字コード表示

 [System.Text.Encoding]::Default

 # ディレクトリのみ抽出
 dir -Directory -Recurse | Select FullName > _XXX_Directory_Path.txt

 # 加工しやすいように出力
 Get-ChildItem .\ -File -Recurse | Sort-Object FullName | Select-Object LastWriteTime, Name, DirectoryName | Where-Object { ! $_.PSIsContainer } | Export-Csv -LiteralPath .\_Export_CSV_.csv -Encoding Default | Format-Table -AutoSize | Out-String -Width 4096

 Get-ChildItem .\ -File -Recurse | Sort-Object FullName | Select-Object LastWriteTime  | Where-Object { ! $_.PSIsContainer } | Export-Csv -LiteralPath .\_YYYYMMDD_hhmm_.txt -Encoding Default | Format-Table -AutoSize | Out-String -Width 4096


【PowerShell】コンソール出力をログに保存

PowerShellの実行結果(標準出力など) をログに保存する方法です。
(Linuxでいうところのscriptコマンド相当)
※このコマンドはISE環境では実行できません。


# ログ取得開始
Start-Transcript <ログファイル名>

# ログ取得終了
Stop-Transcript

【PowerShell】ローカルアカウント作成時に複数グループを設定する

ローカルアカウント作成時に複数グループを設定するサンプル作りました。

ファイル名:A2.ps1
# //変数格納//
  $computer = $Env:ComputerName
  $cdpath = (Get-Location).Path
# //text⇒CSV変換//
  $csv = import-csv -path $cdpath\A2.txt
# //テキスト情報の読込み//
  foreach($strUser in $csv)
  {
# //情報取得//
  $user = $struser.user
  $password = $struser.password
  $description =  $struser.description
  $group1 = $struser.group1
  $group2 = $struser.group2
# //初期化//
  Clear-Host
# //ユーザー作成//
  $ObjOU = [ADSI]"WinNT://$computer"
  $objUser = $objOU.Create("User", $user)
  $objUser.setpassword($password)
  $objUser.put("description",$description)
  $objUser.UserFlags = 0x10000 #パスワードを無期限に設定
  $objUser.SetInfo()
# //グループ設定 (1) //
  $objGroup = [ADSI]"WinNT://$computer/$group1"
  $objGroup.add("WinNT://$computer/$user")
  $objGroup.SetInfo()
# //グループ設定 (2) //
  $objGroup = [ADSI]"WinNT://$computer/$group2"
  $objGroup.add("WinNT://$computer/$user")
  $objGroup.SetInfo()
  }

##################################################
※読み込ませるデータに複数のグループを設定しておく
ファイル名:A2.txt
user,password,description,group1,group2
Test01,Password01,"説明文",administrators,users
Test02,Password01,"説明文",administrators,users
##################################################

【PowerShell】ローカルアカウントの設定変更スクリプト

既存しているローカルアカウントの設定を変更するサンプル作りました。

ファイル名:A1.ps1
# //変数格納//
  $computer = $Env:ComputerName
  $cdpath = (Get-Location).Path
# //text⇒CSV変換//
  $csv = import-csv -path $cdpath\A.txt

# //ファイル情報の読込み//

 foreach($strUser in $csv){
# //情報取得//
 $user = $struser.user
 $password = $struser.password
 $description =  $struser.description
# //初期化//
 Clear-Host
# //説明文とパスワードの設定変更//
 $objUser = [ADSI]"WinNT://$computer/$user"
 $objUser.setpassword($password)
 $objUser.description = $description
 $objUser.SetInfo()
}

【PowerShell】パワーシェルスクリプトの呼び出しBATファイル

BATファイルからパワーシェルスクリプトを実行するサンプル作りました。
※パワーシェルスクリプトをBATから呼び出す為には、
 管理者権限でコマンドプロンプトを実行し、
 実行ポリシーのセキュリティをRemotesignedに変更する等が必要なことに留意する
    (※Windows2012の場合、Remotesignedがデフォルト値の為、変更する必要なし)

ファイル名:A.bat
@echo off
echo 実行ポリシー保存
powershell Get-ExecutionPolicy
for /f "delims=" %%a in ('powershell -Command Get-ExecutionPolicy') do @set backupPolicy=%%a
echo 実行ポリシー変更
powershell Set-ExecutionPolicy Remotesigned
powershell Get-ExecutionPolicy
echo スクリプト実行
pause
powershell %CD%\A.ps1
echo 実行ポリシー戻し
powershell -Command Set-ExecutionPolicy %backupPolicy%
powershell Get-ExecutionPolicy
pause

【PowerShell】ローカルアカウント作成スクリプト

ローカルアカウント作成するスクリプトのサンプル作ってみました。
※パワーシェルを実行する為には色々な条件が必要な為、実際に上記スクリプトを実行する際には環境を整えてから実行してください。

ファイル名:A.ps1
# //変数格納//
  $computer = $Env:ComputerName
  $cdpath = (Get-Location).Path
# //text⇒CSV変換//
  $csv = import-csv -path $cdpath\A.txt

# //テキスト情報の読込み//
 foreach($strUser in $csv)
{
# //情報取得//
    $user = $struser.user
    $password = $struser.password
    $description =  $struser.description
    $group = $struser.group
# //初期化//
    Clear-Host

# //ユーザー作成//
    $ObjOU = [ADSI]"WinNT://$computer"
    $objUser = $objOU.Create("User", $user)
    $objUser.setpassword($password)
    $objUser.put("description",$description)
    $objUser.UserFlags = 0x10000 #パスワードを無期限に設定
    $objUser.SetInfo()
# //グループ設定//
    $objGroup = [ADSI]"WinNT://$computer/$group"
    $objGroup.add("WinNT://$computer/$user")
    $objGroup.SetInfo()
}

【PowerShell】読み込ませるテキストデータ

テストデータのサンプル作ってみました。
※文字コードは、Unicodeでテキストを保存する or 変換しておかないと文字化けするのに注意

ファイル名:A.txt
user,password,description,group
Test01,Password01,"説明文01",administrators
Test02,Password01,"説明文02",administrators

更新日付

05 2025/06 07
S M T W T F S
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30

RECOMMEND

プロフィール

HN:
Account
HP:
性別:
非公開
職業:
--- NODATA ---
趣味:
--- NODATA ---
自己紹介:
◆当blogは、Linuxサーバ構築する際の実際の設定手順を個人的メモとして記載しております。LinuC試験の役に立つ情報があるかも…?

リンク

 | HOME | 
Copyright ©  -- LinuC(Linux技術者認定資格)&リナックスサーバ構築設定事例 --  All Rights Reserved
Design by CriCri / Photo by Melonenmann / powered by NINJA TOOLS / 忍者ブログ / [PR]