how to connect oracle database and run query by powershell - powershell

How can I connect to an Oracle database (11g)? I have a list of oracle servers and I want to execute a query on them from Jump Server (the Jump server has Powershell v2).
Servers are accessible from Jump Server as I am able to access them by UNC path.
Update:
I know it can be done by using ODP.Net connector. But i don't want to use it due to approval.

You can try the method using an OleDbConnection described here
function Get-OLEDBData ($connectstring, $sql) {
$OLEDBConn = New-Object System.Data.OleDb.OleDbConnection($connectstring)
$OLEDBConn.open()
$readcmd = New-Object system.Data.OleDb.OleDbCommand($sql,$OLEDBConn)
$readcmd.CommandTimeout = '300'
$da = New-Object system.Data.OleDb.OleDbDataAdapter($readcmd)
$dt = New-Object system.Data.datatable
[void]$da.fill($dt)
$OLEDBConn.close()
return $dt
}

Related

How to access mdb file in windows10 by powershell?

Following the suggestion that shown in the following link,
I have created a powershell script to access the mdb file.
https://devblogs.microsoft.com/scripting/hey-scripting-guy-can-i-query-a-microsoft-access-database-with-a-windows-powershell-script/
which is:
$cn = new-object -comobject ADODB.Connection
$rs = new-object -comobject ADODB.Recordset
$cn.Open("Provider = Microsoft.Jet.OLEDB.4.0;Data Source = $path")
However, i have updated my computer to windows 10 (64bit) and installed ACCESS 2016. The powershell script does not working anymore. And it shows that the
Provider = Microsoft.Jet.OLEDB.4.0 is not found.
I have tried to change the Provider = Microsoft.ACE.OLEDB.12.0
But it does not help, as it shows the same error that the provider is not found.
Moreover, I have tried to open it with ACCESS 2016, it also does not help as it shows that can not open the database file that created in previous version.
Please help....
Thanks for whom reply my question.
Finally I got insight and find the answer myself.
I search the provider from Microsoft and find the link below.
https://www.microsoft.com/en-us/download/details.aspx?id=13255
After install the 64bit version, I can use the following code with the Microsoft.ACE.OLEDB.12.0 provider.
$cn = new-object -comobject ADODB.Connection
$rs = new-object -comobject ADODB.Recordset
$cn.Open("Provider = Microsoft.ACE.OLEDB.12.0;Data Source = $path")
If Win 10 OS 64 bits with MS Office 32bits / click2run , use 32bits Powershell ;)

Cannot use Azure SQL with Azure DevOps Server 2019 - (Azure Government)

