Netgate Discussion Forum
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Search
    • Register
    • Login

    Trying to upload XML files with Powershell

    Scheduled Pinned Locked Moved General pfSense Questions
    3 Posts 1 Posters 579 Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • P
      ps-dgapinski
      last edited by ps-dgapinski

      Hello, I am trying to powershell my way into uploading XML files to one or many SG-3100's, and I am a little stuck on the page action. Since it is showing as a form with 2 sections, I am unsure that my code is doing the correct action, and well, it is not uploading. Here is how I'm trying to do it (after making the session, which does connect successfully):

      $Arguments = @{
                         __csrf_magic=$LoopCsrfToken;
                         restorearea=$RestoreTHIS.Value;
                         conffile = $RestoreFile;
                         decrypt='yes';
                         restore='restore'
                     }
                             
      $LoopResult = Invoke-WebRequest -TimeoutSec $Timeout -WebSession  $LoopSession -Uri "https://$Luri/diag_backup.php" -Method Post -Body $LoopArguments -InFile $LoopRestoreFile
                     
      

      Here is the contents of $Arguments variable:

      Name : decrypt
      Value : yes

      Name : restorearea
      Value : aliases

      Name : restore
      Value : restore

      Name : conffile
      Value : C:\path\aliasestest.xml

      Name : __csrf_magic
      Value : sid:091673177ee74bbeb3b0c6c15f34135bb8e53921,1599253539

      Is there anything glaringly wrong about my method, URL or arguments?
      Thanks for your help!

      Dan

      1 Reply Last reply Reply Quote 0
      • P
        ps-dgapinski
        last edited by

        After taking another look at the webpage code here, I see that it is only a standard form for the backup/download config area, and the restore area does not have a post action, so Powershell doesn't like that at all. I read on Reddit a little talk about this working after PS v6.1, so I'm investigating that now.

        1 Reply Last reply Reply Quote 0
        • P
          ps-dgapinski
          last edited by ps-dgapinski

          OK I think I found the way. For all the time I spent trying to figure it out, it's embarrasingly simple, but I took longer because I did not understand that:

          1. it needs powershell 7 (or maybe 6+, but I didn't test that)
          2. it needs to NOT run in ISE. Running in ISE screws up the type used for the file in the final hash table, even if you try to run PS 7 with enter-pssession. You've been warned! :-)

          Anyway, this sample code worked for me - hope it helps someone else:

          $Timeout = 15
          $restorearea='aliases'
          $conffile='c:\path\aliasestest.xml'
          $CsrfToken = $null;
          $PW = 'pfsense'
          $Uri = 'https://192.168.1.1'
          
          $LoginPage = Invoke-WebRequest -TimeoutSec $Timeout -Uri $Uri -SessionVariable Session 
          $CsrfToken = $LoginPage.InputFields.FindByName('__csrf_magic').Value
          
          $Credential = New-Object System.Management.Automation.PSCredential -ArgumentList 'admin', (ConvertTo-SecureString -AsPlainText -Force ($PW))
          $Creds = @{
          	__csrf_magic=$CsrfToken;
          	usernamefld=$Credential.GetNetworkCredential().UserName;
          	passwordfld=$Credential.GetNetworkCredential().Password;
          	login='Login'
          	}
                      
          # Login to web portal
          $Result = Invoke-WebRequest -TimeoutSec $Timeout -WebSession $Session -Uri $uri -Method Post -Body $Creds 
          $CsrfToken = $Result.InputFields.FindByName('__csrf_magic').Value
          
          # Get backup pagethat 
          $Result = Invoke-WebRequest -TimeoutSec $Timeout -WebSession $Session -Uri "$uri/diag_backup.php" 
          $CsrfToken = $Result.InputFields.FindByName('__csrf_magic').Value
          
          $RestoreArguments =  @{
              __csrf_magic=$CsrfToken
              donotbackuprrd='yes'
              encrypt_password=''
              conffile=get-item -path $conffile
              decrypt_password=''
              restorearea=$RestoreArea
              backuparea=''
              restore='Restore Configuration'
              }
          $Result = Invoke-WebRequest -TimeoutSec $Timeout -WebSession $Session -Uri "$uri/diag_backup.php" -Method 'POST' -form $RestoreArguments 
          
          
          
          1 Reply Last reply Reply Quote 0
          • First post
            Last post
          Copyright 2025 Rubicon Communications LLC (Netgate). All rights reserved.