28 August 2020

How to install with WinGet the easy way - The winstall.app way

By using the new WinGet package manager you can use PowerShell to install all the software you need.

But how to use the command's in the correct way?

The site https://winstall.app let's you select all the apps you need and generate an install script for you to install them all at once.

Lets say you want to install, Spotify, Google Chrome, Microsoft Teams, 7Zip and Windows Terminal

Go to the site an search the apps you want and click:

The below command is showed, run it in an elevated PowerShell window and voila all your apps are installed silently and in the version you selected (if applicable)

winget install --id=Spotify.Spotify -e ; winget install --id=Google.Chrome -e ; winget install --id=Microsoft.Teams -e ; winget install --id=7zip.7zip -e ; winget install --id=Microsoft.WindowsTerminal -e

27 August 2020

How to Send Email with Office365 and PowerShell

This is nice and handy.
I came across this at Adam the Automator, it's 2 ways to send mail with PowerShell.

One is with authenticated send the other with direct send.

The authenticated way:

# Get the credential            
$credential = Get-Credential            
            
## Define the Send-MailMessage parameters            
$mailParams = @{            
    SmtpServer                 = 'smtp.office365.com'            
    Port                       = '587' # or '25' if not using TLS            
    UseSSL                     = $true ## or not if using non-TLS            
    Credential                 = $credential            
    From                       = 'sender@yourdomain.com'            
    To                         = 'recipient@yourdomain.com', 'recipient@NotYourDomain.com'            
    Subject                    = "SMTP Client Submission - $(Get-Date -Format g)"            
    Body                       = 'This is a test email using SMTP Client Submission'            
    DeliveryNotificationOption = 'OnFailure', 'OnSuccess'            
}            
            
## Send the message            
Send-MailMessage @mailParams
The direct send way:
## Build parameters            
$mailParams = @{            
    SmtpServer                 = '.mail.protection.outlook.com'            
    Port                       = '25'            
    UseSSL                     = $true               
    From                       = 'sender@yourdomain.com'            
    To                         = 'recipient@yourdomain.com'            
    Subject                    = "Direct Send $(Get-Date -Format g)"            
    Body                       = 'This is a test email using Direct Send'            
    DeliveryNotificationOption = 'OnFailure', 'OnSuccess'            
}            
            
## Send the email            
Send-MailMessage @mailParams
See the full blogpost here

21 August 2020

Messages that are sent by using the "Send As" and "Send on behalf" permissions are copied only to the Sent Items folder of the sender

UPDATED 21-08-2020 - Added Exchange Online method


This is something I didn't know existed but keeps popping up as a frequent question in our organization.

An outlook user with access to a shared mailbox, send as or send on behalf of rights sends an email from the shared mailbox, but the sent email doesn't get saved in the shared mailbox sent items folder but in the users sent items folder.

Since Exchange server 2010 SP2 update roll-up 4 it's possible to specify where the sent emails will be saved. This also goes for Exchange server 2013 CU9 and up.

First check the current settings for a shared mailbox:
Get-MailboxSentItemsConfiguration -Identity sharedmailboxname            
            
RunspaceId                  : 2d911aef-3416-42f9-9900-531d0fcdea94            
SendAsItemsCopiedTo         : Sender            
SendOnBehalfOfItemsCopiedTo : Sender            
Identity                    :            
IsValid                     : True
As you can see the "SendasItemsCopiedTo" and "SendOnBehalfOfItemsCopiedTo" is set to "Sender"
There are 2 settings to choose from: "Sender" "SenderAndFrom".

