-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConnect-SapGui.ps1
More file actions
109 lines (85 loc) · 3.25 KB
/
Copy pathConnect-SapGui.ps1
File metadata and controls
109 lines (85 loc) · 3.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#Requires -Version 5.1
<#
.SYNOPSIS
Attaches to a running SAP GUI, opens a connection and logs on.
.DESCRIPTION
PowerShell port of src/vbs/SapConnection.vbs.
The SAP GUI object model is populated asynchronously, so each step is polled
until it appears or the timeout expires. The password travels as a
PSCredential and is only converted to plain text at the moment it is written
into the logon field.
Emits the GuiSession object so the caller can pipe it into other scripts.
.PARAMETER ConnectionString
SAP connection string, e.g. "/H/sapgw.example.com/S/3200".
.PARAMETER Language
Logon language, e.g. "KO" or "EN".
.PARAMETER Credential
SAP user name and password. Prompted for when omitted.
.PARAMETER TimeoutSeconds
How long to wait for each stage of the object model. Default 30.
.EXAMPLE
$session = .\Connect-SapGui.ps1 -ConnectionString '/H/sap.example.com/S/3200' -Language KO
.\Set-SapTableColumn.ps1 -Session $session -TableId 'wnd[1]/usr/tblX' -Values '1000,1010'
.NOTES
Requires SAP GUI for Windows with scripting enabled on the client
(Options > Accessibility & Scripting) and on the server
(profile parameter sapgui/user_scripting = TRUE).
#>
[CmdletBinding()]
param(
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[string]$ConnectionString,
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[string]$Language,
[Parameter(Mandatory)]
[System.Management.Automation.PSCredential]
[System.Management.Automation.Credential()]
$Credential,
[ValidateRange(1, 600)]
[int]$TimeoutSeconds = 30
)
Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'
Add-Type -AssemblyName Microsoft.VisualBasic
function Wait-SapObject {
param(
[Parameter(Mandatory)][scriptblock]$Factory,
[Parameter(Mandatory)][string]$What,
[Parameter(Mandatory)][int]$TimeoutSeconds
)
$deadline = (Get-Date).AddSeconds($TimeoutSeconds)
do {
try {
$candidate = & $Factory
if ($null -ne $candidate) { return $candidate }
}
catch {
Write-Verbose "waiting for $What : $($_.Exception.Message)"
}
Start-Sleep -Milliseconds 500
} while ((Get-Date) -lt $deadline)
throw "Timed out after ${TimeoutSeconds}s waiting for $What."
}
$application = Wait-SapObject -What 'the SAP GUI scripting engine' -TimeoutSeconds $TimeoutSeconds -Factory {
[Microsoft.VisualBasic.Interaction]::GetObject('SAPGUI').GetScriptingEngine()
}
$connection = Wait-SapObject -What "a connection to $ConnectionString" -TimeoutSeconds $TimeoutSeconds -Factory {
$application.OpenConnectionByConnectionString($ConnectionString, $true)
}
$session = Wait-SapObject -What 'a session on the connection' -TimeoutSeconds $TimeoutSeconds -Factory {
$connection.Children(0)
}
$session.FindById('wnd[0]').Maximize()
$session.FindById('wnd[0]/usr/txtRSYST-LANGU').Text = $Language
$session.FindById('wnd[0]/usr/txtRSYST-BNAME').Text = $Credential.UserName
$networkCredential = $Credential.GetNetworkCredential()
try {
$session.FindById('wnd[0]/usr/pwdRSYST-BCODE').Text = $networkCredential.Password
}
finally {
$networkCredential.Password = $null
}
$session.FindById('wnd[0]').SendVKey(0)
Write-Output $session