Advertisement
Guest User

UAG Functions PSH Module

a guest
Apr 6th, 2013
316
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $ErrorActionPreference = "Stop"
  2. $global:SourceFile = Join-path ((Get-ItemProperty hklm:\software\whalecom\e-gap\configuration).InstallPath) "common\conf\UAG.egf" -ErrorAction SilentlyContinue
  3. $global:BackupDir = "c:\EGFBackups"
  4.  
  5. $global:UagActivator = "C:\Scripts\UAGActivate.exe"
  6.  
  7.  
  8. function global:Set-UAGConfig(
  9.     $SourceFile, $BackupDir, $UAGActivatorPath){
  10.     if ($SourceFile){
  11.         $global:SourceFile = $SourceFile
  12.     }
  13.     if ($BackupDir){
  14.         $global:BackupDir = $BackupDir
  15.     }
  16.     if ($UAGActivatorPath){
  17.         $global:UagActivator = $UAGActivatorPath
  18.     }
  19. }
  20.  
  21. function global:Backup-UAGConfig(){
  22.     if (-not [System.IO.Directory]::Exists($global:BackupDir)){
  23.         md $global:BackupDir
  24.     }
  25.     copy $global:SourceFile (Join-path $global:BackupDir ("UAG_" + [datetime]::Now.ToString("yyyyMMdd_HHmmss") + ".egf")) -Force
  26. }
  27.  
  28. function global:Disable-UAGApp($AppName){
  29.  
  30.     $str = ([System.IO.File]::ReadAllText($SourceFile))
  31.    
  32.     #check if app exists
  33.     if (-not [regex]::IsMatch($str, ("m_csAppPortalName_[0-9]+=" + $AppName.Replace(".","\.") + "\r\n"))){
  34.         #<< raise error >>
  35.         Write-Error "Ошибка: Приложения с таким именем не существует."
  36.         return $false
  37.     }
  38.    
  39.     #BackupConf -SourceFile $global:SourceFile -BackupDir $global:BackupDir
  40.  
  41.     $regex = New-Object system.Text.RegularExpressions.Regex(("m_csAppPortalName_([0-9]+)="+$AppName.Replace(".","\.")+"\r\n"),
  42.         [System.Text.RegularExpressions.RegexOptions]::Multiline)
  43.  
  44.     $match = $regex.Match($str)
  45.     $Appid = $match.Groups[1].Value
  46.  
  47.     $str = [regex]::Replace($str, "(m_bEnabled_" + $Appid + ")=[^\r\n]+", "`$1=0")
  48.     $str = [regex]::Replace($str, "[\t]+m_nTotalAuthList_" + $Appid + "=[^\r\n]+\r\n", "")
  49.     $str = [regex]::Replace($str, "[\t]+m_csAppAuthList_" + $Appid + "_[^=]=[^\r\n]+\r\n", "")
  50.     $str = [regex]::Replace($str, "(m_csAppName_(" + $Appid + "))=[^\r\n]+", "`$1=Unused`$2")
  51.     $str = [regex]::Replace($str, "(m_csAppPortalName_(" + $Appid + "))=[^\r\n]+", "`$1=Unused`$2")
  52.     $str = [regex]::Replace($str, "(m_csAppShortDesc_(" + $Appid + "))=[^\r\n]*", "`$1=Unused`$2")
  53.     $str = [regex]::Replace($str, "(m_csAppLongDesc_(" + $Appid + "))=[^\r\n]*", "`$1=Unused`$2")
  54.    
  55.     [System.IO.File]::WriteAllText($global:SourceFile, $str)
  56. }
  57.  
  58. function global:Enable-UAGApp([int]$ProjectID, [guid]$AppGUID, $AppName, $IpAddr){
  59.    
  60.     $str = ([System.IO.File]::ReadAllText($global:SourceFile))
  61.    
  62.     #check if app exists
  63.     if ([regex]::IsMatch($str, ("m_csAppPortalName_[0-9]+=" + $AppName.Replace(".","\.") + "\r\n"))){
  64.         #<< raise error >>
  65.         Write-Error "Ошибка: Приложение с таким именем уже опубликовано."
  66.         return $false
  67.     }
  68.    
  69.     #BackupConf -SourceFile $global:SourceFile -BackupDir $global:BackupDir
  70.  
  71.     $regex = New-Object System.Text.RegularExpressions.Regex("m_csAppPortalName_([0-9]+)=Unused[0-9]+\r\n",
  72.         [System.Text.RegularExpressions.RegexOptions]::Multiline)
  73.  
  74.     $match = $regex.Match($str)
  75.     if ($match.Captures.Count -ne 1){
  76.         #<< raise error >>
  77.         Write-Error "Ошибка: Не найдено свободных слотов для публикации приложения."
  78.         return $false
  79.     }
  80.     $Appid = $match.Groups[1].Value
  81.    
  82.     $str = [regex]::Replace($str, "(m_bEnabled_" + $Appid + ")=[^\r\n]+", "`$1=1")
  83. #   if ($AppName -notlike ("P" + $ProjectID.ToString() + ".*")){
  84. #       $AppName = "P" + $ProjectID.ToString() + "." + $AppName
  85. #   }
  86.     $str = [regex]::Replace($str, "(m_csAppName_(" + $Appid + "))=[^\r\n]+", "`$1=P" + $ProjectID.ToString() + "." + $AppName)
  87.     $str = [regex]::Replace($str, "(m_csAppLongDesc_(" + $Appid + "))=[^\r\n]*", "`$1=" + $AppGUID.ToString().ToUpper().Replace("-",""))
  88.     $str = [regex]::Replace($str, "(m_csAppPortalName_(" + $Appid + "))=[^\r\n]+", "`$1=" + $AppName)
  89.     $str = [regex]::Replace($str, "(m_csAppShortDesc_(" + $Appid + "))=[^\r\n]*", "`$1=Project" + $ProjectID.ToString() )
  90.     $str = [regex]::Replace($str, "(m_csAppAdvancedSSListValue_(" + $Appid + ")_ID_0)=[^\r\n]+", "`$1=" + $AppName)
  91.     $str = [regex]::Replace($str, "(m_csAppAdvancedSSListValue_(" + $Appid + ")_ID_2)=[^\r\n]+", "`$1=" + $AppName)
  92.     $str = [regex]::Replace($str, "(m_csAppAdvancedSSListValue_(" + $Appid + ")_ID_6)=[^\r\n]+", "`$1=" + [system.Net.IPAddress]::Parse($IpAddr).ToString())
  93.     $HexIPAddress = "0x" +  [string]::Format("{0:X2}", [system.Convert]::ToByte($IpAddr.Split(".")[3])) +
  94.                             [string]::Format("{0:X2}", [system.Convert]::ToByte($IpAddr.Split(".")[2])) +
  95.                             [string]::Format("{0:X2}", [system.Convert]::ToByte($IpAddr.Split(".")[1])) +
  96.                             [string]::Format("{0:X2}", [system.Convert]::ToByte($IpAddr.Split(".")[0]))
  97.     $str = [regex]::Replace($str, "(m_csAppAdvancedSSListValue_(" + $Appid + ")_ID_8)=[^\r\n]+", "`$1=" + $HexIPAddress)
  98.     [System.IO.File]::WriteAllText($SourceFile, $str)
  99. }
  100.  
  101. function global:Set-UAGApp($AppName, $NewAppName, [int]$ProjectID, [guid]$AppGUID,  $IpAddr){
  102.    
  103.     $str = ([System.IO.File]::ReadAllText($global:SourceFile))
  104.    
  105.     #check if app exists
  106.     if (-not [regex]::IsMatch($str, ("m_csAppPortalName_[0-9]+=" + $AppName.Replace(".","\.") + "\r\n"))){
  107.         #<< raise error >>
  108.         Write-Error "Ошибка: Приложения с таким именем не существует."
  109.         return $false
  110.     }
  111.    
  112.     #BackupConf -SourceFile $global:SourceFile -BackupDir $global:BackupDir
  113.  
  114.     $regex = New-Object System.Text.RegularExpressions.Regex(("m_csAppPortalName_([0-9]+)="+ $AppName.Replace(".","\.") +"\r\n"),
  115.         [System.Text.RegularExpressions.RegexOptions]::Multiline)
  116.  
  117.     $match = $regex.Match($str)
  118.     if ($match.Captures.Count -gt 1){
  119.         #<< raise error >>
  120.         Write-Error "Ошибка: Найдено более одного приложения"
  121.         return $false
  122.     }
  123.     $Appid = $match.Groups[1].Value
  124.  
  125.    
  126.     if ($NewAppName){
  127.         $str = [regex]::Replace($str, "(m_csAppName_(" + $Appid + "))=[^\r\n]+", "`$1=P" + $ProjectID.ToString() + "." + $AppName)
  128.         $str = [regex]::Replace($str, "(m_csAppPortalName_(" + $Appid + "))=[^\r\n]+", "`$1=" + $AppName)
  129.         $str = [regex]::Replace($str, "(m_csAppAdvancedSSListValue_(" + $Appid + ")_ID_0)=[^\r\n]+", "`$1=" + $AppName)
  130.         $str = [regex]::Replace($str, "(m_csAppAdvancedSSListValue_(" + $Appid + ")_ID_2)=[^\r\n]+", "`$1=" + $AppName)
  131.     }
  132.    
  133.  
  134.     if ($AppGUID){
  135.         $str = [regex]::Replace($str, "(m_csAppLongDesc_(" + $Appid + "))=[^\r\n]*", "`$1=" + $AppGUID.ToString().ToUpper().Replace("-",""))
  136.     }
  137.    
  138.     $str = [regex]::Replace($str, "(m_csAppShortDesc_(" + $Appid + "))=[^\r\n]*", "`$1=Project" + $ProjectID.ToString() )
  139.    
  140.     if ($IpAddr){
  141.         $str = [regex]::Replace($str, "(m_csAppAdvancedSSListValue_(" + $Appid + ")_ID_6)=[^\r\n]+", "`$1=" + [system.Net.IPAddress]::Parse($IpAddr).ToString())
  142.         $HexIPAddress = "0x" +  [string]::Format("{0:X2}", [system.Convert]::ToByte($IpAddr.Split(".")[3])) +
  143.                             [string]::Format("{0:X2}", [system.Convert]::ToByte($IpAddr.Split(".")[2])) +
  144.                             [string]::Format("{0:X2}", [system.Convert]::ToByte($IpAddr.Split(".")[1])) +
  145.                             [string]::Format("{0:X2}", [system.Convert]::ToByte($IpAddr.Split(".")[0]))
  146.         $str = [regex]::Replace($str, "(m_csAppAdvancedSSListValue_(" + $Appid + ")_ID_8)=[^\r\n]+", "`$1=" + $HexIPAddress)
  147.     }
  148.     [System.IO.File]::WriteAllText($global:SourceFile, $str)
  149. }
  150.  
  151. #$UserArray - hashtable
  152. #m_csAppAuthList_X_ID as key
  153. #authObj as value
  154. #authObj = { [string] ID,
  155. #            [string] IsUser,
  156. #            [string] Repository,
  157. #            [string] UserName,
  158. #            [string] Allow
  159. #          }
  160. function global:Set-UAGAuth([string]$AppName, [hashtable]$UserArray){
  161.  
  162.     $str = ([System.IO.File]::ReadAllText($SourceFile))
  163.  
  164.     #check if app exists
  165.     if (-not [regex]::IsMatch($str, ("m_csAppPortalName_[0-9]+=" + $AppName.Replace(".","\.") + "\r\n"))){
  166.         #<< raise error >>
  167.         Write-Error "Ошибка: Приложения с таким именем не существует."
  168.         return $false
  169.     }
  170.    
  171.     #BackupConf -SourceFile $SourceFile -BackupDir $BackupDir
  172.  
  173.     #Get Application ID by Name
  174.     $regex = New-Object system.Text.RegularExpressions.Regex(("m_csAppPortalName_([0-9]+)="+$AppName.Replace(".","\.")+"\r\n"), "Multiline")
  175.     $match = $regex.Match($str)
  176.     $Appid = $match.Groups[1].Value
  177.    
  178.     #Очищаем список AuthList
  179.     while([regex]::IsMatch($str, "m_csAppAuthList_" + $Appid + "_(Allow|ID|IsUser|Repository|UserName)_[0-9]+=[^\r\n]+")){
  180.         $str = [regex]::Replace($str, "[\t]+m_csAppAuthList_" + $Appid + "_(Allow|ID|IsUser|Repository|UserName)_[0-9]+=[^\r\n]+\r\n", "")
  181.     }
  182.    
  183.     #Удаляем AuthList Count
  184.     if ([regex]::IsMatch($str, "m_nTotalAuthList_" + $AppID + "=[^\r\n]+")){
  185.         $str = [regex]::Replace($str, "[\t]+m_nTotalAuthList_" + $Appid + "=[^\r\n]+\r\n", "")
  186.     }
  187.    
  188.     #Добавляем AuthList Count
  189.     $str = [regex]::Replace($str, "(([\t]+)m_csAppPortalName_" + $AppID + "=[^\r\n]+\r\n)", "`$1`$2m_nTotalAuthList_"+ $AppID + "=" + $UserArray.Count.ToString() +"`r`n")
  190.    
  191.     #Добавляем список пользователей
  192.     $i = 0
  193.     foreach($UserAuth in $UserArray.Values){
  194.         $str = [regex]::Replace($str, "(([\t]+)m_nTotalAuthList_"+$AppID+"=[^\r\n]+\r\n)", "`$1`$2m_csAppAuthList_" + $AppID + "_Allow_" + $i + "=" + $UserAuth.Allow + "`r`n")
  195.         $str = [regex]::Replace($str, "(([\t]+)m_nTotalAuthList_"+$AppID+"=[^\r\n]+\r\n)", "`$1`$2m_csAppAuthList_" + $AppID + "_ID_" + $i + "=" + $UserAuth.ID + "`r`n")
  196.         $str = [regex]::Replace($str, "(([\t]+)m_nTotalAuthList_"+$AppID+"=[^\r\n]+\r\n)", "`$1`$2m_csAppAuthList_" + $AppID + "_IsUser_" + $i + "=" + $UserAuth.IsUser + "`r`n")
  197.         $str = [regex]::Replace($str, "(([\t]+)m_nTotalAuthList_"+$AppID+"=[^\r\n]+\r\n)", "`$1`$2m_csAppAuthList_" + $AppID + "_Repository_" + $i + "=" + $UserAuth.Repository + "`r`n")
  198.         $str = [regex]::Replace($str, "(([\t]+)m_nTotalAuthList_"+$AppID+"=[^\r\n]+\r\n)", "`$1`$2m_csAppAuthList_" + $AppID + "_UserName_" + $i + "=" + $UserAuth.UserName + "`r`n")
  199.         $i++
  200.     }
  201.  
  202.     [System.IO.File]::WriteAllText($SourceFile, $str)
  203. }
  204.  
  205. function global:Get-UAGAuth([string]$AppName){
  206.     [hashtable]$AuthList = @{}
  207.    
  208.     $str = ([System.IO.File]::ReadAllText($SourceFile))
  209.  
  210.     #check if app exists
  211.     if (-not [regex]::IsMatch($str, ("m_csAppPortalName_[0-9]+=" + $AppName.Replace(".","\.") + "\r\n"))){
  212.         #<< raise error >>
  213.         Write-Error "Ошибка: Приложения с таким именем не существует."
  214.         return $null
  215.     }
  216.    
  217.     #Get Application ID by Name
  218.     $regex = New-Object system.Text.RegularExpressions.Regex(("m_csAppPortalName_([0-9]+)=" + $AppName.Replace(".","\.") +"\r\n"), "Multiline")
  219.     $match = $regex.Match($str)
  220.     $Appid = $match.Groups[1].Value
  221.    
  222.    
  223.     #Get Auth List length
  224.     $AuthListCount = 0
  225.     $match = [regex]::Match($str, "m_nTotalAuthList_" + $AppID + "=([^\r\n])")
  226.     if ($match.Captures.Count -ne 0){
  227.         $AuthListCount = [system.Convert]::ToInt32($match.Groups[1].Value)
  228.     }
  229.    
  230.     if ($AuthListCount -eq 0){
  231.         return [hashtable]$AuthList
  232.     }
  233.    
  234.     for($i = 0; $i -lt $AuthListCount; $i++){
  235.         $authID = [regex]::Match($str, "m_csAppAuthList_" + $Appid + "_ID_" + $i + "=([^\r\n]+)").Groups[1].Value
  236.         $authIsUser = [regex]::Match($str, "m_csAppAuthList_" + $Appid + "_IsUser_" + $i + "=([^\r\n]+)").Groups[1].Value
  237.         $authRepository = [regex]::Match($str, "m_csAppAuthList_" + $Appid + "_Repository_" + $i + "=([^\r\n]+)").Groups[1].Value
  238.         $authUserName = [regex]::Match($str, "m_csAppAuthList_" + $Appid + "_UserName_" + $i + "=([^\r\n]+)").Groups[1].Value
  239.         $authAllow = [regex]::Match($str, "m_csAppAuthList_" + $Appid + "_Allow_" + $i + "=([^\r\n]+)").Groups[1].Value
  240.        
  241.         $objAuth = New-Object System.Object
  242.         $objAuth | Add-Member -Name "ID" -MemberType NoteProperty -Value $authID
  243.         $objAuth | Add-Member -Name "IsUser" -MemberType NoteProperty -Value $authIsUser
  244.         $objAuth | Add-Member -Name "Repository" -MemberType NoteProperty -Value $authRepository
  245.         $objAuth | Add-Member -Name "UserName" -MemberType NoteProperty -Value $authUserName
  246.         $objAuth | Add-Member -Name "Allow" -MemberType NoteProperty -Value $authAllow
  247.         #$objAuth.ID | Out-Host
  248.         $AuthList[$objAuth.ID] = $objAuth
  249.     }
  250.    
  251.     return [hashtable]$AuthList
  252. }
  253.  
  254. function global:Get-UAGAppList([int]$ProjectID){
  255.     [object[]]$appList = @()
  256.    
  257.     $str = ([System.IO.File]::ReadAllText($SourceFile))
  258.    
  259.     $regex = New-Object System.Text.RegularExpressions.Regex("m_csAppShortDesc_(\d+)=Project$ProjectID\r\n",
  260.         [System.Text.RegularExpressions.RegexOptions]::Multiline)
  261.    
  262.     $matches = $regex.Matches($str)
  263.    
  264.     foreach($match in $matches){
  265.         $obj = New-Object System.Object
  266.         $obj | Add-Member -Name "AppPortalName" -MemberType NoteProperty -Value `
  267.             ([regex]::Match($str, "m_csAppPortalName_" + $match.Groups[1].Value + "=([^\r\n]+)\r\n").Groups[1].Value)
  268.         $obj | Add-Member -Name "AppName" -MemberType NoteProperty -Value `
  269.             ([regex]::Match($str, "m_csAppName_" + $match.Groups[1].Value + "=([^\r\n]+)\r\n").Groups[1].Value)
  270.         $obj | Add-Member -Name "VMID" -MemberType NoteProperty -Value `
  271.             ([regex]::Match($str, "m_csAppLongDesc_" + $match.Groups[1].Value + "=([^\r\n]+)\r\n").Groups[1].Value)
  272.         $obj | Add-Member -Name "Project" -MemberType NoteProperty -Value `
  273.             ([regex]::Match($str, "m_csAppShortDesc_" + $match.Groups[1].Value + "=([^\r\n]+)\r\n").Groups[1].Value)
  274.         $obj | Add-Member -Name "IPAddress" -MemberType NoteProperty -Value `
  275.             ([regex]::Match($str, "m_csAppAdvancedSSListValue_" + $match.Groups[1].Value + "_ID_6=([^\r\n]+)\r\n").Groups[1].Value)
  276.        
  277.         $appList += $obj
  278.     }
  279.    
  280.     return $appList
  281. }
  282.  
  283. function global:Get-UAGApp([guid]$VMID, [string]$AppName){
  284.     if ($VMID -and $AppName){
  285.         Write-Error "Необходимо указать только один из параметров: VMID или AppName"
  286.         return
  287.     }
  288.    
  289.     if (-not $VMID -and -not $AppName){
  290.         Write-Error "Необходимо указать хотя бы один из параметров: VMID или AppName"
  291.         return
  292.     }
  293.    
  294.     $str = ([System.IO.File]::ReadAllText($SourceFile))
  295.    
  296.     if ($AppName){
  297.         $regex = New-Object System.Text.RegularExpressions.Regex(("m_csAppPortalName_(\d+)=" + $AppName.Replace(".","\.") + "\r\n"),
  298.             [System.Text.RegularExpressions.RegexOptions]::Multiline)
  299.     }
  300.     else {
  301.         $VMIDStr = $VMID.ToString().ToUpper().Replace("-","")
  302.         $regex = New-Object System.Text.RegularExpressions.Regex("m_csAppLongDesc_(\d+)=$VMIDStr\r\n",
  303.             [System.Text.RegularExpressions.RegexOptions]::Multiline)
  304.     }
  305.        
  306.     $matches = $regex.Matches($str)
  307.    
  308.     [object[]]$appList = @()
  309.  
  310.     foreach($match in $matches){
  311.         $obj = New-Object System.Object
  312.         $obj | Add-Member -Name "AppPortalName" -MemberType NoteProperty -Value `
  313.             ([regex]::Match($str, "m_csAppPortalName_" + $match.Groups[1].Value + "=([^\r\n]+)\r\n").Groups[1].Value)
  314.         $obj | Add-Member -Name "AppName" -MemberType NoteProperty -Value `
  315.             ([regex]::Match($str, "m_csAppName_" + $match.Groups[1].Value + "=([^\r\n]+)\r\n").Groups[1].Value)
  316.         $obj | Add-Member -Name "VMID" -MemberType NoteProperty -Value `
  317.             ([regex]::Match($str, "m_csAppLongDesc_" + $match.Groups[1].Value + "=([^\r\n]+)\r\n").Groups[1].Value)
  318.         $obj | Add-Member -Name "Project" -MemberType NoteProperty -Value `
  319.             ([regex]::Match($str, "m_csAppShortDesc_" + $match.Groups[1].Value + "=([^\r\n]+)\r\n").Groups[1].Value)
  320.         $obj | Add-Member -Name "IPAddress" -MemberType NoteProperty -Value `
  321.             ([regex]::Match($str, "m_csAppAdvancedSSListValue_" + $match.Groups[1].Value + "_ID_6=([^\r\n]+)\r\n").Groups[1].Value)
  322.         $appList += $obj
  323.     }
  324.    
  325.     return $appList
  326. }
  327.  
  328. function global:Activate-UAGConfig(){
  329.     $procObjs = Get-Process | where {$_.Name -eq "Configuration" }
  330.     if ($procObjs){
  331.         Write-Error "Ошибка. Обнаружена запущенная консоль UAG Management."
  332.         return $false
  333.     }
  334.    
  335.     $procObjs = Get-Process | where {$_.Name -eq "ConfigMgrUtil" }
  336.     if ($procObjs){
  337.         Write-Error "Ошибка. Процесс ConfigMgrUtil.exe уже запущен."
  338.         return $false
  339.     }
  340.  
  341.     $procObj = Start-Process $global:UagActivator -Wait -PassThru
  342.     $procObj.Refresh()
  343.     if ($procObj.ExitCode -ne 0){
  344.         Write-Error "Ошибка. Процесс UAGActivate.exe завершился с кодом $($procObj.ExitCode)."
  345.         return $false
  346.     }
  347.  
  348.     $procObjs = Get-Process | where {$_.Name -eq "ConfigMgrUtil" }
  349.  
  350.     if (-not $procObjs){
  351.         Write-Error "Ошибка. Не удается найти процесс ConfigMgrUtil.exe"
  352.         return $false
  353.     }
  354.     elseif ($procObjs.Count -gt 1){
  355.         Write-Error "Ошибка. Запущено более одного процесса ConfigMgrUtil.exe"
  356.         return $false
  357.     }
  358.     else {
  359.         $procObjs | Wait-Process -Timeout (25*60)
  360.         $procObjs.Refresh()
  361.         if ($procObjs.ExitCode -ne 0){
  362.             Write-Error "Ошибка. Процесс ConfigMgrUtil.exe завершился с кодом $($procObjs.ExitCode)."
  363.             return $false
  364.         }
  365.         else {
  366.             return $true
  367.         }
  368.     }
  369. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement