Posted On February 8, 2020

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

kimconnect 0 comments
blog.KimConnect.com >> Codes >> 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 *

Related Post

SQL Error Msg 3201, Level 16, State 2

Sample Error Message: Msg 3201, Level 16, State 2, Line 2Cannot open backup device '\\1.1.1.1\d$\backup\TEST_MSCRM.bak'.…

PowerShell: Windows Get-EventLog vs Get-WinEvent

Get-Eventlog is the legacy Windows log querying command. Its advanced filtering is limited. Whereas Get-WinEvent,…

PowerShell: Test LDAPS Connection

Function testLdap { [CmdletBinding()] Param( [Parameter(Position=0,ValueFromPipeline=$true)]$dcs=$($ENV:LOGONSERVER -replace '\\',''), [Parameter(Position=1,ValueFromPipeline=$true)]$port='636' ) $ErrorActionPreference = "Stop" $results =…