# ================================================================= # # Windows PowerShell script for NDACC file transfer to NASA # # call in Powershell (powershell.exe), assuming curl2nasa.ps1 is in current directory (otherwise give full path) # # PS D:> curl2nasa.ps1 -filepath d:\my_path\my_file -email joe.slow@nuts.bolts.com -logon_auto xrtzlprmpflogoncredentialpfrt # # Below in the script you can set -email and -logon_auto to your correct values. Then you can omit them on the command line. # Then just call # # PS D:> curl2nasa.ps1 -filepath d:\my_path\my_file # # # Call from DOS shell (cmd.exe), assuming curl2nasa.ps1 is in current directory (otherwise give full path) # # D:> powershell -File .\curl2nasa.ps1 -filepath d:\my_path\my_file -email joe.slow@nuts.bolts.com -logon_auto xrtzlprmpflogoncredentialpfrt # # as above, the values for -email and -logon_auto can also be set directly in the script curl2nasa.ps1. In that case just call # # D:> powershell -File .\curl2nasa.ps1 -filepath d:\my_path\my_file # # ------------------------------------------------------------------------------------- # Windows workaround for the linux curl command # see also https://docs.microsoft.com/de-de/powershell/module/Microsoft.PowerShell.Utility/Invoke-WebRequest?view=powershell-7&viewFallbackFrom=powershell-3.0 # # a lot of things are missing / do not work in older PowerShell versions # # May 2020, wolfgang.steinbrecht@dwd.de # # ================================================================= # ================================================================= # # command line parameters of script: # # -FilePath D:\my_path\my_file filename, including full path, of the file to be transfered # -email joe.slow@nuts.bolts.com email address to be passed to NASA server # -logon_auto xrtzlprmpflogoncredentialpfrt logon credentials for NASA server # # the values in param( ... ) are default parameters # change those to your values, then you don't need to give them # as command line parameters # ================================================================= param ( # # filename with full path should really come as command lien parameters # (unless you always have the same filename, e.g. zip-Archive) # $FilePath = 'd:\tmp\groundbased_lidar.temperature_dwd001_hohenpeissenberg_20191026t174700z_20191027t050309z_001.hdf', # # submitter's email, set to your value # $email='joe.slow@nuts.bolts.com', #set to your value # # auto_logon credentials, set to your value # $logon_auto="xrtzlprmpflogoncredentialpfrt" #set to your value ) # ================================================================= # # initialize a few things # # ================================================================= # # set the https Tls protocol version, the default version (TLSv1) always resulted in error # "cannot setup safe SSL/TLS-channel" # $AllProtocols = [System.Net.SecurityProtocolType]'Tls11,Tls12' [System.Net.ServicePointManager]::SecurityProtocol = $AllProtocols # # NASA server URL # $Uri = "https://www-air.larc.nasa.gov/cgi-bin/NDscan?auto+$email" # # File to be transmitted # # # Lidar Files # #$FilePath = 'd:\tmp\groundbased_lidar.temperature_dwd001_hohenpeissenberg_20191026t174700z_20191027t050309z_001.hdf' #$FilePath = 'd:\tmp\hote2001.sbl' #$FilePath = 'd:\tmp\hoo32001.sbl' #$FilePath = 'd:\tmp\lidar_hdf.zip' # # Sonde Files # #$FilePath = 'd:\tmp\ho200327.b06' #$FilePath = 'd:\tmp\ho200330.b05' # # read the (binary) File to be transmitted into byte array # $fileBin = Get-Content -Path $FilePath -Encoding Byte -Raw $FileName = Split-Path -leaf $FilePath # ================================================================= # # Build HTTP multipart/form-data # # PowerShell does not (yet) have built-in support for making 'multipart' (i.e. binary file upload compatible) # form uploads. So we have to craft one... # # This is doing similar to: # $ curl -i -F "file=@file.any" -F "computer=MYPC" http://url # # Boundary is anything that is guaranteed not to exist in the sent data (i.e. string long enough) # # Note: The protocol is very precise about getting the number of line feeds correct (both CRLF or LF work). # # ================================================================= # # stuff for string encoding # use codepage iso-8859-1 which has 1 byte per character # and allows 256 values per byte / character # (ASCII won't work, UFT-8 won't work) $CODEPAGE = "iso-8859-1" # alternatives are ASCII, UTF-8, iso-8859-1 $enc = [System.Text.Encoding]::GetEncoding($CODEPAGE) # Convert byte-array to string, iso-8859-1 just passes all bytes 1 to 1 $fileEnc = $enc.GetString($fileBin) # Linefeed character $LF = "`r`n" # We need a boundary (something random() will do best) $boundary = [System.Guid]::NewGuid().ToString() # # make one long string: header + (binary) file + trailer # $bodyLines = ( "--$boundary", "Content-Disposition: form-data; name=`"LOCALFILE`"; filename=`"$FileName`"", "Content-Type: application/octet-stream$LF", $fileEnc, "--$boundary", "Content-Disposition: form-data; name=`"NDACC_LOGON`"$LF", "Submit", "--$boundary", "Content-Disposition: form-data; name=`"LOGON-AUTO`"$LF", $logon_auto, "--$boundary--$LF" ) -join $LF # # for checking: write bodylines to (binary) temporary file # #$binFile="$Filepath.bin" #[io.file]::WriteAllBytes($binFile, $enc.GetBytes($bodyLines)) # # send WebRequest to NASA server using -Body # $Req = Invoke-WebRequest -Uri $uri -Method Post -ContentType "multipart/form-data; boundary=$boundary" -Body $bodyLines -OutFile "$FilePath.html" -PassThru # # show key parts / lines of the response that came back from the Nasa Server # usually we get http: Status: 200 OK # # and then we use grep to grab lines with important keywords # "http: Status: "+$Req.StatusCode+" "+$Req.StatusDescription $Req.Content | grep "ailed" $Req.Content | grep "atalog" $Req.Content | grep "acceptable"