I have been struggling to get my DevOpsServer 2019-RC1 installation to see my Azure SQL Server
My DevOpsServer install is on an Azure VM, as recommended
I have implemented everything from the below documentation regarding the set up of an MSI for the VM, and granting the MSI access to Azure SQL via AAD authentication:
https://learn.microsoft.com/en-us/tfs/server/install/install-azure-sql
I have also added the VNET where the VM NIC is located, as a VNET firewall rule on the Azure SQL Server to ensure there are no connection issues
My DevOpsServer refuses to see the Azure SQL Server or its databases
To confirm that Azure SQL is not blocked from the VM, I successfully created an ODBC system DSN connection on the VM, which allows me to see the Azure SQL Server, and its Databases
Per the reference documentation:
When setting up a new DevOps Server instance, i selected "This is a
new Azure DevOps Server deployment"
On the Database page of the configuration wizard, specify the Azure SQL
Database server instance, typically in the form of
"SQLInstanceName.database.windows.net"
Please let me know if there is anything else i can do to help the Devops Server Configuration Wizard see my Azure SQL Server and Databases
PS I am trying to get this working in Azure Government (MAG) if this changes the capability...
Error received when attempting to connect to the SQL Database programmatically via the following Powershell script:
# Retrieve the access token
$response = Invoke-WebRequest -Uri 'http://169.254.169.254/metadata/identity/oauth2/token?api-version=2018-02-01&resource=https%3A%2F%2Fdatabase.usgovcloudapi.net' -Method GET -Headers #{Metadata="true"}
$content = $response.Content | ConvertFrom-Json
$AccessToken = $content.access_token
# Create the connection
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = "Data Source = test-sqlsrv.database.usgovcloudapi.net; Initial Catalog = inldb"
$SqlConnection.AccessToken = $AccessToken
$SqlConnection.Open()
# Send a Query
$SqlCmd = New-Object System.Data.SqlClient.SqlCommand
$SqlCmd.CommandText = "SELECT * from <TABLE>;"
$SqlCmd.Connection = $SqlConnection
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter.SelectCommand = $SqlCmd
$DataSet = New-Object System.Data.DataSet
$SqlAdapter.Fill($DataSet)
Try running ALTER USER [VMName] WITH DEFAULT_SCHEMA=dbo from both the collection and config DBs. I have a PR out to add this to the documentation and this might be causing your issue.
Try connecting to the SQL instance via Powershell instead of Azure DevOps Server.
You can run the following script to do that:
# Retrieve the access token
$response = Invoke-WebRequest -Uri 'http://169.254.169.254/metadata/identity/oauth2/token?api-version=2018-02-01&resource=https%3A%2F%2Fdatabase.windows.net%2F' -Method GET -Headers #{Metadata="true"}
$content = $response.Content | ConvertFrom-Json
$AccessToken = $content.access_token
# Create the connection
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = "Data Source = <AZURE-SQL-SERVERNAME>; Initial Catalog = <DATABASE>"
$SqlConnection.AccessToken = $AccessToken
$SqlConnection.Open()
# Send a Query
$SqlCmd = New-Object System.Data.SqlClient.SqlCommand
$SqlCmd.CommandText = "SELECT * from <TABLE>;"
$SqlCmd.Connection = $SqlConnection
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter.SelectCommand = $SqlCmd
$DataSet = New-Object System.Data.DataSet
$SqlAdapter.Fill($DataSet)
This should at least give you a more descriptive error to debug.
Gvazzana, I also recommend testing with the script danwkennedy provided, but be sure to change database.windows.net to database.usgovcloudapi.net for Azure Government.
I would pay close attention to any errors around obtaining an access token and errors associated with opening the SQL connection.
If nothing's standing out is it possible to capture a Fiddler trace?
I agree that you should perform the PowerShell test to test your MSI. It looks like you are having issues reaching the IP to get the access token. If you are attempting to connect from a Government network, it is likely that you are going through some type of web proxy service (Blue Coat, etc.) to get to the internet. You can maybe trick the proxy by emulating the user agent string from a browser by using the -UserAgent parameter on your Invoke-WebRequest command.
If that doesn't work, you may need to reach out to your proxy team to troubleshoot the issue.
Lastly, if you are able to test successfully in PowerShell, but you still get the connection test error in the DevOps install, make sure to follow the steps here:
https://learn.microsoft.com/en-us/tfs/server/install/install-azure-sql
I forgot to add the user to the master DB... Once I did that, everything worked. Good luck.
You can access to your Azure Devops database with your Windows Authentication level. So your Windows login user should have configured sysadmin privilege in your database (e.g Microsoft SQL server)

How to run Powershell script on local computer but with credentials of a domain user

