Trying to upload XML files with Powershell
-
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 : yesName : restorearea
Value : aliasesName : restore
Value : restoreName : conffile
Value : C:\path\aliasestest.xmlName : __csrf_magic
Value : sid:091673177ee74bbeb3b0c6c15f34135bb8e53921,1599253539Is there anything glaringly wrong about my method, URL or arguments?
Thanks for your help!Dan
-
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.
-
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:
- it needs powershell 7 (or maybe 6+, but I didn't test that)
- 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