Please see my Function-to-be below:
Function Query {
param (
[string]$query
[string]$server
[string]$dbase
[string]$user
[string]$pass
)
if ($user) {
$connstr = "Server={0};Database={1};User ID={2};Password={3};Trusted_Connection=False;Connect Timeout=15" -f $server, $dbase, $user, $pass
}
else {
$connstr = "Server={0};Database={1};Integrated Security=True;Connect Timeout=15" -f $server, $dbase
}
$conn.ConnectionString = $connstr
switch ($query.Split()[0]) {
"SELECT" {
$cmd = New-Object System.Data.SqlClient.SqlCommand($query,$conn)
$adapter = New-Object System.Data.SqlClient.SqlDataAdapter($cmd)
$dataset = New-Object System.Data.DataSet
$adapter.Fill($dataset) | Out-Null
return $dataset
}
"UPDATE" {
$cmd = New-Object System.Data.SqlClient.SqlCommand($query,$conn)
return $cmd.ExecuteNonQuery()
}
"INSERT" {
$cmd = New-Object System.Data.SqlClient.SqlCommand($query,$conn)
return $cmd.ExecuteNonQuery()
}
}
}
Query -query "SELECT TOP 10 myField FROM myTable" -server "SQLEXPRESS" -dbase "TEST"
This doesn't work, Powershell ISE gives me red in the param section but I don't understand why. Because of the many different ways people seem to construct Powershell functions (I'm a beginner :)) I am somewhat confused.
How do I make this function work?
Put commas , between the parameters:
Function SomeName {
Param ($param1,$param2,$param3)
}
etc. You can use whitespace or line break after the comma for readability, which is what most people do:
Function SomeName {
param (
$param1,
$param2
)
}
Related
I'm building a simple GUI where support staff can look up basic user information. The GUI is supposed to pull this data from an SQL DB, but nothing happens when the button is pressed.
function SQLquery {
param (
[Parameter(Mandatory = $true)]
[ValidateSet('NemOrgDB')]
[string]
$System,
[string]
$Query
)
switch ($System) {
NemOrgDB { $ConnectionString = 'Place holder for Stack Overflow' }
}
#New SqlConnection object
$sqlConnection = New-Object System.Data.SqlClient.SqlConnection
$sqlConnection.ConnectionString = $ConnectionString
$sqlConnection.Open()
#New Sqlcommand object
$sqlCommand = New-Object System.Data.SqlClient.SqlCommand
$sqlCommand.Connection = $sqlConnection
$sqlCommand.CommandText = $Query
#Create new sqldataadapter object
$sqlDataAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$sqlDataAdapter.SelectCommand = $sqlCommand
#Create new dataset object
$DataSet = New-Object System.Data.DataSet
#Fill dataset with dataadapter input
try {
$sqlDataAdapter.Fill($DataSet) | Out-Null
$output = $DataSet.Tables
}
catch {
$output = $null
}
#Close sql connection
$sqlConnection.close()
$null = $sqlConnection
$output
}
Here is the Function in use:
#Søgnigns criteria
$EmployeeID = Read-Host -Prompt "What user to find?"
#SQL Query
SQLquery -System NemOrgDB -Query "Select [medarbejder_wnr]
,[navn]
,[stilling]
,[stilling_nr]
,[firmakode_txt]
FROM [PersonData_NemOrg].[dbo].[Personale]
WHERE medarbejder_wnr ='$EmployeeID';"
The 2 pieces of code work as they should in a console environment with no Gui attached, they return the following results:
EmployeeID: EmployeeID
Name: Name
Role: IT support
Role number : Number
fimakode_txt : Company
When the same code is run inside the GUI nothing happens, here is the GUI code:
#ButtonSearch
#
$ButtonSearch.BackColor = [System.Drawing.SystemColors]::ActiveBorder
$ButtonSearch.FlatStyle = [System.Windows.Forms.FlatStyle]::System
$ButtonSearch.Font = (New-Object -TypeName System.Drawing.Font -ArgumentList #([System.String]'Tahoma',[System.Single]9,[System.Drawing.FontStyle]::Bold,[System.Drawing.GraphicsUnit]::Point,([System.Byte][System.Byte]0)))
$ButtonSearch.Location = (New-Object -TypeName System.Drawing.Point -ArgumentList #([System.Int32]12,[System.Int32]118))
$ButtonSearch.Name = [System.String]'ButtonSearch'
$ButtonSearch.Size = (New-Object -TypeName System.Drawing.Size -ArgumentList #([System.Int32]150,[System.Int32]23))
$ButtonSearch.TabIndex = [System.Int32]24
$ButtonSearch.Text = [System.String]'Search'
$ButtonSearch.UseCompatibleTextRendering = $true
$ButtonSearch.UseVisualStyleBackColor = $false
$ButtonSearch.add_Click($ButtonSearch_Click)
#
AND
$ButtonSearch_Click = {
$Wnummer = $TextBoxWnummer1.Text
SQLquery -System NemOrgDB -Query "Select [medarbejder_wnr]
,[navn]
,[stilling]
,[stilling_nr]
,[firmakode_txt]
FROM [PersonData_NemOrg].[dbo].[Personale]
WHERE medarbejder_wnr ='$Wnummer';"
}
It simply returns nothing.
I have a script which look like below:
function Invoke-SQL {
param(
[string] $dataSource = ".\MSSQLSERVER",
[string] $database = "master",
[string] $sqlCommand = $(throw "Please specify a query.")
)
$connectionString = "Data Source=$dataSource; Integrated Security=True; Initial Catalog=$database; Connect Timeout=100"
$connection = new-object system.data.SqlClient.SQLConnection($connectionString)
$command = new-object system.data.sqlclient.sqlcommand($sqlCommand,$connection)
Try {
$connection.Open()
} catch {
return, $null
}
$adapter = New-Object System.Data.sqlclient.sqlDataAdapter $command
$dataset = New-Object System.Data.DataSet
$adapter.Fill($dataSet) | Out-Null
$connection.Close()
$dataSet.Tables
}
[string]$SQL = "select CmsSrvName from CmsServerList_VM"
$DbInstances = Invoke-SQL "DBSRV" "Test" $SQL
workflow wf1{
Param([System.Data.DataTable]$instance)
ForEach -Parallel -ThrottleLimit 10 ($i in $instance) {
InlineScript{
$t = $using:i
Write-Verbose "$t['CmsSrvName']"
}
}
}
wf1 -Verbose $DbInstances
Output:
VERBOSE: [localhost]:System.Data.DataRow['CmsSrvName']
VERBOSE: [localhost]:System.Data.DataRow['CmsSrvName']
VERBOSE: [localhost]:System.Data.DataRow['CmsSrvName']
The output is not what I expected, it just print out the type name not the value. How can I access the DataRow value in a workflow?(in Powershell 5)
Thanks in advance for the help
Is there an easy way to test the connectivity to a MS SQL Server instance from a client (without loading any SQL assemblies) with PowerShell?
MS Sql: Servername\Instance Port 1433
How can I test the connectivity to the server with PowerShell from a normal client?
Use the SqlConnection class to test a connection. You don't have to load any SQL assemblies.
Helper function:
function Test-SQLConnection
{
[OutputType([bool])]
Param
(
[Parameter(Mandatory=$true,
ValueFromPipelineByPropertyName=$true,
Position=0)]
$ConnectionString
)
try
{
$sqlConnection = New-Object System.Data.SqlClient.SqlConnection $ConnectionString;
$sqlConnection.Open();
$sqlConnection.Close();
return $true;
}
catch
{
return $false;
}
}
Usage example:
Test-SQLConnection "Data Source=localhost;database=myDB;User ID=myUser;Password=myPassword;"
This is basically the same as Martin's answer, only the connection string is build from the parameters, and the time taken to connect is measured.
e.g:
Test-SQLDatabase -Server SQLServer -Database SomeDB -Username SQLUser -Password password
or
Test-SQLDatabase -Server Server1\SQLExpress -Database SomeDB -UseWindowsAuthentication
.
function Test-SQLDatabase
{
param(
[Parameter(Position=0, Mandatory=$True, ValueFromPipeline=$True)] [string] $Server,
[Parameter(Position=1, Mandatory=$True)] [string] $Database,
[Parameter(Position=2, Mandatory=$True, ParameterSetName="SQLAuth")] [string] $Username,
[Parameter(Position=3, Mandatory=$True, ParameterSetName="SQLAuth")] [string] $Password,
[Parameter(Position=2, Mandatory=$True, ParameterSetName="WindowsAuth")] [switch] $UseWindowsAuthentication
)
# connect to the database, then immediatly close the connection. If an exception occurrs it indicates the conneciton was not successful.
process {
$dbConnection = New-Object System.Data.SqlClient.SqlConnection
if (!$UseWindowsAuthentication) {
$dbConnection.ConnectionString = "Data Source=$Server; uid=$Username; pwd=$Password; Database=$Database;Integrated Security=False"
$authentication = "SQL ($Username)"
}
else {
$dbConnection.ConnectionString = "Data Source=$Server; Database=$Database;Integrated Security=True;"
$authentication = "Windows ($env:USERNAME)"
}
try {
$connectionTime = measure-command {$dbConnection.Open()}
$Result = #{
Connection = "Successful"
ElapsedTime = $connectionTime.TotalSeconds
Server = $Server
Database = $Database
User = $authentication}
}
# exceptions will be raised if the database connection failed.
catch {
$Result = #{
Connection = "Failed"
ElapsedTime = $connectionTime.TotalSeconds
Server = $Server
Database = $Database
User = $authentication}
}
Finally{
# close the database connection
$dbConnection.Close()
#return the results as an object
$outputObject = New-Object -Property $Result -TypeName psobject
write-output $outputObject
}
}
}
That depends on what you actually want to test. If you just want to verify that you can connect to the port on the remote host something like this will do:
$server = 'servername'
$port = 1433
$tcp = New-Object Net.Sockets.TcpClient
if ([void]$tcp.Connect($server, $port)) {
'connected'
} else {
'not connected'
}
$tcp.Dispose()
If you want to verify that a connection to an SQL Server instance can be established you'll need something like this:
$dbhost = 'servername'
$dbinst = 'instance'
$dbname = 'master'
$username = ...
$password = ...
$cs = "Server=$dbhost\$dbinst;Database=$dbname;User Id=$username;" +
"Password=$password;"
$cn = New-Object -COM 'ADODB.Connection'
$cn.ConnectionString = $cs
try {
$cn.Open()
if ($cn.State -eq 1) {
'connected'
$cn.Close()
} else {
'not connected'
}
} catch {
'not connected'
}
I've used the ConnectionState enum to check the database connection state.
Documentation on this enum can be found here
It can be accessed with the following: [System.Data.ConnectionState]::Open
Other options are Broken, Closed, Connecting, Executing, and Fetching.
Example:
class Database
{
[System.Data.SqlClient.SqlConnection]$Connection
[void]Connect
{
$sqlConnection = New-Object System.Data.SqlClient.SqlConnection
$sqlConnection.ConnectionString = $connectionString
$sqlConnection.Open()
$this.Connection = $sqlConnection
}
[bool]IsConnected
{
return $this.Connection.State -eq [System.Data.ConnectionState]::Open
}
}
I have an SSIS execute process task where I am calling a PowerShell script. I found that I can get the process task to fail if the PowerShell script fails by trapping the errors like this:
trap {
$err = $_.Exception
while ( $err.InnerException )
{
$err = $err.InnerException
Write-Host $err.Message
};
exit 1
}
I would like to get the error messages to return to SSIS, but I can't seem to get it to work. It just returns the following:
The process exit code was "1" while the expected was "0".
Any help is appreciated.
Full Code:
Param(
[String]$excelPath,
[String]$serverName,
[String]$databaseName,
[String]$tableName
)
$ErrorActionPreference = 'Stop'
[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMO') | Out-Null
[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMO.SqlDataType') | Out-Null
Trap {
$err = $_.Exception
while ( $err.InnerException )
{
$err = $err.InnerException
Write-Host $err.Message
};
exit 1
}
$excel = New-Object -ComObject excel.application
$excel.visible = $False
$excel.displayalerts=$False
$workbook = $excel.Workbooks.Open($ExcelPath)
$workSheet = $workbook.worksheets.Item(1).name
$workbook.Close()
$excel.quit()
$excel = $null
$query = "select * from [$workSheet`$]";
$connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=`"$excelPath`";Extended Properties=`"Excel 12.0 Xml;HDR=YES`";"
# Instantiate some objects which will be needed
$serverSMO = New-Object Microsoft.SqlServer.Management.Smo.Server($serverName)
$db = $serverSMO.Databases[$databaseName];
$newTable = New-Object Microsoft.SqlServer.Management.Smo.Table ;
$newTable.Parent = $db;
$newTable.Name = $tableName ;
$conn = New-Object System.Data.OleDb.OleDbConnection($connectionString)
$conn.open()
$cmd = New-Object System.Data.OleDb.OleDbCommand($query,$conn)
$dataAdapter = New-Object System.Data.OleDb.OleDbDataAdapter($cmd)
$dataTable = New-Object System.Data.DataTable
$dataAdapter.fill($dataTable)
$conn.close()
# Drop the table if it exists
if($db.Tables.Contains($tableName).Equals($true))
{
($db.Tables[$tableName]).Drop()
}
# Iterate the columns in the DataTable object and add dynamically named columns to the SqlServer Table object.
foreach($col in $dataTable.Columns)
{
$sqlDataType = [Microsoft.SqlServer.Management.Smo.SqlDataType]::Varchar
$dataType = New-Object Microsoft.SqlServer.Management.Smo.DataType($sqlDataType);
$dataType.MaximumLength = 1000;
$newColumn = New-Object Microsoft.SqlServer.Management.Smo.Column($newTable,$col.ColumnName,$dataType);
$newColumn.DataType = $dataType;
$newTable.Columns.Add($newColumn);
}
$newTable.Create();
#bcp data into new table
$connectionString = "Data Source=$serverName;Integrated Security=true;Initial Catalog=$databaseName;"
$bc = New-Object ("Data.SqlClient.SqlBulkCopy") $connectionString
$bc.DestinationTableName = "$tableName"
$bc.WriteToServer($dataTable)
Following this post when I am trying to implement Using functionality in Powershell
Function cUsing {
param (
[System.IDisposable] $inputObject = $(throw "The parameter -inputObject is required."),
[ScriptBlock] $scriptBlock = $(throw "The parameter -scriptBlock is required.")
)
Try { &$scriptBlock }
Finally {
if ($inputObject -ne $null) {
if ($inputObject.psbase -eq $null) {
$inputObject.Dispose()
} else {
$inputObject.psbase.Dispose()
}
}
}
}
cUsing($sqlConnection = New-Object System.Data.SqlClient.SqlConnection)
{
$sqlConnection.ConnectionString = "Server=myserver; Database=master; Integrated Security= True"
$sqlConnection.Open()
$sqlCmd = New-Object System.Data.SqlClient.SqlCommand
$sqlCmd.CommandText = "SELECT STATEMENT"
$sqlCmd.Connection = $sqlConnection
$dsValues = New-Object System.Data.DataSet
$daValues = New-Object System.Data.SqlClient.SqlDataAdapter($sqlCmd)
$daValues.Fill($dsValues)
Write-Host $dsValues.Tables[0]
}
Error:
[ScriptBlock] $scriptBlock = $(throw <<<< "The parameter -scriptBlock is required.")
Edit [Graimer]
When I have moved the curly braces like below
cUsing($sqlConnection = New-Object System.Data.SqlClient.SqlConnection){
....
....
Write-Host $dsValues.Tables[0]}
I am not getting any error but the output which I am getting is
$sqlConnection.ConnectionString = "Server=myserver; Database=master; Integrated Security= True"
$sqlConnection.Open()
$sqlCmd = New-Object System.Data.SqlClient.SqlCommand
$sqlCmd.CommandText = "Select query"
$sqlCmd.Connection = $sqlConnection
$dsValues = New-Object System.Data.DataSet
$daValues = New-Object System.Data.SqlClient.SqlDataAdapter($sqlCmd)
$daValues.Fill($dsValues)
Write-Host $dsValues.Tables[0]
EDIT [Ansgar Wiechers]
when I have changed the scriptblock line to
Function cUsing {
param (
[System.IDisposable] $inputObject = $(throw "The parameter -inputObject is required."),
[ScriptBlock] $scriptBlock = ${throw "The parameter -scriptBlock is required."}
)
I am not getting any error but I am getting the same output which I have showed in Edit above.
The problem with your code is that you don't provide the scriptblock as a parameter, but as a command on it's own. Commands in Powershell are on a single line. Your code runs the command with only the using parameter, and fails. THEN you declare a scriptblock.
You have two options here; escape the linebreak(BAD practice), or move the opening curly brace one line up so it is considered part of the cusing command. Like this:
cUsing($sqlConnection = New-Object System.Data.SqlClient.SqlConnection) {
$sqlConnection.ConnectionString = "Server=myserver; Database=master; Integrated Security= True"
...
..
.
}
A script block is defined via curly braces, not via $(). Like this:
[ScriptBlock]$scriptBlock = { throw "The parameter ..." }
$() is the operator for evaluating subexpressions.
Add-Type #"
using System;
public class Code : System.IDisposable
{
public bool IsDisposed { get; set; }
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (!IsDisposed)
{
IsDisposed = true;
}
}
}
"#
function using-block {
param (
[System.IDisposable]$InputObject = $(throw "The parameter -inputObject is required."),
[ScriptBlock]$ScriptBlock = $(throw "The parameter -scriptBlock is required.")
)
try { &$ScriptBlock }
finally {
if ($InputObject) {
if ($InputObject.PSBase) {
$InputObject.PSBase.Dispose()
} else {
$InputObject.Dispose()
}
}
}
}
using-block($c = New-Object Code) {
$c.IsDisposed
}
$c.IsDisposed
Outputs:
False
True