none
Scripting searching for certs on file RRS feed

  • General discussion

  • I've written the following script that searches a drive that the user gives the script copies them to a directory then at a later point runs a function using to whatever is found in those directories to a .pem file. I keep having errors with my function converting files. Can anyone offer feedback on my script as to ways to improve it or what directions I might need to go with it


    Wednesday, March 21, 2018 8:22 PM

All replies

  • Your post is filled with garbage characters.

    -- Bill Stewart [Bill_Stewart]

    Wednesday, March 21, 2018 8:57 PM
  • Please fix your post.  It is filled with HTML and CSSS formatting strings.  This makes it unreadable.

    DO not paste formatted text into the editor as it will do this.  Don't use Word, Outlook, Notepad++ or any other formatting editor to paste text.  THe HTML will be included in your paste.

    Some older versions of Fox and Chrome have similar issues when posting to forms.


    \_(ツ)_/

    Wednesday, March 21, 2018 10:23 PM

  • #Variables 
    $Global:pemfiles = "Get-ChildItem $env:HOMEDRIVE\OpenSSL-Win64\bin\pemfolder"
    $Global:cerfiles = "Get-ChildItem $env:HOMEDRIVE\OpenSSL-Win64\bin\cerfolder"
    $Global:p12files = "Get-ChildItem $env:HOMEDRIVE\OpenSSL-Win64\bin\p12folder"
    $Global:pfxfiles = "Get-ChildItem $env:HOMEDRIVE\OpenSSL-Win64\bin\pfxfolder"


    #Getting user input on which drive is to be searched for certs
    Read-host -Prompt 'Please ensure you have read the readme file before running this script!'
    $drive = Read-Host -Prompt 'What <g class="gr_ gr_42 gr-alert gr_gramm gr_inline_cards gr_disable_anim_appear Grammar multiReplace" data-gr-id="42" id="42">drive</g> are the certs on? example C:\ D:\ F:\ Press Enter to Continue '

    #Print to user on which drive will be searched for certs
    Write-Host "Your Drive letter is '$drive' " 
    Write-Host "You may see error messages related to Permissions this is normal please continue to let the script run"
    mkdir $env:HOMEDRIVE\OpenSSL-Win64\bin\cerfolder 2> $null
    mkdir $env:HOMEDRIVE\OpenSSL-Win64\bin\p12folder 2> $null
    mkdir $env:HOMEDRIVE\OpenSSL-Win64\bin\pfxfolder 2> $null

    #Searching for  files  entire file system on drive provided by user and copying them to users home drive
    Get-ChildItem $drive\*.cer -Recurse | Move-Item -Destination $env:HOMEDRIVE\OpenSSL-Win64\bin\cerfolder 2> $null
    Get-ChildItem $drive\*.p12 -Recurse | Move-Item -Destination $env:HOMEDRIVE\OpenSSL-Win64\bin\p12folder 2> $null
    Get-ChildItem $drive\*.pfx -Recurse | Move-Item -Destination $env:HOMEDRIVE\OpenSSL-Win64\bin\pfxfolder 2> $null
    # Confirming with user these are the files he wants to convert if no it exits and gives in instructions on how to move forward 

    Invoke-Expression $cerfiles
    Invoke-Expression $p12files
    Invoke-Expression $pfxfiles

    #Providing user instructions on which directories to inspect 
    Write-Host "These files will be converted to files is this ok?"
    Write-host "Please review  $env:HOMEDRIVE\OpenSSL-Win64\bin\p12folder $env:HOMEDRIVE\OpenSSL-Win64\bin\cerfolder $env:HOMEDRIVE\OpenSSL-Win64\bin\pfxfolder and ensure those files are the ones you want to have converted"
    #Provide user an <g class="gr_ gr_33 gr-alert gr_spell gr_inline_cards gr_disable_anim_appear ContextualSpelling ins-del multiReplace" data-gr-id="33" id="33">oppurinity</g> to answer if files are the way they want them

    Read-host -Prompt "Have you reviewed $env:HOMEDRIVE\OpenSSL-Win64\bin\pemfolder, $env:HOMEDRIVE\OpenSSL-Win64\bin\p12folder $env:HOMEDRIVE\OpenSSL-Win64\bin\cerfolder $env:HOMEDRIVE\OpenSSL-Win64\bin\pfxfolder and ensure those files are the ones you want to have converted"
    Read-host 'Match Exact Sytanx Yes or No' -OutVariable answer 

    # If then statement to match user answer if no exits and has them <g class="gr_ gr_34 gr-alert gr_spell gr_inline_cards gr_disable_anim_appear ContextualSpelling ins-del multiReplace" data-gr-id="34" id="34">reun</g> if yes continues on <g class="gr_ gr_35 gr-alert gr_gramm gr_inline_cards gr_disable_anim_appear Grammar only-ins doubleReplace replaceWithoutSep" data-gr-id="35" id="35">script</g> 
    if ($answer -match "No") 
    {
        Write-Host "files need to be reviewed before continuing please delete files you do not want to convert and re-run script"
        Start-Sleep -Seconds 10
        exit
    }


    convert 
    #Function to conver .cer,.pem,.p12 and .pfx certs to .pem 
    function convert {

    Get-ChildItem $env:HOMEDRIVE\OpenSSL-Win64\bin\cerfolder\*.cer| ForEach-Object {
      certutil -encode $_.FullName ("{0}\{1}.pem" -f $_.DirectoryName,$_.BaseName)
    }

    Get-ChildItem $env:HOMEDRIVE\OpenSSL-Win64\bin\p12folder\*.p12| ForEach-Object {
      certutil -encode $_.FullName ("{0}\{1}.pem" -f $_.DirectoryName,$_.BaseName)
    }

    Get-ChildItem $env:HOMEDRIVE\OpenSSL-Win64\bin\pfxfolder\*.pfx| ForEach-Object {
      certutil -encode $_.FullName ("{0}\{1}.pem" -f $_.DirectoryName,$_.BaseName)
    }
    }



    Thursday, March 22, 2018 12:31 PM
  • You post contains a bunch of garbage characters.

    You also need to ask a specific question. "Can anyone offer feedback" is not specific enough (feedback about what, specifically?).

    We recommend posting a short sample script that contains only the minimum amount of code needed to reproduce the problem (the sscce principle). Explain what you are trying to do with the code, what results you expected, and the specific problem (exact errors please, not "it didn't work").

    When posting your question, please post plain-text. For code, use the "insert code block" tool. The program you are pasting from or something is inserting junk into the text that makes it unreadable.


    -- Bill Stewart [Bill_Stewart]


    Thursday, March 22, 2018 2:01 PM
  • Sorry about the garbage characters there was an extension on my browser that was adding them in. As far as the posting of the overall code currently one major issue I am having is related to invokeing a command I have stored in a variable. I have tried using single ' and double " around the command and not sure what is really causing my issue there.

    $Global:cerfiles = Get-ChildItem $env:HOMEDRIVE\OpenSSL-Win64\bin\cerfolder $Global:p12files = Get-ChildItem $env:HOMEDRIVE\OpenSSL-Win64\bin\p12folder $Global:pfxfiles = Get-ChildItem $env:HOMEDRIVE\OpenSSL-Win64\bin\pfxfolder Invoke-Expression $Global:cerfiles Invoke-Expression $Global:p12files Invoke-Expression $Global:pfxfiles

    S C:\Users\suitable\Documents\Powershellscripts> Invoke-Expression $Global:cerfiles
    Invoke-Expression : Cannot bind argument to parameter 'Command' because it is null.
    At line:1 char:19
    + Invoke-Expression $Global:cerfiles
    +                   ~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidData: (:) [Invoke-Expression], ParameterBindingValidationException
        + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.InvokeExpressionCommand


    Thursday, March 22, 2018 3:27 PM
  • Why are you using Invoke-Expression?

    -- Bill Stewart [Bill_Stewart]

    Thursday, March 22, 2018 3:56 PM