用于自动安装模块的 Power Shell 脚本

问题描述 投票:0回答:1

加载所需的程序集

添加类型 -AssemblyName System.Windows.Forms

添加类型 -AssemblyName System.Drawing

函数显示消息框 {

param (

    [string]$message,

    [string]$title

)

[System.Windows.Forms.MessageBox]::Show($message, $title)

}

函数运行查询{

param (

    [string]$selectedDate

)



$queries = @(

    "SELECT * FROM your_table WHERE date_column = TO_DATE('$selectedDate', 'YYYY-MM-DD')"

) * 10



$connectionString = "User Id=username;Password=password;Data Source=hostname/service_name"



try {

    $connection = New-Object Oracle.ManagedDataAccess.Client.OracleConnection($connectionString)

    $connection.Open()



    $excel = Open-ExcelPackage -Path 'your_excel_file.xlsx'

    $worksheet = $excel.Workbook.Worksheets[2] # Sheet number 3 (index 2)

    $worksheet.Cells.Clear()



    $progressBar.Maximum = $queries.Length

    $progressBar.Value = 0



    for ($i = 0; $i -lt $queries.Length; $i++) {

        $statusLabel.Text = "Status: Running query $($i + 1)"

        [System.Windows.Forms.Application]::DoEvents()



        $command = $connection.CreateCommand()

        $command.CommandText = $queries[$i]

        $reader = $command.ExecuteReader()



        $dataTable = New-Object System.Data.DataTable

        $dataTable.Load($reader)



        $rowIndex = $worksheet.Dimension?.End.Row + 1

        if ($null -eq $rowIndex) { $rowIndex = 1 }



        Export-Excel -Worksheet $worksheet -Append -InputObject $dataTable -StartRow $rowIndex -StartColumn 1 -TableName "QueryData"



        $progressBar.Value = $i + 1

    }



    Close-ExcelPackage -ExcelPackage $excel



    $connection.Close()

    Show-MessageBox -message "All queries executed successfully." -title "Success"

} catch {

    Show-MessageBox -message "An error occurred: $_" -title "Failure"

}

}

创建表格

$form = 新对象 System.Windows.Forms.Form

$form.Text = "Oracle 查询执行器"

$form.Size = 新对象 System.Drawing.Size(400, 300)

$form.StartPosition = "CenterScreen"

创建日期选择器

$datePicker = 新对象 System.Windows.Forms.DateTimePicker

$datePicker.Format = [System.Windows.Forms.DateTimePickerFormat]::Short

$datePicker.Location = 新对象 System.Drawing.Point(10, 10)

$form.Controls.Add($datePicker)

创建进度条

$progressBar = 新对象 System.Windows.Forms.ProgressBar

$progressBar.Location = 新对象 System.Drawing.Point(10, 50)

$progressBar.Size = 新对象 System.Drawing.Size(360, 30)

$form.Controls.Add($progressBar)

创建状态标签

$statusLabel = 新对象 System.Windows.Forms.Label

$statusLabel.Text =“状态:等待开始”

$statusLabel.Location = 新对象 System.Drawing.Point(10, 90)

$form.Controls.Add($statusLabel)

创建按钮

$runButton = 新对象 System.Windows.Forms.Button

$runButton.Text = "运行查询"

$runButton.Location = 新对象 System.Drawing.Point(10, 130)

$runButton.Add_Click({

$selectedDate = $datePicker.Value.ToString("yyyy-MM-dd")

Run-Queries -selectedDate $selectedDate

})

$form.Controls.Add($runButton)

显示表格

$form.ShowDialog()

我想安装模块,但我无法做到。

powershell automation module
1个回答
0
投票
# Database connection parameters
$server = "your_database_server"
$database = "your_database_name"
$username = "your_username"
$password = "your_password"

# Construct the connection string
$connectionString = "Server=$server;Database=$database;User Id=$username;Password=$password;"

# Define the query to execute
$query = "SELECT SYSDATE FROM dual;"

# Create a connection object
$connection = New-Object System.Data.SqlClient.SqlConnection
$connection.ConnectionString = $connectionString

# Open the connection
$connection.Open()

# Create a command object
$command = $connection.CreateCommand()
$command.CommandText = $query

# Execute the query and store the results
$results = $command.ExecuteReader()

# Loop through the results (in this case, we expect only one result)
while ($results.Read()) {
    # Print the result (assuming there is only one column in the result set)
    Write-Host "Current date from database: $($results.GetValue(0))"
}

# Close the connection and dispose of objects
$connection.Close()
$connection.Dispose()
© www.soinside.com 2019 - 2024. All rights reserved.