もちろん、お手伝いします。指定された要件に基づいて、PowerShellアプリケーションを作成します。このアプリケーションは指定のパスからファイルやフォルダをコピーし、データベースで管理されたパスの一部を部署コードと日付で変更します。

スクリプトの概要

  1. データベースからコピー元とコピー先のパスを取得します。
  2. ユーザーから部署コードと日付を入力として受け取ります。
  3. パスの一部を変更してファイルやフォルダをコピーします。

スクリプト

# パラメータを取得
param(
    [string]$departmentCode,
    [string]$date
)

# DBからコピー元とコピー先のパスを取得
$connectionString = "Data Source=your_server;Initial Catalog=your_database;Integrated Security=True"
$connection = New-Object System.Data.SqlClient.SqlConnection
$connection.ConnectionString = $connectionString
$connection.Open()
$command = $connection.CreateCommand()
$command.CommandText = "SELECT SourcePath, DestinationPath FROM Paths WHERE DepartmentCode = '$departmentCode'"
$reader = $command.ExecuteReader()

# コピー処理
while ($reader.Read()) {
    $sourcePath = $reader["SourcePath"].Replace("{date}", $date)
    $destinationPath = $reader["DestinationPath"].Replace("{date}", $date)
    Copy-Item -Path $sourcePath -Destination $destinationPath -Recurse
}

$connection.Close()

このコードはデータベースから指定の部署コードに関連するパスを取得し、指定された日付でパスを変更してコピーします。コード内の your_serveryour_database は実際の値に置き換える必要があります。

さらに詳しい設定やカスタマイズが必要であれば、お知らせください。

もちろん、データベースとしてMS Accessを使用することが可能です。以下のコードは、指定された部署コードに基づいてMS Accessデータベースからコピー元とコピー先のパスを取得し、指定された日付でパスを変更してコピーします。

# パラメータを取得
param(
    [string]$departmentCode,
    [string]$date
)

# MS Accessの接続文字列
$accessPath = "C:\\path\\to\\your_database.accdb"
$connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=$accessPath;Persist Security Info=False;"

# 接続の開始
$connection = New-Object -TypeName System.Data.OleDb.OleDbConnection -ArgumentList $connectionString
$connection.Open()

# コマンドの準備
$command = $connection.CreateCommand()
$command.CommandText = "SELECT SourcePath, DestinationPath FROM Paths WHERE DepartmentCode = ?"
$command.Parameters.Add((New-Object Data.OleDb.OleDbParameter -ArgumentList ("DepartmentCode", $departmentCode)))

# コピー処理
$reader = $command.ExecuteReader()
while ($reader.Read()) {
    $sourcePath = $reader["SourcePath"].Replace("{date}", $date)
    $destinationPath = $reader["DestinationPath"].Replace("{date}", $date)
    Copy-Item -Path $sourcePath -Destination $destinationPath -Recurse
}

# 接続の終了
$connection.Close()

このコードは、指定されたパスの your_database.accdb にあるMS Accessデータベースに接続します。このデータベースから部署コードに基づいてパスを取得し、日付でパスを変更してコピーします。

$accessPath 変数をデータベースの実際のパスに変更してください。また、必要に応じてテーブル名やクエリを変更することができます。何か他にお手伝いできることがあれば、お知らせください。

勿論、クラス化によりコードをより構造化し、再利用しやすくします。以下は、指定した要件に基づいてPowerShellでクラス化されたコードです。

class FileCopyManager {
    [string]$departmentCode
    [string]$date
    [string]$accessPath

    FileCopyManager([string]$departmentCode, [string]$date, [string]$accessPath) {
        $this.departmentCode = $departmentCode
        $this.date = $date
        $this.accessPath = $accessPath
    }

    [void]CopyFiles() {
        $connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=$($this.accessPath);Persist Security Info=False;"
        $connection = New-Object -TypeName System.Data.OleDb.OleDbConnection -ArgumentList $connectionString
        $connection.Open()
        $command = $connection.CreateCommand()
        $command.CommandText = "SELECT SourcePath, DestinationPath FROM Paths WHERE DepartmentCode = ?"
        $command.Parameters.Add((New-Object Data.OleDb.OleDbParameter -ArgumentList ("DepartmentCode", $this.departmentCode)))

        $reader = $command.ExecuteReader()
        while ($reader.Read()) {
            $sourcePath = $reader["SourcePath"].Replace("{date}", $this.date)
            $destinationPath = $reader["DestinationPath"].Replace("{date}", $this.date)
            Copy-Item -Path $sourcePath -Destination $destinationPath -Recurse
        }

        $connection.Close()
    }
}

# 使用例
$departmentCode = "HR"
$date = "20210817"
$accessPath = "C:\\path\\to\\your_database.accdb"
$copyManager = [FileCopyManager]::new($departmentCode, $date, $accessPath)
$copyManager.CopyFiles()

このコードでは、FileCopyManagerクラスが作成され、コピー処理がそのクラスのメソッドとして定義されています。クラスのコンストラクタは部署コード、日付、およびAccessデータベースへのパスを受け取ります。

使用例も提供していますので、必要に応じてこれらの値を変更してください。何か他にお手伝いできることがあれば、お知らせください。

もちろん、ODBC接続を使用してMS Accessデータベースに接続することができます。以下は、ODBCを使用してデータベースに接続するクラス化されたPowerShellスクリプトです。

class FileCopyManager {
    [string]$departmentCode
    [string]$date
    [string]$accessPath

    FileCopyManager([string]$departmentCode, [string]$date, [string]$accessPath) {
        $this.departmentCode = $departmentCode
        $this.date = $date
        $this.accessPath = $accessPath
    }

    [void]CopyFiles() {
        $connectionString = "Driver={Microsoft Access Driver (*.mdb, *.accdb)};Dbq=$($this.accessPath);"
        $connection = New-Object -TypeName System.Data.Odbc.OdbcConnection -ArgumentList $connectionString
        $connection.Open()
        $command = $connection.CreateCommand()
        $command.CommandText = "SELECT SourcePath, DestinationPath FROM Paths WHERE DepartmentCode = ?"
        $parameter = New-Object -TypeName System.Data.Odbc.OdbcParameter
        $parameter.Value = $this.departmentCode
        $command.Parameters.Add($parameter)

        $reader = $command.ExecuteReader()
        while ($reader.Read()) {
            $sourcePath = $reader["SourcePath"].Replace("{date}", $this.date)
            $destinationPath = $reader["DestinationPath"].Replace("{date}", $this.date)
            Copy-Item -Path $sourcePath -Destination $destinationPath -Recurse
        }

        $connection.Close()
    }
}

# 使用例
$departmentCode = "HR"
$date = "20210817"
$accessPath = "C:\\path\\to\\your_database.accdb"
$copyManager = [FileCopyManager]::new($departmentCode, $date, $accessPath)
$copyManager.CopyFiles()

このコードでは、ODBCドライバを使用してMS Accessデータベースに接続します。必要に応じて接続文字列、クエリ、パラメータを調整できます。