Posts

Showing posts with the label Powershell

Securing Powershell Scripts with Code-Signing Certificate

Image
Ideally, all Powershell scripts should be signed by a code signing certificate by a certificate authority that is trusted by the host machine. You can also sign you Powershell script with a a self-signed code-signing certificate. You don't need any other tools like make cert to sign your Powershell script to generate self-signed code-signing certificate because Powershell has a built-in cmdlet ( New-SelfSignedCertificate ) to generate it. Create a Self-Signed Code-Signing Certificate with Powershell CmdLet $subject = "Imran Aftab Rana”  $cert = New-SelfSignedCertificate -Subject $subject -Type CodeSigningCert -CertStoreLocation cert:\LocalMachine\My The above cmdlet creates a self-signed code-signing certificate and places it in Local Machine Personal certificate store. Export a Code-Signing Certificate with Powershell CmdLet You can also export the certificate into a .p7b  file and import it into another system or within same machine. Export-Certifica...

Understanding Powershell ExecutionPolicy and securing Powershell CmdLets/Scripts with Code-Signing Certificate

Image
One of the great things about Powershell is that its powerful and at the same time very safe. Powershell script cannot be executed as is unless user provides permission to execute PS script/cmdlet/module. This is generally set through Powershell Execution Policy. There are four types of Execution Policy : Allsigned RemoteSigned Restricted Unrestricted AllSigned  - Scripts will run only if they have been signed by a trusted publisher. RemoteSigned  - Scripts created locally will run, but those downloaded from the Internet will not (unless they are digitally signed by a trusted publisher). Restricted  - Scripts won’t run. Unrestricted  - Scripts will run regardless of where they have come from and whether they are signed. Ideally, all PS scripts should be digitally signed by a code-signing certificate. But in case, you don't want to buy an expensive code-signing certificate from a Certification Authority (CA), you can create a Self-Signed certificate a...

Fundamental of Powershell Scripting

Image
As a developer/programmer, if you have developed expertise in one programming language - learning another is just about understanding the syntax, the rest of the concepts are similar among all languages. So to get started developing automation scripts for Powershell , here are the things you care about: Read input & write output Data Types Operators Conditional Statements Looping Statement Functions  Object oriented scripting Read input & write output Output to console: Lets start with "Hello World!", in powershell there are two ways to output to console: Write-Host Write-Output The second one (Write-Output) is more preferred way to write to console as it does not destroy pipeline inputs.  You can find the code here Input from console: Reading inputs from console is super easy in powershell. Find the code above on my Github Gist here . Data Types Powershell is based on .NET Framework (and Powershell Core on .NET Core)...