Last active
August 22, 2019 13:06
-
-
Save darrenjrobinson/98997cd99c78cac535abb97c973244b3 to your computer and use it in GitHub Desktop.
zOS RACF Task Automation using PowerShell. Associated blogpost https://blog.darrenjrobinson.com/automating-racf-administration-with-powershell/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# wc3270 HTTPD Config from wc3270 Start Options e.g. wc3270 A:mainframe.customer.com.au -httpd 127.0.0.1:6001 | |
$wc3270HTTPDHost = "127.0.0.1" # e.g FQDN of host running it, or localhost DNS Name or IP | |
$wc3270HTTPDPort = "6001" # HTTPD Port | |
$racfHost = "zoshost.customer.com.au" # z/OS Host | |
$racfUserID = "ADMINUserName" # RACF UserID | |
$racfUserPassword = "AdminPassword" # RACF Password | |
$tracelog = "c:\temp\zOStracelog.txt" # TraceLogPath | |
$wc3270Path = "C:\Program Files\wc3270" # wc3270 Install Path | |
$wc3270Args = "A:$($racfHost) -httpd $($wc3270HTTPDHost):$($wc3270HTTPDPort) -trace -tracefile `"$($tracelog)`" -utf8" | |
Function startWC3270 { | |
if (!$Global:3270Term.ProcessName) { | |
$Global:3270Term = start-process ".\wc3270.exe" -ArgumentList $wc3270Args -WorkingDirectory $wc3270Path -PassThru | |
if ($Global:3270Term.Id) { | |
write-host "wc3270 Started" | |
write-host $Global:3270Term | |
# allow time for wc3270 to start | |
start-sleep -milliseconds 1500 | |
$Global:3270TermTrace = Get-Process -Name catf | |
write-host $Global:3270TermTrace.Name $Global:3270TermTrace.ID | |
return $Global:3270Term, $Global:3270TermTrace | |
} | |
} | |
} | |
Function stopWC3270 { | |
if (get-process -Id $Global:3270Term.Id) { | |
Stop-Process $Global:3270Term -PassThru | |
$Global:3270Term = $null | |
Stop-Process $Global:3270TermTrace -PassThru | |
$Global:3270TermTrace = $null | |
write-host "wc3270 Stopped" | |
} | |
} | |
function TraceLogCheck($tracelog) { | |
$log = get-item -Path $tracelog | |
$logUpdate = Get-Content $tracelog -Tail 3 | |
$logStatus = @{} | |
$logStatus.add("Length", $log.Length) | |
$logStatus.add("Trace", $logUpdate) | |
return $logStatus | |
} | |
function WaitforProcessing { | |
$currentStatus = TraceLogCheck($tracelog) | |
$oldStatus = $currentStatus | |
[int]$loop = 0 | |
do { | |
$loop++ | |
start-sleep -Seconds 1 | |
$newStatus = $null | |
$newStatus = TraceLogCheck($tracelog) | |
# Processing ? | |
if ($newStatus.Length -gt $oldStatus.Length) { | |
$oldStatus = $newStatus | |
Start-Sleep -Milliseconds 750 | |
$loop = 0 | |
write-output "[Waiting] $($newStatus.Length)" | |
write-output "[Waiting] $($newStatus.Trace[2])" | |
} | |
} while ($loop -le 3) | |
} | |
# Start 3270 Connection | |
startWC3270 | |
# Pause for startup | |
start-sleep -seconds 2 | |
WaitforProcessing | |
<# | |
GET USERS | |
#> | |
# Test to see if wc3270 Service is up | |
$status = Invoke-RestMethod -uri "http://$($wc3270HTTPDHost):$($wc3270HTTPDPort)/3270/rest/stext/Query()" -UseBasicParsing -Method Get | |
WaitforProcessing | |
if ($status.Contains("host $($racfHost)")) { | |
# Logon | |
$logon = Invoke-RestMethod -uri "http://$($wc3270HTTPDHost):$($wc3270HTTPDPort)/3270/rest/stext/String($($racfUserID))" -UseBasicParsing -Method Get | |
WaitforProcessing | |
if ($logon.Contains("U U U C($($racfHost))")) { | |
$logon = $null | |
$logon = Invoke-RestMethod -uri "http://$($wc3270HTTPDHost):$($wc3270HTTPDPort)/3270/rest/stext/enter" -UseBasicParsing -Method Get | |
WaitforProcessing | |
if ($logon.Contains("U U U C($($racfHost))") ) { | |
$logon = $null | |
$logon = Invoke-RestMethod -uri "http://$($wc3270HTTPDHost):$($wc3270HTTPDPort)/3270/rest/stext/String($($racfUserPassword)" -UseBasicParsing -Method Get | |
WaitforProcessing | |
if ($logon.Contains("U U U C($($racfHost))")) { | |
$logon = Invoke-RestMethod -uri "http://$($wc3270HTTPDHost):$($wc3270HTTPDPort)/3270/rest/stext/enter" -UseBasicParsing -Method Get | |
WaitforProcessing | |
if ($logon.Contains("U U U C($($racfHost))")) { | |
# GET USERS | |
$getUsers = Invoke-RestMethod -uri "http://$($wc3270HTTPDHost):$($wc3270HTTPDPort)/3270/rest/stext/string(%22SEARCH%20CLASS%20(USER)%22)" -UseBasicParsing -Method Get | |
#SEARCH CLASS(USER) | |
WaitforProcessing | |
if ($getUsers.Contains("U U U C($($racfHost))")) { | |
$getUsers = $null | |
$getUsers = Invoke-RestMethod -uri "http://$($wc3270HTTPDHost):$($wc3270HTTPDPort)/3270/rest/stext/enter" -UseBasicParsing -Method Get | |
$getUsers | |
WaitforProcessing | |
$exit = Invoke-RestMethod -uri "http://$($wc3270HTTPDHost):$($wc3270HTTPDPort)/3270/rest/stext/disconnect" -Method Get | |
$exit | |
} # GET USER | |
} # Password Process Confirm | |
} # Enter Password | |
} # Login ID Process | |
} #Login ID Enter | |
} # Connection | |
stopWC3270 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment