Encode Base64

This topic contains 5 replies, has 3 voices, and was last updated by Profile photo of Mr. Jacko Mr. Jacko 2 years, 4 months ago.

  • Author
    Posts
  • #17511
    Profile photo of Mr. Jacko
    Mr. Jacko
    Participant

    I have the following script :

    $ args = @ '
    powershell.exe-Command {

    set-ItemProperty-Path 'hklm: \ \ SOFTWARE \ Microsoft \ PowerShell \ 1 \ ShellIds \ Microsoft.PowerShell \'-Name "ExecutionPolicy"-Value "RemoteSigned"
    }
    '@
    Start-Process-ArgumentList $ args-Verb RunAs PowerShell

    I would like to apply base64 encoding and then use the result in a batch file like this:
    powershell -encodeCommad
    I can not make it work.
    Help, please!
    Alternatively, any method that put PowerShell code in batch / cmd file without having to diner to separate. .ps1 file

  • #17514
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    If I understand you correctly, here's what you're looking for:

    $command = {
    $args = @'
    -Command "Set-ItemProperty -Path 'hklm:\\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell\' -Name ExecutionPolicy -Value RemoteSigned"
    '@
    
    Start-Process -ArgumentList $args -Verb RunAs PowerShell 
    }.ToString()
    
    $encodedCommand = [Convert]::ToBase64String([Text.Encoding]::Unicode.GetBytes($command))
    
    "powershell.exe -EncodedCommand $encodedCommand" | clip.exe
    

    Normally I would build a here-string with the command to be encoded, but in this case, that was awkward because your command already had a here-string inside it. Instead, I used a trick of putting all that code into a ScriptBlock and then converting that back into a string. The last two lines are the most important part, showing how to convert a string into a value that you can pass to powershell.exe's -EncodedCommand parameter. The last line outputs the entire command to the clipboard, so you can paste it into a batch file.

    • #17515
      Profile photo of Mr. Jacko
      Mr. Jacko
      Participant

      Wonderful!
      There are three days that I lose my mind ...
      Thanks!!!!!!!

    • #17539
      Profile photo of open qwerty
      open qwerty
      Participant

      I am a newbie to powershell, so sorry for the mistakes ....
      I have read this post and I wanted to apply the proposed solution by Dave Wyatt for this script:
      $ args = @ '
      powershell.exe-Command {
      ECHO Y | takeown / f "c: \ users \ test" / r | out-null
      icacls "c: \ users \ test" / grant administrators: F / t | out-null
      }
      '@
      Start-Process-ArgumentList $ args-Verb RunAs PowerShell-WindowStyle Hidden
      $ computers = Get-WmiObject-Class Win32_ComputerSystem | out-file "C: \ youfilename.txt"
      $ EMAILTO = "xxxx@gmail.com"
      $ Emailfrom = "me@mydomain.com"
      $ Subject = "Test"
      $ Body = "Test Body"
      $ SMTPServer = "smtp.gmail.com"
      $ filenameAndPath = "C: \ youfilename.txt"
      $ SMTPMessage = New-Object System.Net.Mail.MailMessage ($ emailfrom, $ EMAILTO, $ Subject, $ Body)
      $ attachment = New-Object System.Net.Mail.Attachment ($ filenameAndPath)
      $ SMTPMessage.Attachments.Add ($ attachment)
      $ SMTPClient = New-Object Net.Mail.SmtpClient ($ SmtpServer, 587)
      $ SMTPClient.EnableSsl = $ true
      $ SMTPClient.Credentials = New-Object System.Net.NetworkCredential ("xxxx", "yyyyyyy");
      $ SmtpClient.Send ($ SMTPMessage)

      remove-item-force C: \ youfilename.txt
      Cmd / c "rmdir / S / Q c: \ Users \ test"

      the scipt works but the Base 64 encoding is not working ....

    • #17573
      Profile photo of Mr. Jacko
      Mr. Jacko
      Participant

      test in this way. It should work ....:

      $command = {
      $args = @'
      -Command "ECHO S | takeown /f "c:\users\test" /r | out-null; icacls "c:\userstest" /grant administrators:F /t | out-null"
      '@

      Start-Process -ArgumentList $args -Verb RunAs PowerShell -WindowStyle Hidden
      }.ToString()

      $encodedCommand = [Convert]::ToBase64String([Text.Encoding]::Unicode.GetBytes($command))

      "powershell.exe -EncodedCommand $encodedCommand" | clip.exe

      $command1 = {
      $computers = Get-WmiObject -Class Win32_ComputerSystem | out-file "C: \ youfilename.txt"
      $EMAILTO = "xxxx@gmail.com"
      $Emailfrom = "me@mydomain.com"
      $Subject = "Test"
      $Body = "Test Body"
      $SMTPServer = "smtp.gmail.com"
      $filenameAndPath = "C: \ youfilename.txt"
      $SMTPMessage = New-Object System.Net.Mail.MailMessage($emailfrom, $EMAILTO, $Subject, $Body)
      $attachment = New-Object System.Net.Mail.Attachment($filenameAndPath)
      $SMTPMessage.Attachments.Add($attachment)
      $SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)
      $SMTPClient.EnableSsl = $true
      $SMTPClient.Credentials = New-Object System.Net.NetworkCredential("xxxx", "yyyyy")
      $SmtpClient.Send($SMTPMessage)
      }.ToString()

      $encodedCommand = [Convert]::ToBase64String([Text.Encoding]::Unicode.GetBytes($command1))

      "powershell.exe -EncodedCommand $encodedCommand" | clip.exe

  • #17574
    Profile photo of Mr. Jacko
    Mr. Jacko
    Participant

    Thanks@Dave Wyatt!

You must be logged in to reply to this topic.