I have to implement a solution where I have to deploy a SSIS project (xy.ispac) from one machine to another. So far I've managed to copy-cut-paste the following stuff from all around the internet:
# Variables
$ServerName = "target"
$SSISCatalog = "SSISDB" # sort of constant
$CatalogPwd = "catalog_password"
$ProjectFilePath = "D:\Projects_to_depoly\Project_1.ispac"
$ProjectName = "Project_name"
$FolderName = "Data_collector"
# Load the IntegrationServices Assembly
[Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.Management.IntegrationServices")
# Store the IntegrationServices Assembly namespace to avoid typing it every time
$ISNamespace = "Microsoft.SqlServer.Management.IntegrationServices"
Write-Host "Connecting to server ..."
# Create a connection to the server
$sqlConnectionString = "Data Source=$ServerName;Initial Catalog=master;Integrated Security=SSPI;"
$sqlConnection = New-Object System.Data.SqlClient.SqlConnection $sqlConnectionString
$integrationServices = New-Object "$ISNamespace.IntegrationServices" $sqlConnection
$catalog = $integrationServices.Catalogs[$SSISCatalog]
# Create the Integration Services object if it does not exist
if (!$catalog) {
# Provision a new SSIS Catalog
Write-Host "Creating SSIS Catalog ..."
$catalog = New-Object "$ISNamespace.Catalog" ($integrationServices, $SSISCatalog, $CatalogPwd)
$catalog.Create()
}
$folder = $catalog.Folders[$FolderName]
if (!$folder)
{
#Create a folder in SSISDB
Write-Host "Creating Folder ..."
$folder = New-Object "$ISNamespace.CatalogFolder" ($catalog, $FolderName, $FolderName)
$folder.Create()
}
# Read the project file, and deploy it to the folder
Write-Host "Deploying Project ..."
[byte[]] $projectFile = [System.IO.File]::ReadAllBytes($ProjectFilePath)
$folder.DeployProject($ProjectName, $projectFile)
This seemed to be working surprisingly well on the development machine - test server pair. However, the live environment will be a bit different, the machine doing the deployment job (deployment server, or DS from now on) and the SQL Server (DB for short) the project is to be deployed are in different domains and since SSIS requires windows authentication, I'm going to need to run the above code locally on DS but using credentials of a user on the DB.
And that's the point where I fail. The only thing that worked is to start the Powershell command line interface using runas /netonly /user:thatdomain\anuserthere powershell, enter the password, and paste the script unaltered into it. Alas, this is not an option, since there's no way to pass the password to runas (at least once with /savecred) and user interactivity is not possible anyway (the whole thing has to be automated).
I've tried the following:
Simply unning the script on DS, the line $sqlConnection = New-Object System.Data.SqlClient.SqlConnection $sqlConnectionString would use the credentials from DS which is not recognized by DB, and New-Object does not have a -Credential arg that I could pass to
Putting everything into an Invoke-Command with -Credential requires using -Computername as well. I guess it would be possible to use the local as 'remote' (using . as Computername) but it still complains about access being denied. I'm scanning through about_Remote_Troubleshooting, so far without any success.
Any hints on how to overcome this issue?
A solution might be to use a sql user (with the right access rights) instead of an AD used.
Something like this should work.
(Check also the answer to correct the connection string)

Grant read permission for MSysObjects

I need to get the metadata of some hundred MS Access DBs, so I need to automate the process of metadata gathering.
I want to query stuff described here, but before I can query the DBs I need read access to MSysObjects tables.
I Keep getting following error:
Record(s) cannot be read; no read permission on 'MsysObjects'.
so I dug deeper:
I've read the article about similar issues in the C# section (see here and also this one).
I put together a simple script which works well for creating tables and so on, but I am having problems to setup the read access.
$ScrUsr = $(whoami)
Write-Host $ScrUsr
$cmd = "GRANT SELECT ON MSysObjects TO [$ScrUsr]"
Write-Host $cmd
Function Invoke-ADOCommand($Db)
{
$connection = New-Object -ComObject ADODB.Connection
$connection.Open("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=$db;" )
$command = New-Object -ComObject ADODB.Command
$command.ActiveConnection = $connection
$command.CommandText = $cmd
$rs = $command.Execute()
$connection.Close()
}
$Db = "C:\Temp\test1.accdb"
Invoke-ADOCommand -db $Db
Executing the Script throws me following Error reason:
Cannot open the Microsoft Office Access database engine workgroup information file.
How can I allow read permission on MSysObjects?
There are two issues you must address:
Your code will use the Access security account "Admin" when interacting with the Access ACCDB database. Other Access security accounts are only available if you have implemented Access user-level security (ULS), but that feature is only available for MDB format databases. Therefore, with ACCDB, you can't GRANT anything to any account other than "Admin".
You must include the location of the "System database" in your connection string.
If you don't know where to find the System database, open Access and check the output from this statement in the Immediate window. (Ctrl+g will open the Immediate window)
Debug.Print Application.DBEngine.SystemDb
My System database is C:\Users\hans\AppData\Roaming\Microsoft\Access\System1.mdw Substitute yours in the code example below.
I'm not really proficient with Powershell, but this example ran without error for me. (Be sure to change both $Db and $SystemDb.)
$ScrUsr = $(whoami)
Write-Host $ScrUsr
$cmd = "GRANT SELECT ON MSysObjects TO Admin;"
Write-Host $cmd
Function Invoke-ADOCommand($Db, $SystemDb)
{
$connection = New-Object -ComObject ADODB.Connection
$ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=$db;Jet OLEDB:System database=$SystemDb;"
Write-Host $ConnectionString
$connection.Open($ConnectionString)
$discard = $connection.Execute($cmd)
$connection.Close()
}
$Db = "C:\Users\hans\AccessApps\Sample.accdb"
$SystemDb = "C:\Users\hans\AppData\Roaming\Microsoft\Access\System1.mdw"
Invoke-ADOCommand -db $Db -SystemDb $SystemDb

Powershell connect to firebird

Hey I was wondering how one could connect to a firebird database (gdb) file from within powershell. Is there a way to use the .net data provider for firebird to connect? Is there a way to connect with System.Data.Odbc.OdbcConnection to firebird?
function Get-ODBC-Data{
param([string]$query=$(throw 'query is required.'))
$conn=New-Object System.Data.Odbc.OdbcConnection
$connStr = "Driver={Red Database/Firebird driver};Server=localhost;Port=****;Database=*.fdb;Uid=user;Pwd=userpassword;"
$conn.ConnectionString= $connStr
$conn.open
$cmd=new-object System.Data.Odbc.OdbcCommand($query,$conn)
$cmd.CommandTimeout=15
$ds=New-Object system.Data.DataSet
$da=New-Object system.Data.odbc.odbcDataAdapter($cmd)
[void]$da.fill($ds)
$ds.Tables[0]
#Write-Output $ds.Tables[0].rows.count
$conn.close()
}
$query = #"
select count(*)
from UNIFO_PAYMENT U
join DOCUMENT D on D.ID = U.ID
"#
$result = Get-ODBC-Data -query $query
The code from #Alexandr is from https://www.andersrodland.com/working-with-odbc-connections-in-powershell/
I suggest you read the entire thing. While he isn't exactly explaining the code either, I think it's fairly self-documenting, the only information that is missing, is how to get/use the Firebird driver.
You can get the ODBC driver installer from firebird https://www.firebirdsql.org/en/odbc-driver/
After Installing it, open windows ODBC administration (odbcad32.exe), check the drivers tab to make sure that the "Firebird/Interbase(r) driver" is there.
From here you can either use that driver name to run #Alexandr 's code
$connStr = "Driver=Firebird/Interbase(r) driver;Server=localhost;Port=****;Database=*.fdb;Uid=user;Pwd=userpassword;"
OR Go to either the User DSN tab, or the system DSN tab. Click add, select the firebird driver, set up the DSN with a name, the path to your database and the other required database registration info you normally would. Keep the DSN name simple, it's an identifier you will use.
Then you can simply replace the $constr with
$connStr = "DSN=YourDsnName;"
Yes, powershell allows you to create .Net classes and call .Net methods. See here. So with little trouble you can convert your C# code to powershell.