Microsoft Forefront TechCenter > Forefront Forums > Forefront TMG and ISA Server > powershell scripting for Forefront TMG ?

Answered powershell scripting for Forefront TMG ?

  • Wednesday, May 05, 2010 5:34 AM
     
     
    I noticed that most of modern Microsoft apps have support for Powershell. can anybody give me an example how to "Export (Back Up) Array Configuration" with Powershell ?

Answers

  • Wednesday, May 05, 2010 7:44 AM
    Moderator
     
     Answered Has Code

    Here is an example taken from Microsoft Forefront Threat Management Gateway (TMG) Administrator's Companion which I hope doesn't infringe any copyrights!

    #-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
    #
    #  This code is Copyright (c) 2009 Microsoft Corporation. 
    #
    #  All rights reserved.
    #
    #  THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
    #  ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
    #  THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
    #  PARTICULAR PURPOSE.
    #
    #  IN NO EVENT SHALL MICROSOFT AND/OR ITS RESPECTIVE SUPPLIERS BE
    #  LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
    #  DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
    #  WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
    #  ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
    #  OF THIS CODE OR INFORMATION.
    #
    #-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
    
    # declare and define the TMG root object
    $oFPC = New-Object -comObject FPC.root
    
    # declare and define the value that expresses if array work was successful
    $bFailed = $False
    
    # declare and define the value that expresses whether any changes occurred
    $bChanges = $False
    
    # declare and define the TMG arrays collection
    $cArrays = $oFPC.Arrays
    
    # declare the Array object variable
    $oArray
    
    # enumerate (walk through) the array list looking for the ones of interest
    $Result = "Outbound Proxy" 
    
    Foreach ($oArray in $cArrays)
    {
    write-host "Policy name:" $oArray.PolicyAssignment.EnterprisePolicyUsed.Name
      If ( $oArray.PolicyAssignment.EnterprisePolicyUsed.Name -eq $Result){
        write-host "Array Name:" $oArray.name
        # if we try to work an array and fail, we want to quit now
        If ( UpdateArray($oArray) -eq $false ) 
             {$bFailed = $True}
        # otherwise, we declare that we made changes to at least one array
             else {$bChanges = $True}
            }
     }
     # if we made any changes without failures, now is the time to save them
    If ($bChanges -ne ( $bFailed )) 
        {SaveChanges( $oArray )}
      {SaveChanges( $oArray )}
    
    Function UpdateArray( $oArray )
    {
      #define the default return value for this function
      $UpdateArray = $False
    
      #declare and define the TMG export file path
      $szOutFilePath = "C:\TmgExportFile.xml"
    
      #declare and define the optional data for the export method
      $iOptionalData = 0
    
      #declare and define the TMG export data password
      $szPassword = ""
    
      #declare and define the TMG export file comment section
      $szComment = "Exported by ExportArrays.ps1 at " 
    
      #try to export the current configuration to a file
      write-host "Name:" $oArray.Name
      $oArray.ExportToFile($szOutFilePath, $iOptionalData, $szPassword, $szComment)
    
      #disable script error handling
      $bOverwrite= $False
    
      #declare and define the TMG import services reset flag
      $bReset = $False
    
      #declare and define the TMG import policy reload flag
      $bReload = $True
    
      #declare and define the TMG import file path
      $szInFilePath = "C:\TmgImportFile.xml"
    
      #try to import the configuration update from a file
      $oArray.ImportFromFile($szInFilePath, $iOptionalData, $szPassword, $bOverwrite, $bReset, $bReload)
      $UpdateArray = $True
    }
    
    Function SaveChanges( $oArray )
    {
    	Trap [Exception] 
      {
    	write-host "Failed to save the array configuration changes; " $_.Exception.GetType().FullName "; " $_.Exception.Message 
    
      	# if it fails, tell the user and bail out  
    	write-host "Failed to save the array configuration changes; " $_.Exception.GetType().FullName "; " $_.Exception.Message
    	Exit
    	} 
    
      # define the default value of this function
      $SaveChanges = $False
      $oArray.Save
    
      # no failures, return "true"
      $SaveChanges = $True
      write-host "Changes Saved"
    }
    

    Jason Jones | Forefront MVP | Silversands Ltd | My Blogs: http://blog.msedge.org.uk and http://blog.msfirewall.org.uk