PowerShell: Some Tricks in Using the Here-String to Declare Blocks of Code

Trigger COMMAND CLI from within PowerShell

# Fastest way to list all files in a directory. Bar none.
$getFiles = @"
cmd.exe /C dir '$source' /S /B /W /A:-D
"@
$files = Invoke-Expression -Command:$getFiles;

Loading a dotNet class into PowerShell

# Setting Powershell to skip SSL certificates checking
$classTrustAllCerts = @"
using System.Net;
using System.Security.Cryptography.X509Certificates;
public class TrustAllCertsPolicy : ICertificatePolicy {
public bool CheckValidationResult(ServicePoint srvPoint, X509Certificate certificate,WebRequest request, int certificateProblem) {
return true;
}
}
"@
Add-Type -TypeDefinition $classTrustAllCerts
$trustAllCertsPolicy=New-Object TrustAllCertsPolicy
[System.Net.ServicePointManager]::CertificatePolicy = $trustAllCertsPolicy
[System.Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

Leave a Reply

Your email address will not be published. Required fields are marked *