To set it to save in both mailboxes use:
Set-MailboxSentItemsConfiguration -Identity sharedmailboxname -SendAsItemsCopiedTo `
senderandfrom -SendOnBehalfOfItemsCopiedTo SenderAndFrom
Check to be sure:
Get-MailboxSentItemsConfiguration -Identity sharedmailboxname            
            
RunspaceId                  : 2d911aef-3416-42f9-9900-531d0fcdea94            
SendAsItemsCopiedTo         : SenderAndFrom            
SendOnBehalfOfItemsCopiedTo : SenderAndFrom            
Identity                    :            
IsValid                     : True
Set it to all shared mailboxes in a nice one liner for Exchange 2010:
Get-Mailbox -ResultSize Unlimited -RecipientTypeDetails sharedmailbox | Set-MailboxSentItemsConfiguration `
-SendAsItemsCopiedTo SenderandFrom -SendOnBehalfOfItemsCopiedTo SenderAndFrom
Set it to all shared mailboxes in a nice one liner for Exchange 2013:
Get-Mailbox -ResultSize Unlimited -RecipientTypeDetails sharedmailbox | Set-Mailbox `
-MessageCopyForSendOnBehalfEnabled $True -MessageCopyForSentAsEnabled $True
For Exchange Online there are two ways of doing this, in is in the Office 365 Admin portal and the other is in PowerShell.

In the Portal go to Groups - Shared Mailboxes

Then find the Shared mailbox you want to edit and edit "Sent Items"

And here you will see the two toggles to choose you options

With PowerShell:

After loggin in to Exchange Online Check the shared mailbox you want to modify:

Get-EXOMailbox -Identity sharedmailbox@domain.com | FL

These are the attributes that we triggered from the GUI toggle within the Office365 Admin Center earlier:

MessageCopyForSentAsEnabled : True
MessageCopyForSendOnBehalfEnabled : True

Set these attributes for a single mailbox:
Set-EXOMailbox -Identity sharedmailbox@domain.com -MessageCopyForSendOnBehalfEnabled $true -MessageCopyForSentAsEnabled $true

Enable these settings on all your shared mailboxes in your environment:
Get-EXOMailbox -RecipientTypeDetails shared | Where-Object {$_.messagecopyforsentasenabled -eq "" }

If you have to return all shared mailboxes that has the Sent Items Copy enabled:
Get-EXOMailbox -RecipientTypeDetails shared | Where-Object {$_.messagecopyforsentasenabled -eq "true" }

Take the selection of the shared mailboxes that don’t have the Sent Items Copy enabled yet and combine that with the Set-Mailbox command that enables both Sent Items Copy attributes:

Get-EXOMailbox -RecipientTypeDetails shared | Where-Object{$_.messagecopyforsentasenabled -eq ""} | Set-EXOMailbox -MessageCopyForSendOnBehalfEnabled $true -MessageCopyForSentAsEnabled $true


20 August 2020

Bulk create certificates from .CSR to .CER with root and intermediate included with PowerShell (quick and dirty)

This script was born after I needed to create over 200 certificates requested by .csr files.

The regular way of pasting the content of the .csr into the certsrv site was to time consuming, boring and repetitive to do by hand.

But I couldn't find a script or method that would work for what I needed.

I had hoped that the famous PSPKI module by Vadims Podāns (https://www.pkisolutions.com/tools/pspki/) could help me out, and in a way it could but not entirely how I wanted it. Turned out the "foreach" I tried couldn't pass trough the "Submit-CertificateRequest" commandlet.

So this is what I came up with it requires some Notepad++ find and replace, it's a bit quick and dirty but does the job.

If somebody knows how to make this better, please drop a message.

It requires the PSPKI, found here or from PowerShell: 

Install-Module -Name PSPKI

The script:
Import-Module pspki            
            
cd C:\Scripts\Get-CertificateFromCSR\csr            
            
$PKI = "YourCAServer.domain.lan"            
$OutPath = "C:\Scripts\Get-CertificateFromCSR\IssuedCertificates"            
            
$status = Submit-CertificateRequest -path "C:\Scripts\Get-CertificateFromCSR\CSR\Certificate01.csr" -CA $PKI -Attribute "CertificateTemplate:Webserver"             
$ReqID = $status.requestid            
Get-IssuedRequest -RequestID $reqID -CertificationAuthority $PKI | Receive-Certificate -Path $OutPath\Certificate01 -Force            
cd $outpath\Certificate01            
$item = ls            
Rename-Item -path $item -newname Certificate01.cer            
$status = Submit-CertificateRequest -path "C:\Scripts\Get-CertificateFromCSR\CSR\Certificate02.csr" -CA $PKI -Attribute "CertificateTemplate:Webserver"             
$ReqID = $status.requestid            
Get-IssuedRequest -RequestID $reqID -CertificationAuthority $PKI | Receive-Certificate -Path $OutPath\Certificate02 -Force            
cd $outpath\Certificate02            
$item = ls            
Rename-Item -path $item -newname Certificate02.cer

Etc, etc, etc

04 June 2020

How to create key file and certificate file from pfx - Openssl

Install the Windows version of Openssl from:
https://slproweb.com/download/Win64OpenSSL-3_6_0.msi
https://slproweb.com/download/Win64ARMOpenSSL-3_6_0.msi

Open a command prompt window in c:\Program Files\OpenSSL\Bin
(or even better, add Openssl to your Path)

Then type:

For the key file:
openssl pkcs12 -in [yourfile.pfx] -nocerts -out [keyfile-encrypted.key]
Example:
openssl pkcs12 -in c:\temp\mycertificate.pfx -nocerts -out c:\temp\keyfile-mycertificate.key

You will be asked for the pfx password (import password), and then asked to enter a password for the .key file (PEM pass phrase)

For the certificate:
openssl pkcs12 -in [yourfile.pfx] -clcerts -nokeys -out [certificate.crt]
Example:
openssl pkcs12 -in c:\temp\mycertificate.pfx -clcerts -nokeys -out c:\temp\certificate-mycertificate.crt

You will only be asked for the pfx password (import password) since the private key will not be exported.

And for .pfx to .pem

openssl pkcs12 -in file.pfx -out file.nokey.pem -nokeys 

openssl pkcs12 -in file.pfx -out file.withkey.pem

Convert x509 to PEM

openssl x509 -in certificatename.cer -outform PEM -out certificatename.pem



Convert PEM to DER

openssl x509 -outform der -in certificatename.pem -out certificatename.der



Convert DER to PEM

openssl x509 -inform der -in certificatename.der -out certificatename.pem

Convert PEM to P7B

Note: The PKCS#7 or P7B format is stored in Base64 ASCII format and has a file extension of .p7b or .p7c.
A P7B file only contains certificates and chain certificates (Intermediate CAs), not the private key. The most common platforms that support P7B files are Microsoft Windows and Java Tomcat.

openssl crl2pkcs7 -nocrl -certfile certificatename.pem -out certificatename.p7b -certfile CACert.cer



Convert PKCS7 to PEM

openssl pkcs7 -print_certs -in certificatename.p7b -out certificatename.pem



Convert pfx to PEM

Note: The PKCS#12 or PFX format is a binary format for storing the server certificate, intermediate certificates, and the private key in one encryptable file. PFX files usually have extensions such as .pfx and .p12. PFX files are typically used on Windows machines to import and export certificates and private keys.

openssl pkcs12 -in certificatename.pfx -out certificatename.pem



Convert PFX to PKCS#8
Note: This requires 2 commands

STEP 1: Convert PFX to PEM

openssl pkcs12 -in certificatename.pfx -nocerts -nodes -out certificatename.pem



STEP 2: Convert PEM to PKCS8

openSSL pkcs8 -in certificatename.pem -topk8 -nocrypt -out certificatename.pk8



Convert P7B to PFX
Note: This requires 2 commands

STEP 1: Convert P7B to CER

openssl pkcs7 -print_certs -in certificatename.p7b -out certificatename.cer



STEP 2: Convert CER and Private Key to PFX

openssl pkcs12 -export -in certificatename.cer -inkey privateKey.key -out certificatename.pfx -certfile  cacert.cer
Or:
openssl pkcs12 -export -out domain.name.pfx -inkey domain.name.key -in domain.name.crt
Or with intermediate and root:
openssl pkcs12 -export -out domain.name.pfx -inkey domain.name.key -in domain.name.crt -in intermediate.crt -in rootca.crt