oracledba.help

PowerShell

<- QuickStarts

Overview

Windows PowerShell is Microsoft's task automation framework, consisting of a command-line shell and associated scripting language built on .NET. PowerShell enables you to perform administrative tasks on both local and remote Windows systems. Windows Management Framework is an AKA for PowerShell.

The following example installs PowerShell 3 on a Windows 2008 system.

Prerequisites

  • Before installing Windows Management Framework 3.0, uninstall any previous versions of Windows Management Framework.
  • To see which versions of the .NET Framework is installed, view the %WINDIR%\Microsoft.NET\Framework directory. Also check the Framework64 directory on a 64-bit computer, which can have 32 or 64-bit versions installed.
  • Create a directory for your scripts. Example: C:\app\scripts\PowerShell

Installation and Configuration

  1. Download then install (dotNetFx45_Full_setup.exe) the full installation of Microsoft .NET Framework 4.5.
  2. Download then install Windows Management Framework 3.0.
    If you get the dreaded "The update is not applicable to your computer." this works on some systems:
    1. Uninstall 4.5 if not being used.
    2. Reboot system.
    3. Install .NET Framework 4.
    4. Reboot system.
    5. Install .NET 4.5.
    6. Reboot system.
    7. Install for Windows Management Framework 3.0.
  3. Test
    • Go to your scripts directory from a console window.
    • At the OS prompt issue the command: PowerShell
  4. Allow script execution.
    • PS C:\> Set-ExecutionPolicy Unrestricted

Warning 1

On 64-bit Windows there are actually two ways to run PowerShell (32-bit and 64-bit) and the policy must be set on both to run your scripts in many cases.

  • 32 = %windir%\SysWOW64\WindowsPowerShell\v1.0
  • 64 = %windir%\System32\WindowsPowershell\v1.0

Command to run: Set-ExecutionPolicy Unrestricted

Warning 2

If you get the infamous "The update is not applicable to your computer." know you are not alone. The installation of PS3 (AKA Windows Management Framework 3) on some variants of Windows is an atrocity in some cases. Like many, I have tried every Internet hack to circumnavigate this issue. On some systems however, PS3 will still not install. As it stands now, it is a well known and unfixed PowerShell issue.

Many have found staying with PS2 the only option if this error is encountered.

Code Snippet

# Connect to an Oracle 11g database.

# Import Oracle Library (match to .NET version available)
#Add-Type -Path C:\app\oracle\product\11.2.0.3\dbhome_1\ODP.NET\bin\4\Oracle.DataAccess.dll
Add-Type -Path C:\app\oracle\product\11.2.0.3\dbhome_1\ODP.NET\bin\2.x\Oracle.DataAccess.dll

# Connect
try {
	$conn = new-object Oracle.DataAccess.Client.OracleConnection(
               "User Id=scott;Password=tiger;Data Source=localhost/DB01")
	$conn.open()
	write-host "Connection Open"
} catch [Exception] {
	write-host "Error" `t $_.Exception.message
	write-host "Type"  `t $_.Exception.GetType().FullName;
}


# Get Value
try {
	$cmd = $conn.CreateCommand()	
	#$cmd.CommandText = "SELECT 'HelloWorld' FROM dual"
	$cmd.CommandText = "SELECT version,instance_number, startup_time FROM v`$instance" # If SYSDBA
	$rdr = $cmd.ExecuteReader()
	if ( $rdr.read() ) {
		$rdr.GetString(0)
		$rdr.GetDecimal(1)
		$rdr.GetDateTime(2)
	}
} catch [Exception] {
	write-host "Error" `t $_.Exception.message
	write-host "Type"  `t $_.Exception.GetType().FullName;
}


# End
if ($conn.state -eq 'Open') {
	$conn.close()
	$conn.dispose()
}
read-host "QC Pause"
exit

<- QuickStarts